wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Best approach for CardLayout 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 "Best approach for CardLayout" Watch "Best approach for CardLayout" New topic
Author

Best approach for CardLayout

jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
I'm writing an application that uses CardLayout. The first card shows 3 buttons, Edit Add and Finish. The Edit and Add cards have the same layout except for one place. Edit has a drop JComboBox showing current values for the application, Add has a JTextField that allows you to add a new value and submit it. Otherwise, the two cards are identical.
I've played around with using inner classes (super and subclasses), duplicating all of the fields for each card, and using various types of logic to switch between the combobox and the textfield, but I either get some pretty funky results, or the code becomes so garbled as to be unreadable. Writing three separate classes would be nice, but since the application pulls values from all the fields, I haven't figured a way to do that yet (without passing an extremely lengthy list of parameters or adding extra objects to the application to get/set the values for the fields).
Anyone have an idea of how to approach this?
Thanks in advance,
Jason
Vinod Venugopal
Ranch Hand

Joined: Dec 06, 2000
Posts: 148
Hi,
I tried using card layout for a GUI which had different components one different cards, so what I did was created different classes for different components ( for eg. RadioButtonComponent, ButtonComponent classes etc. The classes were generic that it had methods to set / get values & error checking based on component type. All the cards info was put in XML, so I used to read the XML first & then form the cards. I had one class for the main Panel & used to add the different component based class objects to it.
When I clicked "Next" my action listeners of the component class would take care of values & error checking, same time I would grab values from the components & store them.
Also I used to keep a collection of the components in a vector, that would help in keeping track.
Hope u get the logic.
Vinod


- Vinod<br />-------<br />SCJP2
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Jason,
Have you tried making a JPanel class and depending on which type you have either place the combo or text field.
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Ok, I think I'm almost there. Problem is now with the buttons. If I have two panels, one for card2 and one for card3, and I have a panel called buttonPanel that holds two buttons that are used for both Card2 and Card3, can I not do a card2.add( buttonPanel ) and card3.add( buttonPanel)? I have this coded, but the buttons only show up on card3.
Is there a way to have the buttons show up for both cards? I guess I could create a panel that holds two unique buttons for card2, and a whole new set of panel/buttons for card3. Is that the best approach?
Jason
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Are you creating 2 separate instances or buttonPanel. You need to.
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
nope, was doing just one and adding that instance to each panel. I take it that means I'll need to create two instances of the buttons, also, to stick on the buttonPanel?
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Doesn't your button panel create its own buttons? If not, yes you need those as well. If you don't all you are doing by adding them to multiple components is move them.
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
I don't have a separate class for my button panel, if that is what you mean...? I finally worked it so that I do
buttonPanel = new JPanel();
cancel = new JButton( "Cancel" );
submit = new JButton( "Submit" );
//add action listeners, layout managers, etc.
buttonPanel.add( cancel );
buttonPanel.add( submit );
card2.add( buttonPanel );
buttonPanel = new JPanel();
cancel = new JButton( "Cancel" );
submit = new JButton( "Submit" );
//add action listeners, layout managers, etc.
buttonPanel.add( cancel );
buttonPanel.add( submit );
card3.add( buttonPanel );
Don't know if that is the best way, but it works for now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Best approach for CardLayout