Java Swing : How to refresh JTable Model from another page button
I have 2 windows one window shown the JTable
Model data, when double click the row will pop up a new window to edit the data, once submit how can I refresh the JTable
?
Customer.java :
JPanel getJPanel() {
if (jPanel == null) {
jPanel = new JPanel();
jPanel.setLayout(null);
jPanel.setSize(new Dimension(792, 420));
jPanel.add(getJScrollPane(), null);
setUpTableData();
}
JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setBounds(new Rectangle(20, 166, 759, 241));
jScrollPane.setViewportView(getJTable());
}
return jScrollPane;
}
JTable getJTable() {
if (jTable == null) {
jTable = new JTable();
jTable.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
new ContactEdit(name,email,phoneNo,phoneNo2,id).getJInternalFrame().setVisible(true);
}
}
});
DefaultTableModel tableModel = (DefaultTableModel) jTable.getModel();
String[] colName = {"No." ,"Name", "Email","Contact No. 1","Contact No. 2","ID"};
tableModel.setColumnIdentifiers(colName);
jTable.getTableHeader().setBackground(Color.WHITE);
jTable.getTableHeader().setForeground(Color.BLUE);
Font Tablefont = new Font("DialogInput",Font.BOLD,12);
jTable.getTableHeader().setFont(Tablefont);
}
return jTable;
}
public void setUpTableData() {
DefaultTableModel tableModel = (DefaultTableModel) jTable.getModel();
ArrayList<Address> list = sql.getContactLists();
int i = 0;
for (i = 0; i < list.size(); i++) {
String[] bill = new String[6];
for (int j = 0; j < 6; j += 6) {
bill[j] = Integer.toString(i);
bill[j + 1] = list.get(i).getName();
bill[j + 2] = list.get(i).getEmail();
bill[j + 3] = list.get(i).getPhoneNo();
bill[j + 4] = list.get(i).getPhoneNo2();
bill[j + 5] = list.get(i).getId();
}
tableModel.addRow(bill);
}
jTable.setModel(tableModel);
}
Edit.java
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(155, 138, 85, 25));
jButton.setText("Update");
jButton.addActionListener(new java.awt.event.ActionL开发者_运维百科istener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
Address a = new Address(jTextField1.getText(),jTextField.getText(),jTextField3.getText(),jTextField2.getText(),"");
sql.updateContact(a, Integer.parseInt(getJTextField4().getText()));
Customer c = new Customer();
c.setUpTableData();
getJInternalFrame().setVisible(false);
getJInternalFrame().dispose();
}
});
}
return jButton;
}
On JPanel
Load will call this setUpTableData
to retrieve the data from database.
On the edit window, I have added an action listener on the update button to refresh the table, but I don't know how to make the jTable refresh the updated data? The setUpTableData on ActionListener
give me Null Pointer.
One solution would be to give the edit window an instance of he other JPanel
. (You can accomplish that by passing the panel in to the constructor of the edit window) Then you can simply call the setUpTableData()
method from inside the ActionListener
.
The code to do this may look like the following:
Edit frame constructor
public EditFrame(CustomerListFrame cListFrame, ... other params) {
this.cListFrame = cListFrame;
}
Update ActionListener
public void actionPerforment(ActionEvent e) {
cListFrame.setUpTableData(the new data);
}
精彩评论