tags:

views:

67

answers:

4

I've only just begun writing GUI programs, this being my second one. With both projects (both homework assignments) I have had the same issue. The GUI objects (such as JTextField) do not show when the application runs until after I resize the window or move keyboard focus to them. If I do not do one of those two things, then I'll just have an empty application window.

Any ideas why this is happening and what I could do to solve it? I'm working on Mac OS 10.6.1.

My code is below. Feel free to comment on my coding style, but please focus on the problem I'm having.

import javax.swing.*;
import java.awt.*;

public class ToDo extends JFrame {

    private int height = 30,
                width = 300;

    public ToDo() {
        this.setSize(400,400);
        this.setVisible(true);
        this.setLayout(null);
        this.setResizable(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("To Do List");
        JTextField todoItem[] = new JTextField [10];
        Container contentpane = this.getContentPane();
        contentpane.setLayout(null);
        for(int i=0; i<10; i++) {
            todoItem[i] = new JTextField();
            todoItem[i].setBounds(10,(height*(i)+10),width,height);
            contentpane.add(todoItem[i]);
        }

    }

    public static void main(String[] args) {
        new ToDo();
    }
}
A: 

A guess: add the component before setBoundsing it.

Jonathan Feinberg
A: 

I could be wrong--been a long time since I've done a GUI in java--but I'm guessing your issue is making the JFrame visible before you finish adding elements. I think you need to either do that afterwards, or call update on the frame.

EDIT - Also, not sure setting the layout to null is a good idea. I've always used GridBag, but it might lose its default if you set it to null.

DougW
setting layout to null is not a problem if you just want a static layout (which doesn't respond at all to resizing the window)
ammoQ
+2  A: 

There are some rules about how you should never touch Swing objects from outside the Swing thread once they've been realized. I always ignore these rules, but it could well be you've been bitten by them under Mac OS.

As a step in the officially right direction, try to not do setVisible() until you've assembled the whole thing, i.e. at the bottom of the constructor.

Reference material: http://www.math.vu.nl/~eliens/documents/java/tutorial/ui/swing/threads.html

Carl Smotricz
Really, really DON'T ignore those rules--each newer version of the JRE seems to rely more on kosher threading.
Michael Brewer-Davis
Definitely. Do as I say, don't do as I do!But the document I just quoted seems to indicate that the way I do it is OK after all. Seriously, I think if a change in the JRE were to break that (i.e. constructing the GUI, then setVisible() in the main thread) then there would be an uproar of broken programs worldwide - Sun would not survive such a change :)
Carl Smotricz
We haven't talked about threads yet, so thank you for providing that link. It's very helpful.
DiegoFuego
+3  A: 

You have to add the elements before the component is made visible.

Put this as your last line:

        this.setVisible(true);

alt text

This is not OSX related, it happens in Windows also.

OscarRyz