File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Help with basic gui problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Help with basic gui problem" Watch "Help with basic gui problem" New topic
Author

Help with basic gui problem

Nuno Smith
Greenhorn

Joined: Apr 25, 2010
Posts: 3
Hi all. I have recently started out Java and have been experimenting with Swing. However, I have come across this problem I can't figure out.

The code below gives different results in every run. Sometimes gives 3 text fields, others 4, 5, 6, etc.. It's almost random. Why does this happen?

I know I could use the editor GUI for this, but since I will have to generate 81 text fields I think it's best to learn how to do it programatically.



Any help is greatly appreciated.
Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 176

Hello Nuno Psantos,

Try to get the good habbit on placing frame.setVisible(true) at the end of the building method. Nothing was appearing for me, I had to add setVisible at the end just to see something.

Does that fix the problem?


Olly
Nuno Smith
Greenhorn

Joined: Apr 25, 2010
Posts: 3
Olivier Legat, thanks for your reply. That fixed the problem.

I still have some questions I would like you to answer me, if possible:

1. Why should setVisible be called at the end of the building method? Why did this fixed the problem?
2. Is it possible to make a non re-sizable frame? How?
3. I read in some official java sun tutorials, that generally, instead of calling setSize one should call pack(). However, when I do this I don't even see any text fields? Why?
4. Is it good practice to add the components to the its parent component and only then add the parent to the frame? Or can I do it in whatever order I want?
5. I have now uncommented the code that creates and adds the buttons to the frame, but, now, they don't appear in the screen. What am I doing wrong?

Thanks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

Nuno Smith wrote:I know I could use the editor GUI for this, but since I will have to generate 81 text fields I think it's best to learn how to do it programatically.



Nuno Smith wrote:1. Why should setVisible be called at the end of the building method? Why did this fixed the problem?

For changes to the component tree (like adding or removing components) after the frame has been invisible the frame needs to be notified of these changes. Unfortunately, calling any of the add or remove methods will not do that. In Swing a call to "revalidate()" on the JComponent (often JPanel) to which a component was added or from which a component was removed will take care of this notification. I've checked in the source of JFrame and its ancestor classes to see what "setVisible(true)" does, and in the end it calls "invalidate()". That causes automatic revalidation, and therefore the notification.

2. Is it possible to make a non re-sizable frame? How?

By calling "setResizable(false)" perhaps?

3. I read in some official java sun tutorials, that generally, instead of calling setSize one should call pack(). However, when I do this I don't even see any text fields? Why?

pack() sets the size to be the preferred size. This requires a container to have children; otherwise the preferred size will be 0x0 (unless specified otherwise by the layout manager or after having called setPreferredSize)

4. Is it good practice to add the components to the its parent component and only then add the parent to the frame? Or can I do it in whatever order I want?

You can add components in any order you want, as long as you call setVisible(true), pack() and/or revalidate() after the entire component tree is ready.

5. I have now uncommented the code that creates and adds the buttons to the frame, but, now, they don't appear in the screen. What am I doing wrong?

A JFrame's content pane uses a BorderLayout by default. That means that calling add without specifying any constraints adds the component as the center component. You successfully added the button panel. After that however, you overwrite it with the other panel.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Nuno Smith
Greenhorn

Joined: Apr 25, 2010
Posts: 3
Thanks for your replies . I will leave this topic open, as I will certainly have more Swing questions while coding the Sudoku.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

Please start new threads for new questions.
UseOneThreadPerQuestion


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with basic gui problem
 
Similar Threads
Reading file in and sperating it out :URGENT
How do I get my button to work?
Need help
Why can't I display a JPanel?
unable to remove spaces between buttons(making a calculator)