Calvin Calvin - 10 days ago 4
Java Question

Application closes immediately after opening

so I used WindowBuilder on eclipse to design my GUI. After doing so, i tried testing it out to see if the GUI pops up correctly, but for some reason, the application closes almost immediately after running it.

Here is my code:

public class ServerFrame extends JFrame {

private JTextField ServerAddressField;
private JTextField PortNumberField;
private server ChatServer;
private InetAddress ServerAddress ;
private JTextArea ChatBox;
private JTextArea ClientTextArea;
private JTextArea UserText;
private JButton Start, Send;

/**
* Create the application.
*/

/**
* Initialize the contents of the frame.
*/
public ServerFrame() {

setTitle("Server");
setSize(700,700);
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.setVisible(true);

ChatBox = new JTextArea();
ChatBox.setBounds(20, 30, 497, 259);
cp.add(ChatBox);

ClientTextArea = new JTextArea();
ClientTextArea.setBounds(549, 30, 128, 259);
cp.add(ClientTextArea);

UserText = new JTextArea();
UserText.setBounds(20, 317, 497, 57);
cp.add(UserText);

ServerAddressField = new JTextField();
ServerAddressField.setBounds(107, 414, 130, 26);
cp.add(ServerAddressField);
ServerAddressField.setColumns(10);

JLabel lblNewLabel = new JLabel("Server Address:");
lblNewLabel.setBounds(6, 417, 110, 21);
cp.add(lblNewLabel);

PortNumberField = new JTextField();
PortNumberField.setBounds(342, 414, 130, 26);
cp.add(PortNumberField);
PortNumberField.setColumns(10);

JLabel lblPortNumber = new JLabel("Port Number:");
lblPortNumber.setBounds(249, 417, 116, 21);
cp.add(lblPortNumber);

Start = new JButton("Connect");
Start.setBounds(482, 414, 117, 29);
cp.add(Start);

Send = new JButton("Send");
Send.setBounds(529, 332, 117, 29);
cp.add(Send);

JLabel lblChatHistory = new JLabel("Chat History");
lblChatHistory.setBounds(231, 6, 92, 16);
cp.add(lblChatHistory);

JLabel lblClientList = new JLabel("Client List");
lblClientList.setBounds(579, 8, 75, 12);
cp.add(lblClientList);


Start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

ChatServer=new server();
ChatServer.start();

}
});
Send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// ChatServer.SendMassage(ServerAddress.getHostName()+" < Server > "+UserText.getText());
UserText.setText("");

}
});

UserText.addKeyListener(new KeyListener(){

@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub

}

@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER){
e.consume();
Send.doClick();
}
}

@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub

}

});


}

public static void main(String[] args) {
// TODO code application logic here
new ServerFrame();
}


Any help would be appreciated. Thank you very much!

Answer

You never call setVisible(true) on a top level window, the ServerFrame.this JFrame, and so the event thread is never really started.

After adding all your components to the ServerFrame, simply call setVisible(true) on it. ..... or in the main method, do:

new ServerFrame().setVisible(true);

Another issue: While null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.

Also, be sure to start your GUI on the Swing event thread, otherwise you risk running into occasional and hard to debug threading errors. e.g.,

public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> {
        new ServerFrame().setVisible(true);
    });
}