AlistairMonkeyFinger
Programmer
Hi,
struggling with some basic swing code, excuse the bad practises here, but i've created a simple example of a JTable and a JButton in a JFrame, when i click the button the table should change but it doesn't. The code is posted below, i've tried all sorts of cominations of using repaint, revalidate, updateUI methods but cannot get the thing to behave how i want. Any ideas ??
Thanks
Alistair
struggling with some basic swing code, excuse the bad practises here, but i've created a simple example of a JTable and a JButton in a JFrame, when i click the button the table should change but it doesn't. The code is posted below, i've tried all sorts of cominations of using repaint, revalidate, updateUI methods but cannot get the thing to behave how i want. Any ideas ??
Code:
package com.myempire;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
public class SwingTest extends JFrame
{
JPanel jp;
JScrollPane js;
JTable jt;
Object[][] data = {
{"Mary", "Campione",
"Snowboarding", new Integer(5), new Boolean(false)},
{"Alison", "Huml",
"Rowing", new Integer(3), new Boolean(true)},
{"Kathy", "Walrath",
"Chasing toddlers", new Integer(2), new Boolean(false)},
{"Mark", "Andrews",
"Speed reading", new Integer(20), new Boolean(true)},
{"Angela", "Lih",
"Teaching high school", new Integer(4), new Boolean(false)}
};
Object[][] data2 = {
{"Mary", "Campione",
"Snowboarding", new Integer(5), new Boolean(false)},
{"Alison", "Huml",
"Rowing", new Integer(3), new Boolean(true)}
};
String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};
public SwingTest()
{
jp = new JPanel();
jt = new JTable(data,columnNames);
js = new JScrollPane(jt);
JButton jb = new JButton("Click to change table");
jb.addActionListener(new buttonHandler());
jp.add(jb);
jp.add(js);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().add(jp);
}
private class buttonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
jt = new JTable(data2,columnNames);
}
}
public static void main(String[] args)
{
SwingTest swingTest = new SwingTest();
swingTest.show();
}
}
Thanks
Alistair