tags:

views:

130

answers:

4

I am developing a desktop java application with GUI implemented through swings. I have made a JFrame and have added three buttons on it - Add, Edit, Delete.

Now I want that whenever a user clicks on any of the button, the content specific to that button appears besides those three buttons.

So how to implement this. Should I need to add a JPanel besides those three buttons and then add the content specific to the button to that JPanel.

So far, I have taken a JFrame and have added 3 buttons on it. That's it.

For Add button, I want to add some buttons and textfields to add info to the database.

For Delete button, I want to add some buttons to find records in the database based on the info entered through the user in the textfield that appears when the user clicks on the Delete button.

Similar type of content for Edit button.

A: 

I haven't done a lot of Java programming, but I think using 2-3 different JPanel, and make visible the one you need depending on the button that was clicked would do the trick.

I'm not sure if this is the right approach though.

Alex
+2  A: 

So how to implement this. Should I need to add a JPanel besides those three buttons and then add the content specific to the button to that JPane

That would be fine. When you push the button, you can call JPanel.removeAll() to remove all the controls currently in the control, and then just do the layout again, specific to whatever button you pushed.

If you have custom swing controls, just add your custom control the JPanel using a BorderLayout and putting in the center.

Another option would be to use a CardLayout, and flipping between the cards when a user presses one of the buttons. If the layouts for the buttons never change, that would probably be a better way to do it. Obviously if the content changes between button presses, you'll need to redo the layout each time.

Chad Okere
CardLayout is a good approach here.
Jeff Storey
+1  A: 

Either of Chad's or Alex's answers would be fine. You will probably need to call a combination of revalidate() and repaint() on the panel that you've changed, as in the past I've noticed Swing doesn't always like panels being swapped out.

Also, have you considered using a JTabbedPane instead of manually coding the interaction with the add/edit/delete buttons?

Ash
CardLayout is the correct answer - no revalidate()s or repaint()s needed if you choose this.
Nate
In CardLayout don't you always "flick" between cards rather than reference them by ID? In that case, with only 3 cards it's probably best, but what if you've got 10 or 20 to flick through? Maybe not a general case solution.
Ash
Sorry, just read the CardLayout API properly - the constraints object can be used for "fast random access". So, yeah, CardLayout wins I guess.
Ash
A: 

i was using jframe to add all buttons and make new jframe for new window and hide previous one. gven way are better . will do that now.

nicky