1

The content in the JFrame WaitingFrame doesn't show up as expected. This is in essence what I am trying to do:

package org.brbcoffee.missinggui;

import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Main {
    public static KeynoteServer server;
    public static void main(String[] args){
        JFrame frame = new JFrame("SSCCE");
        JButton btn = new JButton("Test");
        btn.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent arg0) {
                connectToPhone();
            }
        });
        frame.add(btn);
        frame.pack();
        frame.setVisible(true);
    }
    public static void connectToPhone(){
        WaitingFrame wf = new WaitingFrame();
        wf.setVisible(true);
        server = new KeynoteServer();
        if (server.setup()){
            System.out.println("Server set up and connected");
        } else {
            System.out.println("Couldn't connect");
        }
        wf.dispose();
    }
}   

@SuppressWarnings("serial")
class WaitingFrame extends JFrame {
    public WaitingFrame(){
        setTitle("Waiting");
        this.setLocationByPlatform(true);

        JLabel label = new JLabel("Waiting for client..."); // This will never show
        JPanel content = new JPanel();          
        content.add(label);

        this.add(content);
        pack();
    }
}
class KeynoteServer{
    private int port = 55555;
    private ServerSocket server;
    private Socket clientSocket;

    public boolean setup() {
        try {
            server = new ServerSocket(55555);
            server.setSoTimeout(10000);
            clientSocket = server.accept();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}

When setup() is called the WaitingFrame does indeed show up, but the contents are missing. I've tried different image formats, but it does work from other methods and classes, so that shouldn't matter. Does anyone know what's going on here?

BjornSnoen
  • 313
  • 3
  • 7
  • 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) 3) Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. Implement a `SwingWorker` for long running tasks. See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details. – Andrew Thompson Dec 14 '12 at 07:54
  • 1
    Thank you Andrew, I'll work on making it SSCCE and update my question. I'll also move all my projects away from using multiple JFrames, particularly the part about having several icons in my my task bar has actually been bothering me, so why I've kept doing it is beyond me. – BjornSnoen Dec 14 '12 at 08:00
  • Good edit (but still hoping to see an SSCCE). – Andrew Thompson Dec 14 '12 at 08:19
  • That's as SSCCE as I could get it while not unbreaking it. Turns out it's actually harder to break than fix now that I know what I did wrong, I was about to give up! – BjornSnoen Dec 14 '12 at 10:14

1 Answers1

2

Use SwingUtilities.invokeLater()/invokeAndWait() to show your frame because all the GUI should be updated from EDT.

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • Thank you! Using this correctly (at least I hope I'm using it correctly) solved my problem. I've been up all night scratching my head and tearing out my hair over this, now I can finally go to sleep thanks to you. If only I had enough reputation to vote you up... – BjornSnoen Dec 14 '12 at 08:48