Why I cannot add a JPanel to JFrame?

Posted by Roman on Stack Overflow See other posts from Stack Overflow or by Roman
Published on 2010-03-22T13:27:01Z Indexed on 2010/03/22 13:31 UTC
Read the original article Hit count: 389

Filed under:
|
|
|
|

Here is the code:

import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.event.*;
import java.awt.*;

public class GameWindow {

    private String[] players;
    private JFrame frame;

    // Constructor.
    public GameWindow(String[] players) {
        this.players = players;
    }

    // Start the window in the EDT.
    public void start() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                showWindow();
                controller.start();
            }
        });
    }

    // Defines the general properties of and starts the window.
    public void showWindow() {
        frame = new JFrame("Game");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
        frame.setSize(600,400);
        frame.setVisible(true);
    }

    // The thread controlling changes of panels in the main window.
    private Thread controller = new Thread() {
        public void run() {
            frame.add(generatePartnerSelectionPanel());
            frame.invalidate();
            frame.validate();
        }
    };

    // Generate the panel for the selection of a partner.
    private JPanel generatePartnerSelectionPanel() {
        JPanel panel = new JPanel();
        panel.add(new JLabel("Pleas select a partner:"));
        return panel;
    }

}

I should see "Pleas select the partner" and I don't. Why?

I suppose that it's because I do not see frame from the run method of the Thread.

© Stack Overflow or respective owner

Related posts about java

Related posts about gui