Swing 将数据传递到新GUI
我有一个Swing 将数据传递到新GUI,swing,oop,user-interface,jframe,Swing,Oop,User Interface,Jframe,我有一个JTable,上面有我从CSV文件中读取的内容。我使用以下方法,当我点击一行时,它将打开一个新的JFrame,并关闭上一个。它将显示诸如ID、坐标、表中所写内容的状态等内容,并可根据需要对其进行编辑。例如下表: |ID |co-ordinates | status | | 1 | (3,21) | pending | | 2 | (4,21) | full | | 3 | (9, 12)
JTable
,上面有我从CSV文件中读取的内容。我使用以下方法,当我点击一行时,它将打开一个新的JFrame
,并关闭上一个。它将显示诸如ID、坐标、表中所写内容的状态等内容,并可根据需要对其进行编辑。例如下表:
|ID |co-ordinates | status |
| 1 | (3,21) | pending |
| 2 | (4,21) | full |
| 3 | (9, 12) | empty |
若我单击第1行,它将弹出另一个框的文本字段中的ID(1)、坐标(3,21)和状态的框,并且是可编辑的。我能够执行单击功能,但不确定在单击行时如何将该数据带到下一帧
//in location class
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {
int row = table.getSelectedRow();
AddEdit An = new AddEdit(); //goes to next class
An.setVisible(true);
dispose();
}
}
});
如何在单击行时将该数据带到下一帧?在不知道您的
JTable
具有什么类型的内容的情况下,我只能提供一个通用的解决方案
int row = table.getSelectedRow();
int userID = (Integer) table.getValueAt(row, 0);
// is co-ordinates [sic] a String or a Point?
// You can do the same as for userID and use (row,1) to get the value
String status = (String) table.getValueAt(row, 2)
使用此方法,您可以创建一个对象[]
并将其发送给AddEdit
的构造函数,或者在AddEdit
中编写一个方法getJTableObject()
或类似的内容。这取决于您是否可以更改AddEdit
你也应该考虑安德鲁斯的建议并使用卡片布局。例如,有了它,你可以使用一个电子邮件发送你的对象
另一种方法是使用
作业窗格
:
Object[] message = { "Please update the information:", newStatusPanel };
int response = JOptionPane.showConfirmDialog(null, message, "Update information",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
newStatusPanel
只是一个JPanel
,您可以在其上放置,即JTextFields
。然后,使用前面显示的方法将JTable
中的内容填入这些字段,当用户单击OK时,更新JTable
// Do something with the result
if (response == JOptionPane.OK_OPTION) {
model.addRow(new Object[] { txtID.getText(), coordinates.getText(), ... });
这看起来像这样:
(注:我稍后会将基于文本的密码更改为基于哈希的密码。请忽略这种明显不安全的密码处理方式)如果不知道您的
JTable
有什么类型的内容,我只能提供一个通用的解决方案
int row = table.getSelectedRow();
int userID = (Integer) table.getValueAt(row, 0);
// is co-ordinates [sic] a String or a Point?
// You can do the same as for userID and use (row,1) to get the value
String status = (String) table.getValueAt(row, 2)
使用此方法,您可以创建一个对象[]
并将其发送给AddEdit
的构造函数,或者在AddEdit
中编写一个方法getJTableObject()
或类似的内容。这取决于您是否可以更改AddEdit
你也应该考虑安德鲁斯的建议并使用卡片布局。例如,有了它,你可以使用一个电子邮件发送你的对象
另一种方法是使用
作业窗格
:
Object[] message = { "Please update the information:", newStatusPanel };
int response = JOptionPane.showConfirmDialog(null, message, "Update information",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
newStatusPanel
只是一个JPanel
,您可以在其上放置,即JTextFields
。然后,使用前面显示的方法将JTable
中的内容填入这些字段,当用户单击OK时,更新JTable
// Do something with the result
if (response == JOptionPane.OK_OPTION) {
model.addRow(new Object[] { txtID.getText(), coordinates.getText(), ... });
这看起来像这样:
(注:我稍后会将基于文本的密码更改为基于散列的密码。请忽略这种明显不安全的密码处理方式)”。打开一个新的JFrame并关闭以前的JFrame。1)请参见2)改用中所示的。顺便说一句-鉴于这是一个问答网站,我以“当点击该行时,如何将该数据放到下一帧?”请不要忘记添加一个问题。yup将更改问题,感谢您的回答。“…打开一个新的JFrame并关闭上一个JFrame。”1)请参见2)使用。顺便说一句-鉴于这是一个问答网站,我以单击行时,如何将该数据带到下一帧?“请不要忘记添加问题。是的,将更改问题谢谢您的回答。