1

私はスイングアプリケーションに取り組んでおり、多数の JTextField の値を保存するための最良の方法を決定しようとしました。アプリケーションは、ネットワークの場所からプロパティ ファイルを読み取ります。プロパティ ファイルには、特定のユーザーごとに約 10 ~ 12 個のプロパティがあります。たとえば、propertyA_1 はユーザー 1 のプロパティであり、propertA_2 はユーザー 2 のプロパティです。プロパティには約 10 ~ 12 人のユーザーが存在するため、値を表示するために約 80 個の JTextField が作成されます。各ユーザーのプロパティは JTabbedPane に表示されます。私が今やりたいことは、ユーザーが各 JTabbedPane に表示されている任意のユーザーの任意の JTextField の値を変更し、[保存] ボタンをクリックすると、プロパティの値が保存されることです。多数の JtextField を処理する最善の方法を教えてください。私はそれを2つの方法で行うと思います

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

1 に答える 1

2

@nlcE cOw が示唆していることを少し詳しく説明すると、次のようなことができます。

1) プロパティを final にする:

        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 パターンを使用することです。

于 2012-05-29T09:03:56.040 に答える