且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将数据传递到新的GUI

更新时间:2023-12-03 17:14:58

我不知道您的JTable具有哪种类型的内容,我只能提供一个通用的解决方案.

Without knowing what type of content your JTable has, I can only offer a generic solution.

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)

这样您可以创建Object[]并将其发送到AddEdit的构造函数或编写方法getJTableObject()AddEdit中的类似内容.这取决于您是否可以更改AddEdit.

With this you can i.e. create an Object[] and send this to the constructor of AddEdit or write a method getJTableObject() or something similar in AddEdit. This depends on wether you can change AddEdit or not.

您还应该考虑安德鲁斯的建议并使用cardLayout.例如,您可以使用 ObserverPattern 并向周围发送对象.

You also should consider Andrews advice and use the cardLayout. With this you could for instance use an ObserverPattern and send your object around.

另一种方法是使用JOptionPane:

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中的内容填充这些字段,然后当用户单击确定"时,更新JTable.

newStatusPanel simply is a JPanel on which you put i.e. JTextFields. You then fill those fields with the content from the JTable by the method I have shown earlier and when the user clicks okay, you update the JTable.

// Do something with the result
if (response == JOptionPane.OK_OPTION) {
model.addRow(new Object[] { txtID.getText(), coordinates.getText(), ... });

这看起来像这样:

(PS:稍后我将基于文本的密码更改为基于哈希的密码.请忽略这种公然的不安全的密码处理方式)

(PS: I will later change the text-based passwords to hash-based. Please ignore this blatant unsecure way of dealing with passwords)