CarlosMorgado
CarlosMorgado

Reputation: 315

Using a JDialog as input

I am trying to use a JDialog as input for a String. But the text I get is from before I click the Button.

this is my dialog:

public class AddMemberDialog extends JDialog {

    private JTextField name;

    public AddMemberDialog() {
        super(new JFrame("Add Member"), "Add Member");
        this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
        this.setMinimumSize(new Dimension(500, 500));
        this.name = new JTextField();

        JButton add = new JButton("Add");
        add.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                close();            
            }
        });
        this.setLayout(new GridLayout(2, 1, 5, 5));

        this.add(name);
        this.add(add);
        this.pack();
    }

    private void close(){   this.dispose(); }

    public String getName(){    return this.name.getText(); }
}

and here is what I am using to get to String:

AddMemberDialog input = new AddMemberDialog();
input.setLocationRelativeTo(this);
input.setVisible(true);
String txt = input.getName();

Upvotes: 2

Views: 3977

Answers (2)

DreadHeadedDeveloper
DreadHeadedDeveloper

Reputation: 620

import javax.swing.JDialog;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridLayout;


public class AddMemberDialog extends JDialog 
{

   private JTextField name;

   public static void main(String[] args)
   {

      AddMemberDialog input = new AddMemberDialog();
      input.setLocationRelativeTo(null);
      input.setVisible(true);

   }

   public AddMemberDialog() 
   {
      super(new JFrame("Add Member"), "Add Member");

      this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
      this.setMinimumSize(new Dimension(500, 500));
      this.name = new JTextField();

      JButton add = new JButton("Add");
      add.addActionListener(
            new ActionListener() {

               public void actionPerformed(ActionEvent arg0) {
                  close();            
               }
            });



      JButton takeInput = new JButton("takeInput");
      takeInput.addActionListener(
            new ActionListener()
            {
               public void actionPerformed(ActionEvent e)
               {


                  String txt = getName();
                  System.out.println(txt);

               }
            }
            );

      this.setLayout(new GridLayout(3, 1, 5, 5));

      this.add(name);
      this.add(add);
      this.add(takeInput);
      this.pack();
   }

   private void close()
   {   

      this.dispose(); 

   }

   public String getName()
   {    

      return this.name.getText(); 

   }

}

Alright, so basically, your issue here is that, if you simply leave the code

              AddMemberDialog input = new AddMemberDialog();
              input.setLocationRelativeTo(this);
              input.setVisible(true);
              String txt = input.getName();

It will automatically take in your input the second the IDE reaches that line of code. Namely, unless you put something into it before the IDE gets there (and the IDE gets there in milliseconds), it won't take anymore input after that. So to compensate, we don't let the program take input until we are good and ready, hence the need for a button. In my above code, I made a new JButton and called it takeInput. Also gave it an ActionListener and in that ActionListener, made it do what you asked for. Now, I can control when the input happens.

Upvotes: 3

jmclellan
jmclellan

Reputation: 550

You can use JOptionPane for this.

import javax.swing.JOptionPane;

public class TestClass
{

  public static void main(String[] args)
  {
    String input = JOptionPane.showInputDialog("Add Member");
    System.out.println(input);
  }
}

Upvotes: 1

Related Questions