GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes JTable(my model is changed how do i update my view)? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable(my model is changed how do i update my view)?" Watch "JTable(my model is changed how do i update my view)?" New topic
Author

JTable(my model is changed how do i update my view)?

Mahmoud Hadad
Ranch Hand

Joined: Jan 09, 2006
Posts: 67
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.
Kai Witte
Ranch Hand

Joined: Jul 17, 2004
Posts: 356
hello,

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.

Kai


Kai Witte's business website Kai Witte's private homepage
Mathias Nilsson
Ranch Hand

Joined: Aug 21, 2004
Posts: 367
If you add a new row then use the



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
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
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
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?
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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
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
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:

GeneralFrame.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class GeneralFrame extends JFrame
{
public GeneralFrame()
{
init();
}
private void init()
{
contentPane = getContentPane();
panel = new JPanel(new BorderLayout());

jTable1 = new JTable();
model = new Model();
jTable1.setModel(model);
scrollPane = new JScrollPane(jTable1);

addButton = new JButton("Add");
addButton.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e)
{
addGeneralFrame agf = new addGeneralFrame();
agf.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
agf.setVisible(true);
}

});

panel.add(scrollPane, BorderLayout.EAST);
panel.add(addButton, BorderLayout.SOUTH);
contentPane.add(panel);
setSize(400,400);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
GeneralFrame gf = new GeneralFrame();
gf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gf.setVisible(true);

}
});
}
private Container contentPane ;
private JPanel panel;
private JTable jTable1;
private JScrollPane scrollPane;
private Model model ;
private JButton addButton;
}


Model.java

import java.util.*;
import java.sql.*;
import javax.swing.table.*;

public class Model extends AbstractTableModel{


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 ArrayList<String[]> tableData;
private String[] columnNames = {"Case Number"};
}



addGeneralFrame.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class addGeneralFrame extends JFrame
{
public addGeneralFrame()
{
init();
}
private void init()
{
contentPane = getContentPane();
panel= new JPanel(new BorderLayout());

model = new Model();
caseNumText = new JTextField("");
submitButton = new JButton("Submit");

submitButton.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent e)
{
String caseNumStr = caseNumText.getText();
String[] row = {caseNumStr };
model.addRow(row);
setVisible(false);
}
});

panel.add(caseNumText, BorderLayout.NORTH);
panel.add(submitButton, BorderLayout.SOUTH);
contentPane.add(panel);
setSize(400,400);
}

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
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
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
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Keep up the good work!

 
jQuery in Action, 2nd edition
 
subject: JTable(my model is changed how do i update my view)?