Java Swing:如何从另一个页面按钮刷新 JTable 模型
我有2个窗口,其中一个窗口显示JTable
模型数据,双击该行时会弹出一个新窗口来编辑数据,提交后如何刷新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.ActionListener() {
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 将调用此 setUpTableData
从数据库检索数据。 在编辑窗口上,我在更新按钮上添加了一个动作侦听器来刷新表,但我不知道如何使jTable刷新更新的数据? ActionListener 上的 setUpTableData 给我空指针。
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.ActionListener() {
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种解决方案是为编辑窗口提供另一个
JPanel
的实例。 (您可以通过将面板传递给编辑窗口的构造函数来完成此操作)然后您可以简单地从ActionListener
内部调用setUpTableData()
方法。执行此操作的代码可能如下所示:
编辑框架构造函数
更新 ActionListener
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 thesetUpTableData()
method from inside theActionListener
.The code to do this may look like the following:
Edit frame constructor
Update ActionListener