This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Updating data in a JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Updating data in a JTable" Watch "Updating data in a JTable" New topic
Author

Updating data in a JTable

Talhah Mafawalla
Greenhorn

Joined: Nov 05, 2006
Posts: 24
Hi all,

I seem to have an anoying problem. The problem is that when i try to insert data in to a JTable using a JButton the JTable wont update until i resize the frame manually, sometimes this does not work. Though the same code works perfectly on some computers, dont know if it is the JDK version or not. Anyways heres the code:

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

public class TableDemo2 extends JFrame{

Vector columnNames, data;
JButton bt1;

public TableDemo2() {
super("TableDemo2");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

columnNames = new Vector();
columnNames.addElement("FirstName");
columnNames.addElement("LastName");
columnNames.addElement("Marks");
columnNames.addElement("Pass");

data = new Vector();

JTable table = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane(table);

JLabel lb1 = new JLabel("Results of Java Programming", JLabel.CENTER);
lb1.setFont(new Font("Courier", Font.ITALIC + Font.BOLD, 16));

bt1 = new JButton("Show Data");
bt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for(int x = 1; x <= 15; x++){
Vector v1 = new Vector();
v1.addElement("FirstName" + x);
v1.addElement("LastName" + x);
v1.addElement(new Integer(60 + 2 * x));
v1.addElement(new Boolean(true));
data.addElement(v1);
}

JTable table = new JTable(data, columnNames);
validate();
}
});

add(lb1, BorderLayout.NORTH);
add(scrollPane, BorderLayout.CENTER);
add(bt1, BorderLayout.SOUTH);
setBounds(100,100,400,300);
setVisible(true);
}

public static void main(String[] args) {
new TableDemo2();
}

}

Sorry if the post is too long, but i need help on this.
Thanks in advance.
Talhah.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
in your actionListener you create a new JTable, but don't do anything with it.

the fact it works on some computers indicates that in your real code you
probably remove the old table from the scrollpane, then add the new one.
in this case it should be
scrollPane.revalidate();
repaint();//sometimes required, sometimes not, safest to have it

you don't really need to create a new JTable, then remove/add, just change
the tableModel

(table will need to be made final, or moved so it is a class field - not local)

//JTable table = new JTable(data, columnNames);
//validate();
table.setModel(new javax.swing.table.DefaultTableModel(data, columnNames));
Talhah Mafawalla
Greenhorn

Joined: Nov 05, 2006
Posts: 24
Thanks alot Michael that has helped alot, i think im on the right path using table models rather than adding data direct to the jtable.

Thanks alot again

Talhah
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Updating data in a JTable