aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Multiple controls in a panel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Multiple controls in a panel" Watch "Multiple controls in a panel" New topic
Author

Multiple controls in a panel

John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
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

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Oct 13, 2006
Posts: 115
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

Joined: Feb 23, 2007
Posts: 1561
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 ]
 
wood burning stoves
 
subject: Multiple controls in a panel