Switch from Jframe to Jdialog

Asked

Viewed 909 times

1

I created a frame of this genre:

inserir a descrição da imagem aqui

But, since it was to present that the client was having trouble connecting to a server, it was better to move to Jdialog

Problem: I can’t make Jdialog visible

Code:

public class WaitingJoption extends JDialog { //se for JFrame funciona direito

    public WaitingJoption() {

        initUI();
    }

    private void initUI() {

        setTitle("Agurde pff...");
        setUndecorated(true);
        JPanel pane= new JPanel();
        JPanel pane1= new JPanel();

        pane.setLayout(new GridBagLayout());
        pane1 = new Surface();

        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.VERTICAL;
        c.ipady = 80;      //make this component tall
        c.ipadx = 80;
        c.weightx = 0.0;
        c.gridwidth = 3;
        c.gridx = 0;
        c.gridy = 0;
        pane.add(pane1, c);


        JLabel labe = new JLabel("O cliente está a tentar connectar com o servidor...");
        c.fill = GridBagConstraints.VERTICAL;
        c.ipady = 40;      //make this component tall
        c.weightx = 0.0;
        c.gridwidth = 3;
        c.gridx = 0;
        c.gridy = 1;
        pane.add(labe, c);


        getContentPane().add(pane);
        setSize(300, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                WaitingJoption wt = new WaitingJoption();
                wt.setVisible(true);
            }
        });
    }
}
  • The best would be to use a Jdialog for this and avoid having multiple Jframe in your program. By the way, with Jdialog you can make the modal component, preventing the user from trying to access another functionality of the program while trying to reconnect.

  • I have tried to implement this idea but I could not get it to work... There is no error but also I can not make it work, I will post code...

  • I tested here and launched an Exception on line 50. You are using the constant EXIT_ON_CLOSE (which is for Jframe components) in a Jdialog. Try switching to setDefaultCloseOperation(DISPOSE_ON_CLOSE);

  • 1

    That’s right, I don’t know why in mine you don’t make that exception, you create an answer with that so you can mark it as right

1 answer

2


In the code in question a IllegalArgumentException on line 50.

This is because you are using an illegal constant for a JDialog in the following passage:

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

The constants that may be used in that method for JDialog sane: DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE, or DISPOSE_ON_CLOSE. Change to:

setDefaultCloseOperation(DISPOSE_ON_CLOSE);

Reference: Windowconstants

Browser other questions tagged

You are not signed in. Login or sign up in order to post.