• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with basic gui problem

 
Nuno Smith
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 176
Chrome Mac Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Nuno Smith
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nuno Smith
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11096
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please start new threads for new questions.
UseOneThreadPerQuestion
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic