将数据传递到新的GUI

将数据传递到新的GUI

本文介绍了将数据传递到新的GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JTable,上面有我从CSV文件中读取的内容.我正在使用以下方法,当我单击一行时,它将打开一个新的JFrame并关闭上一个.它会显示诸如ID,坐标,该表上所写内容的状态之类的信息,并且可以根据需要进行编辑.例如.下表:

I have a JTable with contents on it which I have read from a CSV file. I am using the following method which when I click on a row it will open up a new JFrame and close the previous one. It will display things such as ID, co-ordinates, status of what is written on that table and can edit them if wanted. E.G. table below:

|ID  |co-ordinates      | status   |
| 1  |   (3,21)          |  pending |
|  2 |  (4,21)           | full     |
|  3 |  (9, 12)          |  empty   |

如果单击第1行,则会弹出ID(1),坐标(3,21)和状态的框,并在另一框中的文本字段中弹出该框,并且可编辑.我能够执行单击功能,但是不确定在单击该行时如何将数据带入下一帧.

If I click row 1, it will pop up with frame of the ID(1), co ordinates(3,21) and status in a text field in another frame and is editable. I am able to do the clicking function but am not sure how to take that data onto the next frame when clicking the row.

 //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();
                }
             }
          });

单击行时如何将数据带入下一帧?

How to take that data onto the next frame, when clicking the row?

推荐答案

我不知道您的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)

这篇关于将数据传递到新的GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 00:22