views:

513

answers:

3

Server is a class I made that extends JFrame.

    Server serverApp = new Server(TITLE, WIDTH, HEIGHT, true, false);

I've effectively removed almost all the other code but the problem still remains!

    c = getContentPane();
    c.setLayout(new BorderLayout());

    //Components  /***AHHHHH***/
    lblEnterMessage = new JLabel("Enter Message ");
    txtEnterMessage = new JTextField(50);
    txtEnterMessage.addActionListener(this);
    btnSend = new JButton("Send");
    btnSend.addActionListener(this);
    taDisplay = new JTextArea("Test, test test.", 10, 0);
    taDisplay.setEditable(false);
    JScrollPane jspDisplay = new JScrollPane(taDisplay);

    pnlChatTop = new JPanel(new FlowLayout());
    pnlChatTop.add(lblEnterMessage);
    pnlChatTop.add(txtEnterMessage);
    pnlChatTop.add(btnSend);
    pnlChat = new JPanel(new BorderLayout());
    pnlChat.add(pnlChatTop, BorderLayout.CENTER);
    pnlChat.add(jspDisplay, BorderLayout.SOUTH);

    c.add(pnlChat, BorderLayout.CENTER);

Oh dang, it just suddenly WORKED... And I was about to remove this question but I ran it again a few times it and just randomly WORKS and doesn't WORK at times.

I've just remembered having this problem before with other 'projects' and my solution was to make the window resizable. Whenever I simply resized it, the components would display.

This time, I'm making a game and I don't want it to be resizable... and I wanna know how to fix this problem for good and in the proper way anyway.

Help! Does anyone know why this is happening?

Thanks.

Edit:

public Server(String title, int sizeW, int sizeH, boolean visibility, boolean resizability) {

    /* Initialization */
    //JFrame settings
    setTitle(title);
    setSize(sizeW, sizeH);
    setVisible(visibility);
    setResizable(resizability);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    addKeyListener(this);

Will that help?

+2  A: 

The problem is not apparent from the code you have provided.

It sounds like you want some combination of the pack(), setSize(int,int), setExtendedState(int) and/or setResizable(boolean) methods prior to calling setVisible(true).


Edit:

setTitle(title);
setSize(sizeW, sizeH);
setVisible(visibility);
setResizable(resizability);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

There is a race condition in this code. Sometimes the main thread will get the components into the correct state to be painted before the frame displays; sometimes the frame wins and starts painting before everything is ready.

The thing about using Swing is that you are automatically working with multi-threaded code. Although it is generally safe to initialize controls on the main thread, once you cause the event dispatch thread to start (as setVisible(true) will surely do), all bets are off.

Delay calling setVisible(true) as long as possible. Preferably, don't call it from within your JFrame constructor.

If you need to modify Swing controls after you've kicked off your application, you'll need to do it via the event dispatch thread (see the invokeLater and invokeAndWait methods in SwingUtilities, among others).

McDowell
So do I set the RESIZABILITY first? Then Visibility? Could that... for some reason be the problem?Ok I just tried pack(), setSize is already there, setExtendedState(JFrame.ALL_DONT_WORK) and setResizable(false) all before calling setVisible(true) and it doesn't work.I'd also like to ultra-double-triple-quadruple note that this has happened to my other applications and I setResizable(false) etc. to make it displayable... And I remember my lecturer saying she had the same problem before too and she just resizes it to fix it as well.
Dois
@Dois - have a look at concurrency in the Swing tutorial - http://java.sun.com/docs/books/tutorial/uiswing/
McDowell
calling the JFrame (my Server class) in invokeLater seems to work. Thanks A TON guys, saved a lot of my time and cured my headache.
Dois
+2  A: 

Intermittent failures of this sort suggest synchronization problems. Be sure you build and run your GUI on the EDT. In addition, you might like to see this very simple, ~100 line, GUI chat program.

trashgod
Thanks, that was it.
Dois
+2  A: 

The call to setVisible is too early. It runs immediately and paints the window at the time that it is called. If you have not added all components to the Frame then they are not painted. That is why resizing the frame seems to make it appear. Because resizing causes a repaint to execute.

Make setVisible the last call in your JFrame's constructor.

Vincent Ramdhanie
Actually, initially when I was having the problem, I thought that could be the issue but I actually had a loop running 60 times a second calling repaint for my container so I ruled that out.Maybe my memory is shrouded or something but anyway, the SwingUtilities.invokeLater seems to be working perfectly. Thanks guys.
Dois