hi all, i want to know if there is a way to either alert my view (JTable) of my model (a class that extends AbsatractDataModel) dataq change.or a way to i can use so the model acknowledges the view when it's data change(a row added to an array list of type array of strings which represents the whole table data).? i added data to my model a new row.anyway this addition does not get viewed on the aJTablke component till i re open the app, but i want to reflect this addition in the model to my JTable component.
beside methods in JTable, AbstractTableModel also has methods to notify the JTable about changes. The most universal one is fireTableDataChanged. Usually, you can always use that one. The other fire... methods might result in a better performance, but usually that would not be notable.
don't forget to increment your row count in the getRowCount method
If you have problems when adding a column then the ...
method should work fine.
// Mathias
SCJP1.4
Mahmoud Hadad
Ranch Hand
Joined: Jan 09, 2006
Posts: 67
posted
0
ok here is my problem.i'm adding a row to an array list of type array of strings which represents the table data,getRowCount(){ returns the size of this array list }.i created a method and named it reflectAddition that takes an array of strings which represents my new row and added it to the array list and then called fireTableChanged() then called repaint on my JTable component and nothing were shown.i used getContentPane().remove(JTable componenet) and then added it.i used the method setValueAt() from my implementation of the AbstractTableModel and nothing were shown.my addition to the result set are not shwon till i re open my application from the beggining.i've been into these for many days so realy any help would be great.
Mahmoud Hadad
Ranch Hand
Joined: Jan 09, 2006
Posts: 67
posted
0
i've seen a code to view an addition to the data in the model to the JTable componenet which was like this: public void reflectAddition(String[] row) { data.add(row);//data represents an array list of type array of //strings which represents the whole table data (rows of the table) fireTableChanged(new TableModelEvent(data)); JTableComponent.repaint(); } but this did not work with me either don't know what i'm missing.any ideas guys?
Mahmoud Hadad
Ranch Hand
Joined: Jan 09, 2006
Posts: 67
posted
0
i re implemented the reflectAddition method as below: reflectAddition() { jTable.tableChanged(new TableModelEvent(new GeneralTableModel())); jTable.repaint(); }
doesn't this method recall the constructor for my model which alreday have the new data but it still don't show the addition till i re open my whole application. wanna know how does the table knows how to get rows of data and columns.is it by using getRowCount(), getColumnCount(), getValueAt() methods? and what is the use of setValueAt() is it used to change the view or only apply changes to the model?
fireTableRowsInserted works for me, and there's no need to call repaint:
[ February 28, 2006: Message edited by: Jeff Albertson ]
There is no emoticon for what I am feeling!
Jeff Albertson
Ranch Hand
Joined: Sep 16, 2005
Posts: 1780
posted
0
That is a lot of code, and I'm sorry, but I don't have the time to sift though it. I posted a short example that worked. If your code isn't working, then you should post an equally short counterexample based on your code. [ February 28, 2006: Message edited by: Jeff Albertson ]
Mahmoud Hadad
Ranch Hand
Joined: Jan 09, 2006
Posts: 67
posted
0
i'm so sorry about the last post, really i'm.don't know i tryed to mimic my code as i could.here is the new code:
public Model() { String[] row={"2736"}; tableData = new ArrayList<String[]>(); tableData.add(row); } public Object getValueAt(int row, int column) { String[] rowArray=(String[])tableData.get(row); return rowArray[column]; } public int getColumnCount() { return columnNames.length; }
public int getRowCount() { return tableData.size(); } public void addRow(String[] row) { int firstRow = tableData.size(); tableData.add(row); int lastRow = tableData.size(); fireTableRowsInserted(firstRow, lastRow); }
private Container contentPane; private JPanel panel; private JTextField caseNumText ; private JButton submitButton; private Model model ; }
it is still not working.don't know why maybe because i'm using two frames and using different objects of the model so when the fireTableRowsInserted() occurrs it generates a TableEvent for a different oject other than the one that GeneralFrame uses? thanks guys for replying.
Jeff Albertson
Ranch Hand
Joined: Sep 16, 2005
Posts: 1780
posted
0
I see two problems:
1. You have two frames and two instances of Model, when I think you only want one model. You could pass the exising model to the constructor for addGeneralFrame instead of creating an extra model there. This may not be the best design (why should the data entry window know there is a table involved) but it will get things running.
2. You window is cutting off the left side of cells so you can't see the entry. try calling pack() instead of setSize().
Mahmoud Hadad
Ranch Hand
Joined: Jan 09, 2006
Posts: 67
posted
0
thanks alot guys.you really were a great help.i don't know how did i miss the concept passing by refrence but i think it may be cause i'm using JTable for the first time and i was in a bad mentall shape. Thanks alot guys. Regards, Mahmoud Hadad