Jeremy G
Jeremy G

Reputation: 568

Non-modal JDialog not displaying content?

I am building a server program that receives a socket connection from a client. When the program initiates the server, I would like to display a dialog that displays "Waiting for connection..." as it waits. Once the connection is received, I would like to programatically close the window. As I do not want to block the execution of the program as it waits for the socket connection, I have used a non-modal dialog to display the message. This works, except that the dialog does not display the text that I would like it to. The dialog title displays fine, but the message pane does not. Why is this? I have tried several different ways to accomplish this, including the code below, all to no avail.

public class AboutDialog extends JDialog implements ActionListener 
{
    public AboutDialog(JFrame parent, String title, String message) 
    {
        super(parent, title, false);

        if (parent != null) 
        {
            Dimension parentSize = parent.getSize(); 
            Point p = parent.getLocation(); 
            setLocation(p.x + parentSize.width / 4, p.y + parentSize.height / 4);
        }

        JPanel messagePane = new JPanel();
        messagePane.add(new JLabel(message));
        getContentPane().add(messagePane);

        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        pack(); 
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) 
    {
        setVisible(false); 
        dispose(); 
    }
}

Just in case my explanation isn't clear, I pass "Waiting for connection..." into the AboutDialog constructor as the message parameter. Thanks for any guidance!

Upvotes: 1

Views: 745

Answers (1)

JB Nizet
JB Nizet

Reputation: 691635

You're probably not opening (and closing) your dialog in the event dispatch thread (using SwingUtilities.invokeLater() from the main/listening thread).

That said, having a GUI for a server application is probably not a good idea anyway. Server apps frequently run on headless servers, and are frequently started as services/daemons. Using a log file is probably a better idea than using a GUI.

Upvotes: 2

Related Questions