Calvin Calvin - 1 year ago 69
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() {

Container cp = getContentPane();
cp.setLayout(new FlowLayout());

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

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

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

ServerAddressField = new JTextField();
ServerAddressField.setBounds(107, 414, 130, 26);

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

PortNumberField = new JTextField();
PortNumberField.setBounds(342, 414, 130, 26);

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

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

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

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

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

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

ChatServer=new server();

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


UserText.addKeyListener(new KeyListener(){

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


public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER){

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 Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download