Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple controls in a panel

 
John Lockheart
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a seperate class which extends JPanel. It will occupy the center of a frame. I want add multiple controls to it, but i'm not sure how.



The four buttons will take up the entire West area. The labels stacked on top of eachother beside the TextFields/CheckBox (which are also stacked on eachother) in the Center area. Then the Label on top of the ListBox (which takes up the rest left over space) in the East area.

Any ideas? Below is my class with the code I implemented so far. Any help would be great.



[ March 20, 2008: Message edited by: John Lockheart ]
[ March 20, 2008: Message edited by: John Lockheart ]
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you need to do is learn to nest JPanels. For instance you could have a mainPanel that uses the BorderLayout, and this could hold a westPanel that uses BoxLayout and an eastPanel that uses a GridLayout, and a northPanel that uses FlowLayout, etc... and these panels could have still other JPanels nested in them.

So my two recommendations are:
1) nest multiple JPanels / components when needed, each with their own layout, and
2) learn more about layouts by reading about them in the Sun Java Swing tutorials. Look here
 
John Lockheart
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand that, but How do I go about implementing action listeners for all the panels I create in a class. I need to know when buttons are clicked on those panels. How can I retrieve a button click event from a newly created panel and process it in my main class?
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by John Lockheart:
I understand that, but How do I go about implementing action listeners for all the panels I create in a class. I need to know when buttons are clicked on those panels. How can I retrieve a button click event from a newly created panel and process it in my main class?


I wasn't sure that you did understand as you appeared to be adding many of the controls on top of each other. But perhaps I was wrong.

Here's a silly example that does nothing but places components in a JPanel that's placed in a JFrame. The button actions are handled by a private inner class called ButtonListener that implements the ActionListener interface:


[ March 20, 2008: Message edited by: pete stein ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic