【问题标题】:Capturing text change of a large number of JTextFields捕获大量 JTextField 的文本变化
【发布时间】:2012-05-29 06:21:08
【问题描述】:

我正在开发一个 Swing 应用程序,并在决定什么是保存大量 JTextField 值的最佳方法时感到震惊。 应用程序从某个网络位置读取属性文件。属性文件为每个特定用户提供大约 10-12 个属性,例如 propertyA_1 是用户 1 的属性,propertA_2 是用户 2 的属性。一个属性中将有大约 10-12 个用户,因此它将产生大约 80 个 JTextField 来显示值。每个用户的属性都将显示在 JTabbedPane 上。 我现在要做的是,当用户更改每个 JTabbedPane 中显示的任何用户的任何 JTextField 的值并单击“保存”按钮时,应该保存该属性的值。 告诉我处理大量 JtextField 的最佳方法是什么。我想有两种方法

  1. 单击“保存”按钮时,通过从所有 JTextField 中获取值来保存属性的每个值。
  2. 某种方法可以仅获取更改了文本的那些 JTextField 并保存它们的值。

我不确定如何通过选项 2 来做这件事。但我认为这是最好的方法。

这是我的代码:

public class IOSAutomationTool1 implements ActionListener {

       JButton saveButton  = new JButton("Click to Save");

    /**
     * @param args the command line arguments
     */

    public  void Test() throws IOException {

         File file = new File("C:\\Documents and Settings\\test\\Desktop\\test.properties");

         if( file != null ){

             Properties property = new Properties();

             property.load(new FileInputStream(file));

              JFrame frame = new JFrame("IOSAutomationToolFourthPage");
              frame.setLayout(new FlowLayout());
              JPanel framePanel = new JPanel();
              JPanel buttonPanel = new JPanel();
              framePanel.setSize(700,700);
               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              String numberOfClients = property.getProperty("numberOfClients");
              System.out.println("number of clients: "+numberOfClients);


                saveButton.addActionListener(this);
                buttonPanel.add(saveButton);
              if (!numberOfClients.isEmpty()) {

                   int numberOfClientNumb = Integer.parseInt(numberOfClients);

                      System.out.println("numberOfClientNumb ::" + numberOfClientNumb);

                       JTabbedPane tab = new JTabbedPane();

                      // JTabbedPane tab2 = new JTabbedPane();
                    for( int i=1; i<=numberOfClientNumb; i++){

                       JPanel panel = new JPanel();
                      panel.setSize(400, 400);
                      panel.setLayout(new GridBagLayout());
                      GridBagConstraints gbc = new GridBagConstraints();
                       gbc.insets = new Insets(1, 1, 1, 1);

                        gbc.fill = GridBagConstraints.BOTH;
                        gbc.weightx = 0;
                        gbc.gridx = 0;

                      //Remote IP 
                      JLabel remoteIPLbl = new JLabel("Remote IP : ");
                      panel.add(remoteIPLbl,gbc);
                      JLabel userNameLbl = new JLabel("User Name : ");
                      panel.add(userNameLbl,gbc);
                      JLabel remotePasswordLbl = new JLabel("Remote Password : ");
                      panel.add(remotePasswordLbl,gbc);
                      JLabel remotePathLbl = new JLabel("Remote Path : ");
                      panel.add(remotePathLbl,gbc);
                      JLabel deviceOrSimulatorLbl = new JLabel("Device or Simulator : ");
                      panel.add(deviceOrSimulatorLbl,gbc);

                      gbc.gridx = 1;
                      gbc.weightx = 1;

                      String remoteIPVal = property.getProperty("remoteIP_"+i);
                      JTextField remoteIPField = new JTextField(remoteIPVal);
                      //remoteIPLbl.setBounds(50, 100, 2, 2);
                     remoteIPField.setBounds(200, 100, 2, 2);
                      remoteIPField.setColumns(30);
                      panel.add(remoteIPField,gbc);    

                      //Remote User Name


                      String userNameVal = property.getProperty("remoteUserName_"+i);
                      JTextField userNameField = new JTextField(userNameVal);
                     // userNameLbl.setBounds(50, 200, 2, 2);
                      userNameField.setBounds(200, 200, 2, 2);
                      userNameField.setColumns(20);
                      panel.add(userNameField,gbc);   

                      //Remote Password

                      String remotePasswordVal = property.getProperty("remotePassword_"+i);
                      JTextField remotePasswordField = new JTextField(remotePasswordVal);
                     // remotePasswordLbl.setBounds(50, 300, 2, 2);
                      remotePasswordField.setBounds(200, 300, 2, 2);
                      remotePasswordField.setColumns(20);
                      panel.add(remotePasswordField,gbc); 


                      //Remote Path

                      String remotePathVal = property.getProperty("remotePath_"+i);
                      JTextField remotePathField = new JTextField(remotePathVal);
                     // remotePathLbl.setBounds(50, 400, 2, 2);
                     remotePathField.setBounds(200, 400, 2, 2);
                      remotePathField.setColumns(100);
                      panel.add(remotePathField,gbc); 



                      //deviceOrSimulator

                      String deviceOrSimulatorVal = property.getProperty("deviceOrSimulator_"+i);
                      JTextField deviceOrSimulatorField = new JTextField(deviceOrSimulatorVal);
                      //deviceOrSimulatorLbl.setBounds(50, 500, 2, 2);
                      deviceOrSimulatorField.setBounds(200, 500, 2, 2);
                      deviceOrSimulatorField.setColumns(1);
                      panel.add(deviceOrSimulatorField,gbc); 


                      tab.add("Client "+i,panel);
                      System.out.println(""+i);                     
                   }

                    framePanel.add(tab);

                    frame.add(framePanel);

                   // frame.setLayout(new Lay);
              }
               //frame.add(saveButton);
               frame.add(buttonPanel);
              frame.setSize(800, 800);
              frame.pack();
              frame.setVisible(true);

         }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");

        if(e.getSource() == saveButton){
            System.out.print("button clicked...........");
        }
    }

    public static void main(String[] args) throws IOException{

        IOSAutomationTool1 obj = new IOSAutomationTool1();
        obj.Test();
    }
}

【问题讨论】:

  • 我不确定我是否找到了答案,但正如您描述的情况,您也为我回答了这个问题。当您从属性文件中读取时,我猜您是在键的帮助下执行此操作的,以获取与该键关联的值,所以为什么您不能简单地将其应用为您的选项 2,将此键设置为 @ 987654322@,只需按一下按钮即可更新此键的值。
  • 如何编码?我如何知道 JTextField 的值是否在单击 Jbutton(按钮单击事件)时发生更改。由于我只想更新那些已更改的值,因此其余部分将保持原样。
  • 您还可以查看另一种方法,让一个类说USER,现在当您从DATABASE 检索值时,将它们放在这个类中,现在当您按Save按钮使该类的另一个对象说updatedUser 并将JTextField 的当前值复制到这个新实例,现在比较两个值,只更改已修改的值,其余的保留。 AFAIK,在各个系统上计算值要快得多,然后更新整个数据库,进行一次修改。

标签: java swing


【解决方案1】:

要详细说明@nlcE cOw 的建议,你可以做的是这样的:

1) 使您的财产成为最终的:

        final Properties property = new Properties();

2) 在你的 for 循环之前,创建一个 DocumentListener:

    DocumentListener listener = new DocumentListener() {

        private void updatePropertyForEvent(final Properties property, DocumentEvent e) {
            Document document = e.getDocument();
            Object value = document.getProperty("key");
            if (value !=null)
                try {
                    property.setProperty((String) value, document.getText(0, document.getLength()));
                } catch (BadLocationException e1) {
                    // Should not happpen
                    e1.printStackTrace();
                }
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            updatePropertyForEvent(property, e);
        }

        @Override
        public void insertUpdate(DocumentEvent e) {
            updatePropertyForEvent(property, e);                
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            updatePropertyForEvent(property, e);                
        }
    };

3) 对于您创建的每个文本字段,您在关联文档上放置一个属性,该属性将存储它所代表的属性,并将侦听器添加为 DocumentListener:

String remotePasswordVal = property.getProperty("remotePassword_"+i);
JTextField remotePasswordField = new JTextField(remotePasswordVal);
remotePasswordField.getDocument().putProperty("key", "remotePassword_"+i);
remotePasswordField.getDocument().addDocumentListener(listener);

4) 当您按下“保存”时,您只需将 property 保存到 outputStream。

这是快速而肮脏的解决方案。更简洁的选择是使用适当的 MVC 模式。

【讨论】:

  • 我想这很好解释了,我希望它如何完成 :-) 另一种方法是拥有一个 JTextField 数组,并将相应字段的索引添加到 ArrayList,并存储数据库中各个字段的内容。我不在任何 PC 附近,这个 IPAD 不给我运行程序的特权,所以今天写 cmets :-) +1,为你的这个输入。
猜你喜欢
  • 2020-08-15
  • 2011-08-22
  • 2013-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-27
相关资源
最近更新 更多