1

I'm writing an application in Java using the Java Swing library and am looking for a functionality equivalent to these lines of code from C# using WindowsForms:

 MyDialog form = new MyDialog();
 form.showDialog();

 if (form.DialogResult == DialogResult.OK)
      doSomething();

I haven't been able to find an equivalent functionality for JFrames in Java.

The code I'm working on is the following:

 LoginFrame loginFrame = new LoginFrame(CONTROLLER);
 loginFrame.setVisible(true);

The previous 2 lines of code launch a log-in window where the user can input his e-mail and password. Said window displays 2 buttons: OK and CANCEL. After the window has closed, I'm interested in knowing which one of the 2 buttons the user has pressed.

What is the standard way of doing this in Java Swing with JFrames?

ismarlowe
  • 119
  • 2
  • 13
  • 1
    Probably `JOptionPane.showConfirmDialog(..);`. – Andrew Thompson Dec 21 '17 at 16:18
  • My initial conclusion is that my choice of the JFrame object for dialogs in wrong... can you confirm? Should I be using JDialogs instead? Thanks! – ismarlowe Dec 21 '17 at 16:22
  • 1
    *"Should I be using JDialogs instead?"* Absolutely, for this, either a modal `JDialog` or a `JOptionPane` (which is modal by default). See [this answer](https://stackoverflow.com/a/47644458/418556) for a self contained example of a log-in using a `JOptionPane`. See also [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) (Typically only the main app. should be based on a `JFrame`.) – Andrew Thompson Dec 21 '17 at 17:10

2 Answers2

1

You can set ActionListener to your buttons. There's many ways.

Anonymous Action Listener

button1.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        if(e.getSource() == button1) {
            //if clicks the first button
        } else if (e.getSource() == button2) {
            //if clicks the second button
        }
    }
});

Class that implements Action Listener (best option for maintenance issues)

class CheckButtonActionListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        if(e.getSource() == button1) {
            //if clicks the first button
        } else if (e.getSource() == button2) {
            //if clicks the second button
        }
    }

}

and, set the class to your JButton:

CheckButtonActionListener checker = new CheckButtonActionListener();
button.addActionListener(checker);

or:

button1.addActionListener(new CheckButtonActionListener();
KL_
  • 1,503
  • 1
  • 8
  • 13
1

An application will have a single JFrame as the main window.

If you need child window you would use a JDialog. A JDialog is like a JFrame. You need to code all the logic and handle all the button events yourself

A JOptionPane is a pre-packaged JDialog that give you some default functionality.

You can create a simple JOptionPane with multiple input fields with code something like:

JTextField firstName = new JTextField(10);
//firstName.addAncestorListener( new RequestFocusListener() );
JTextField lastName = new JTextField(10);
Object[] msg = {"First Name:", firstName, "Last Name:", lastName};


int result = JOptionPane.showConfirmDialog(
    frame,
    msg,
    "Enter Name",
    JOptionPane.OK_CANCEL_OPTION,
    JOptionPane.PLAIN_MESSAGE);

if (result == JOptionPane.YES_OPTION)
{
    System.out.println(firstName.getText() + " : " + lastName.getText());
}
else
{
    System.out.println("Canceled");
}

One problem with the above is that focus will be on the buttons, not the text field. You can fix this problem by using the Request Focus Listener

If you don't like the layout of the components in the option pane then you will need to create a custom panel with your components and add the panel to the OptionPane.

You should also check out the section from the Swing tutorial on Making Dialogs. This section and the tutorial is general will give you Swing basics as your transition from C#.

Reil
  • 3
  • 2
camickr
  • 321,443
  • 19
  • 166
  • 288