Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

GUI codelines ordering issues  RSS feed

 
fady zohdy
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello everyone,
i am studying java from head first java book and yesterday i was working on a program called QuizCardBuilder. what i noticed is that when i changed the order of the lines from the book i get a blank frame with nothing on it.
can anybody please explains why this happens?
this is my code that gives blank frame
 
Rob Camick
Ranch Hand
Posts: 2753
11
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The above line is the important line.

By default all Swing components have a size and location of (0, 0). Since the size is 0, there is nothing to paint.

When you invoke the setVisible() method, the layout manager is invoked and the layout manager is responsible for setting the size/location of a component. So you need to invoke this method AFTER all the components have been added to the frame, so the components can be assigned a proper size/location.

Actually you really should be using:



Then the frame will be sized at the preferred size of all the components, rather than just guessing what the size should be.



That is not the way to use constraints when adding a component to the panel. Instead you should be using:



In JDK 5 and above you don't even need to use the getContentPane() method, you can add the component to the frame and the frame is smart enough to add the component to the content pane.
 
fady zohdy
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:

The above line is the important line.

By default all Swing components have a size and location of (0, 0). Since the size is 0, there is nothing to paint.

When you invoke the setVisible() method, the layout manager is invoked and the layout manager is responsible for setting the size/location of a component. So you need to invoke this method AFTER all the components have been added to the frame, so the components can be assigned a proper size/location.

Actually you really should be using:



Then the frame will be sized at the preferred size of all the components, rather than just guessing what the size should be.



That is not the way to use constraints when adding a component to the panel. Instead you should be using:



In JDK 5 and above you don't even need to use the getContentPane() method, you can add the component to the frame and the frame is smart enough to add the component to the content pane.


thamk you so much. you just made my life so much easier
head first book is very old . what should i do after finishing it to catch up with all these changes?!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!