File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JMenu Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JMenu" Watch "JMenu" New topic


Jess Ishasder

Joined: Feb 19, 2002
Posts: 21
I have created a menu. Whenever a menu item is selected, the previous displayed components will be set invisible, and the new components will be set visible.
Suppose I have a menu button called, "Add". It has two menu items---"A Book" and "A CD".
First I selected "A Book", then selected "A CD". Those displays are good. However, when I selected "A Book" again, its components display is in a mess. Why?
The following is part of my code:
addBook.addActionListener(new AddBookListener(this, c));
addCD.addActionListener(new AddCDListener(this, c));
class AddBookListener implements ActionListener
private Exer3 myApplet=null;
private Container myContainer = null;
public AddBookListener(Exer3 app, Container c){
myApplet = app;
myContainer = c;
//Define actionPerformed()
public void actionPerformed(ActionEvent e){
//Clear the pane by calling a helper method--invisibleAll()
helperClass help= new helperClass(myApplet);
/*=====Set Invisible======*/
/*=====Set visible========*/
//Design the layout of the pane
//Ensures that the Container has a valid layout
(AddCDListener did the same thing)
}//End of actionPerformed()
class helperClass{
Exer3 myApplet= null;
public helperClass(Exer3 app)
myApplet = app;
/*===========Set Visible====================*/
public void setVisibleForAddBook()
public void setVisibleForAddCD()
public void setInvisibleForAddBook()
public void setInvisibleForAddCD()
Please help me to solve the problem.
Thank you for your help!
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Jess,
If the same components are getting set visible/invisible over and over again you really should be using a CardLayout with the components required for one menu item on one panel and the others on another panel. Then you only need to set the front card and it won't matter how many components are on each card, etc.
I agree. Here's the link:
subject: JMenu
It's not a secret anymore!