Reputation: 1709
The scenario is that I have a JTable on my JFrame named "MainFrame" and I can edit the data saved in a xml file using a JDialog named "EditFrame". I can change the data in the xml file by click the save button on "EditFrame" but it won't reload the JTable on "MainFrame". The code for MainFrame is as below:
package ca.ism.wen.gui;
import java.awt.BorderLayout;
import java.awt.Container;
...
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
public class MainFrame extends JFrame implements ActionListener{
static JTable jt = null;
static List<RdpServ> rdps = null;
static Vector<String> header = null;
static Vector<Vector<String>> data = null;
static TableModel tm = null;
public MainFrame(){
header = new Vector<String>();
...
loadData();
jt = new JTable(tm);
Container con = this.getContentPane();
JScrollPane p = new JScrollPane(jt);
con.add(p);
JButton btnEdit = new JButton("Edit");
btnEdit.setActionCommand("Edit");
btnEdit.addActionListener(this);
JButton btnLogout = new JButton("Logout");
btnLogout.setActionCommand("Logout");
btnLogout.addActionListener(this);
JPanel jp = new JPanel();
jp.add(btnEdit);
jp.add(btnLogout);
con.add(jp, BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800,600);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.equals("Edit")) {
EditFrame ef = new EditFrame();
ef.addWindowListener(new WindowAdapter(){
@Override
public void windowClosed(WindowEvent e) {
refresh();
}
});
}
else if (cmd.equals("Logout")) {
UserDao.signoff();
System.exit(0);
}
}
public static void refresh(){
loadData();
jt.repaint();
}
public static void loadData(){
rdps= UserDao.getRs();
data = new Vector<Vector<String>>();
for (RS rdp:rdps){
Vector<String> v = new Vector<String>();
...
data.add(v);
}
tm = new DefaultTableModel(data,header){
public boolean isCellEditable(int row, int column)
{
return false;
}
};
}
}
I googled a lot and tried the solution online. None of them works for my code. Please help. Thanks, guys!!!
Upvotes: 0
Views: 1284
Reputation: 15428
tm = new DefaultTableModel(data,header){
public boolean isCellEditable(int row, int column)
{
return false;
}
};
Ok, after creating the model i don't see you added the model
back to the table
inside the loadData()
function. After creating the tm
try: jt.setmodel(tm)
, as jt
is your table instance inside the loadData()
function.
jt
, tm
wired name, hard to understand: make them a little bigger, such as: jTable
, tModel
etc
Edit:
public static void loadData(){
rdps= UserDao.getRs();
data = new Vector<Vector<String>>();
for (RS rdp:rdps){
Vector<String> v = new Vector<String>();
...
data.add(v);
}
tm = new DefaultTableModel(data,header){
public boolean isCellEditable(int row, int column)
{
return false;
}
};
jt.setModel(tm); // <------------------- here
}
Upvotes: 1