0

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..

JanB
  • 7
  • 7
  • *"is it possible to pop up a dialog box that shows both fields at the same time?"* Yes. Put the two text fields in a single `JPanel` with appropriate `JLabel` components to identify each, and put the panel in the `JOptionPane`. General: For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson May 05 '19 at 13:10
  • Possibly related: [Simple popup java form with at least two fields](https://stackoverflow.com/q/3002787) – Pshemo May 05 '19 at 13:14

1 Answers1

1

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); 

              }
           }
L. Wyeth
  • 36
  • 4