So i have a simple java GUI that has two editable text fields.

If i enter the first one, it will pop up a dialog that shows the first field.

##########################################
# Message                              x #
##########################################
# Your textfields                        #
# 1: this is an editable field           #
#                                        #
##########################################

If i enter, it will pop up a dialog that shows the second field.

##########################################
# Message                              x #
##########################################
# Your textfields                        #
# 2: this is an editable field 2         #
#                                        #
##########################################

However, is it possible to pop up a dialog box that shows both fields at the same time?

##########################################
# Message                              x #
##########################################
# Your textfields                        #
# 1: this is an editable field           #
# 2: this is an editable field 2         #
##########################################

This is what I've done so far:

My textFrames:

class EditTextFrame extends JFrame 
{
  private JTextField editableField1; 
  private JTextField editableField2; 


   public EditTextFrame()
   {
          setLayout(new FlowLayout());

       editableField1 = new JTextField("This is an editable field", 25); 
       editableField2 = new JTextField("This is an editable field2", 25); 

       add(editableField1);
       add(editableField2);

       TextFieldHandler handler = new TextFieldHandler();
       editableField1.addActionListener(handler);
       editableField2.addActionListener(handler);
   } 

My ActionEvents:

private class TextFieldHandler implements ActionListener 
   {
      @Override
      public void actionPerformed(ActionEvent e)
      {
         String string1 = ""; 
         String string2 = ""; 


         if (e.getSource() == editableField1)
            string1 = String.format("1: %s",
               e.getActionCommand());

          if (e.getSource() == editableField2)
            string2 = String.format("2: %s",
               e.getActionCommand());


         JOptionPane.showMessageDialog(null, "Your textfields \n" + string1 + string2
                    ); 

      }
   }

I know that my string concat is useless as one of them will be empty since the if statement stops after a condition is met.. but i cant seem to find a way to work around..

1 Answers

1
L. Wyeth On Best Solutions

If both textfields will be filled every time (expected input is both fields, and expected output is both) then you can create two separate event handlers and attach one to each text field. Have the one for the first text field save the string in a global variable, then the second one concat the two strings together and then pop up your text box.

However, to handle all types of inputs (just one or both fields) I would add a third "submit" button. Have this submit button pull the strings from both text fields, then pop up your dialog. Initialize the strings to empty ("") to ensure no null pointers.

 private String field1String = "";
 private String field2String = "";

    // you'll need one of these for each text field
    private class TextField1Handler implements ActionListener 
           {
              @Override
              public void actionPerformed(ActionEvent e)
              {
                field1String = yourField1.getText();

              }
           }

      // attach this to your button
      private class ButtonClickHandler implements ActionListener 
           {
              @Override
              public void actionPerformed(ActionEvent e)
              {
                 JOptionPane.showMessageDialog(null, "Your textfields \n" + 
                  field1String + field2String); 

              }
           }