1

I've created Dialog using awt and swing. In this form I hafe JTextField and okButton which is JButton. How to make the Dialog window hide on clicking okButton? Here is my class's code:

public class QueueDialog extends JFrame implements ActionListener {
  private static final long SerialVersionUID = 1L;
  private static JTextField field = new JTextField(15);
  private Sender sender;
  private String incommingMessagesFolderUrl = "/etc/dlp/templates";

  public QueueDialog() throws Exception {

    sender = new Sender();

    // field.setSize(60, 15);
    JButton okButton = new JButton("ok");
    final JLabel label = new JLabel("Enter the name of queue:");
    GridBagLayout gbag = new GridBagLayout();
    GridBagConstraints gbc = new GridBagConstraints();
    setLayout(gbag);

    gbc.insets = new Insets(2, 0, 2, 0);
    gbc.gridy = 0;
    gbc.gridx = 0;
    gbag.setConstraints(label, gbc);
    gbc.gridy = 1;
    gbc.gridx = 0;
    gbag.setConstraints(field, gbc);
    gbc.gridy = 2;
    gbc.gridx = 0;
    gbag.setConstraints(okButton, gbc);

    add(okButton);
    add(field);
    add(label);
    setTitle("Queue name");
    setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    setSize(400, 200);
    setLocationRelativeTo(null);
    setVisible(true);

    okButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("ok")) {
          // label.setText(field.getText());
          send(field.getText());
        }
      }
    });
  }
}
JREN
  • 3,572
  • 3
  • 27
  • 45
Nikitin Mikhail
  • 2,983
  • 9
  • 42
  • 67
  • 2
    In action listener write this.setVisible(false) :P – Sanyam Goel Jul 08 '13 at 11:56
  • 2
    Anyways it is never a good idea to call overridden method from inside a constructor so please create a different method for the same – Sanyam Goel Jul 08 '13 at 11:58
  • 3
    1) Don't extend frame or other top level containers. Instead create & use an instance of one. 2) Don't set the size of top level containers. Instead layout the content & call `pack()`. 3) That is not a dialog, but a frame. If you want a dialog, use a `JDialog`! – Andrew Thompson Jul 08 '13 at 11:59
  • [`WindowEvent.WINDOW_CLOSING`](http://stackoverflow.com/a/6310284/230513)? – trashgod Jul 08 '13 at 17:26
  • 1
    Add @trashgod (or whoever - the `@` is important) to notify them of a new comment. Since you seem to be ignoring my advice, good luck with it.. – Andrew Thompson Jul 09 '13 at 06:22
  • @trashgod It is no acceptable as it is an event but not the action. As for the first advice it is not useful too because `this.set...` doesn't give any variants. I mean there is no such method in `this` as `setVisible()` – Nikitin Mikhail Jul 09 '13 at 07:21
  • Why not a `JDialog`, as @Andrew suggests, or `JOptionPane`? – trashgod Jul 09 '13 at 07:53
  • @trashgod The solution I suggested is working absolutely fine I have tested it and then posted it :) :) :) – Sanyam Goel Jul 11 '13 at 09:06
  • @NikitinMikhail please checkt it once I donno y it didn work for you :O – Sanyam Goel Jul 11 '13 at 09:10
  • what happened when you typed: _[swing] close dialog button_ into the search field at the trailing upper edge of this or any other page of the site? Voting to close as duplicate – kleopatra Jul 11 '13 at 09:56

1 Answers1

1

As I mentioned in my comment (i.e, the very first comment) setVisible(false) is working absolutely fine on click of Ok button

Please Check this code again Where I have addd the statement in action listener. Just now did it to prove the solution is correct> I was not following this post hoping you must have got your answer in the comment itself

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class QueueDialog extends JFrame implements ActionListener {
  private static final long SerialVersionUID = 1L;
  private static JTextField field = new JTextField(15);
  //private Sender sender;
  private String incommingMessagesFolderUrl = "/etc/dlp/templates";

  public QueueDialog() throws Exception {

   // sender = new Sender();

    // field.setSize(60, 15);
    JButton okButton = new JButton("ok");
    final JLabel label = new JLabel("Enter the name of queue:");
    GridBagLayout gbag = new GridBagLayout();
    GridBagConstraints gbc = new GridBagConstraints();
    setLayout(gbag);

   // gbc.insets = new Insets(2, 0, 2, 0);
    gbc.gridy = 0;
    gbc.gridx = 0;
    gbag.setConstraints(label, gbc);
    gbc.gridy = 1;
    gbc.gridx = 0;
    gbag.setConstraints(field, gbc);
    gbc.gridy = 2;
    gbc.gridx = 0;
    gbag.setConstraints(okButton, gbc);

    add(okButton);
    add(field);
    add(label);
    setTitle("Queue name");
    setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    setSize(400, 200);
    setLocationRelativeTo(null);
    setVisible(true);

    okButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {

        if (e.getActionCommand().equals("ok")) {
            System.out.println("Hello");
            setVisible(false);


          // label.setText(field.getText());
         // send(field.getText());
        }
      }
    });
  }

@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub

}



public static void main(String[] args) throws Exception {
    QueueDialog diag = new QueueDialog();
}
}
Sanyam Goel
  • 2,138
  • 22
  • 40