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

First JPanel  RSS feed

 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my first JPanel and aside from being near the bottom of the learning curve, I cannot get even a minimal panel to display. Here is my class for the JPanel (components to be added later):


From my working JFrame class, I simply do this:

While I get no errors, I also do not get the panel displayed. What I expected was a panel on the frame that is empty and has a border around it with the appropriate title. Can someone explain what I am missing? TIA.
 
K. Tsang
Bartender
Posts: 3620
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You didn't make the frame visible. You don't really need to make the JPanel visible because you adding to the JFrame.



To start off you should have a base class which represents your JFrame. Then add JPanels to it.

 
Rob Camick
Ranch Hand
Posts: 2752
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I expected was a panel on the frame that is empty and has a border around it with the appropriate title. Can someone explain what I am missing? TIA.


You didn't show the code how you create the frame and make it visible.

We don't know what layout manager the frame is using.

The TitledBorder is not considered as part of the preferred size of the panel. So the preferred size is (0, 0). Maybe that is why is it not displayed. Try resizing the frame to see if anything happens.

If not, then the problem is probably the layout manager.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick reply. Sorry, I didn't want to over-complicate things. Here is the code segment that creates the frame:
 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you appear to be using grid bag without setting any locations in the constraints object. Find out about Cay Horstmann's GBC class.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again. That article has a broken link to the example so it is not helpful. I thought the constraints applied to the added components. At this point I don't have any yet. How do I add the constraints to the panel with no components?
 
Rob Camick
Ranch Hand
Posts: 2752
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Don't use a null layout!

You should be using a layout manager. Read the section from the Swing tutorial on Layout Managers. I suggest you start with the BorderLayout since this is the default layout manager for the content pane of the JFrame. A GridBagLayout is one of the more complicated layout managers to use. I would learn the simple ones first.

Download the demo code from the tutorial and use it as a starting point. Your code will be better structured and following Swing programming conventions.

Sorry, I didn't want to over-complicate things.


Until a problem is solved you don't know what part of the code is relevant to the problem.

Therefore, every time you post a question you should be posting a proper SSCCE. The point of a SSCCE is to post simple code directly related the problem and the code MUST compile and execute.

 
Fred Kleinschmidt
Bartender
Posts: 507
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't call frame.setVisible(true) until AFTER you have populated its content pane.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
Don't use a null layout!

You should be using a layout manager. Read the section from the Swing tutorial on Layout Managers. I suggest you start with the BorderLayout since this is the default layout manager for the content pane of the JFrame. A GridBagLayout is one of the more complicated layout managers to use. I would learn the simple ones first.

Thanks. I thought the layout applied to the JPanel I was creating not the JFrame. This is confusing to me. I need to experiment more.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Putnam wrote:This is confusing to me. I need to experiment more.


Experimentation is definitely the right way to go. I recommend starting with working code, though, and experimenting with that. Here's a link to a tutorial which includes working code: http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

EDIT: I see Rob already posted the link to that tutorial. So yeah, what he said:

Rob Camick wrote:Download the demo code from the tutorial and use it as a starting point.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I am kind of on my way. However, I don't understand the location flags. I added a menu bar using BorderLayout.START_PAGE and it did what I expected. After some experimenting I found that the JPanel I wanted to add required BorderLayout.CENTER. I don't understand why it disappears when I use BorderLayout.LINE_START (I plan to add CENTER and LINE_END components later).
 
Rob Camick
Ranch Hand
Posts: 2752
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand why it disappears when I use BorderLayout.LINE_START


I already explained that in my original answer.

Try adding a JButton to the panel and see what happens.

I added a menu bar using BorderLayout.START_PAGE


You should NOT be adding a menu bar to the content pane of the frame. Read the tutorial. There is also a section on "How to Use Menus". Again it has working code you can download and test.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, it didn't disappear, exactly, it just made it a pixel or 2 wide. I think I'm good now. I just need to add sizes. Thanks again.
 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Putnam wrote:Thanks again. That article has a broken link to the example so it is not helpful. . . .
That's a pleasure
The link I posted was all right. This is the example Horstmann was referring to. The link appears to have changed since he wrote that page.
You need to update the constraints for every component you add, and they will need to be passed to the layout (again) for every component you add. You should also set just about every attribute of the constraints object. If you don't set at least xPos, yPos, width, height, and fill you can expect things to go wrong.
But, as other people have said since I last posted, you would probably do better to use a different layout manager.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!