Need help selecting from multiple panels
I am using MyEclipse 5.5 with the Matisse4MyEclipse addon to build a Swing application for Windows. I am an experienced J2EE programmer but only have minor experience with Win programming a few years ago using Visual Age for Java. So I am fumbling my way through making this happen. Generally I will Google the task I am trying to perform and usually find an example that will get me through.
However, I am at a point where I don't understand what I need to do.
I have set up an application. The main class inherits from JFrame. I built a Menu Bar with about 15 options. Each option has a specific panel associated with it. For each unique panel I have created a separate class that inherits from JPanel.
Back in the main class I have a a JPanel inserted in the "Center" of the JFrame. I called this JPanel appPanel and set it's layout to CardLayout. I then assigned all my JPanel classes to the Custom Pallete and proceeded to add them to the appPanel. Here is a snippet of the generated code:
appPanel =new javax.swing.JPanel();
GenerateCommandsPanel generateCommandsPanel =new GenerateCommandsPanel();
GenerateListPanel generateListPanel =new GenerateListPanel()
ImportFilesPanel importFilesPanel =new ImportFilesPanel()
OrganizeFilesPanel organizeFilesPanel =new OrganizeFilesPanel()
appPanel.setLayout(new java.awt.CardLayout());
appPanel.add(generateDownloadCommandsPanel,"card2");
appPanel.add(generateListPanel,"card3");
appPanel.add(importFilesPanel,"card4");
appPanel.add(introPanel,"card5");
appPanel.add(organizeFilesPanel,"card6");
In the application JFrame class I set up actionPerformed events for each menu item. I then created a managePanels(x) method which is called from each of the menu events. Each menu event calls the managePanels method with a unique identifier for that menu item. In the managePanels method it determines which (if any) panel is currently being displayed and set it to visible(false), i.e. importFilesPanel.setVisible(false). Then it determines which panel should now be displayed and calls visible(true) for that panel, i.e. introPanel.setVisible(true).
When I execute the code it appears to bring up the correct panel but very little of the contents of the panel are displayed. And then only input fields are shown. If I run the mouse over the panel any input fields in that panel will become visible as the mouse pointer moves directly over it. However, none of the text fields ever become visible.
I feel like I am just about there but I can't figure out how to cause the entire panel to display. I've tried calling repaint() after setting visible(true) with no change.
What do I need to do to cause the entire panel to display properly?
Thanks in advance.
Message was edited by:
GPuckett
Message was edited by:
GPuckett
Message was edited by:
GPuckett
[3291 byte] By [
GPucketta] at [2007-11-27 6:55:17]

# 1
I tried another option.. I added the following to the managePanels method:java.awt.CardLayout layout = (java.awt.CardLayout)appPanel.getLayout();layout.show(appPanel, "card5");The results were the same as above.
# 2
> I built a Menu Bar with about 15 options
Why did you start to write an application with 15 options. Wouldn't it be better to test you design with 1 or 2 first to make sure it works and then scale up your application?
Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html]How to Use Card Layout[/url]. It has a working example. The example uses a combo box to switch panels, but the code would be similiar for selection by a menu item.
If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.
# 3
Thanks for your reply.
First of all I DID start off with a little module where I created a couple of panels with separate background colors and was able to build a menu with an option that did EXACTLY what I wanted to do, change panels when I selected the right menu options. The code I am testing now does exactly the same thing as that code did, control wise. Except I am trying to show a panel with actual fields rather than pretty colors. I really didn't think it worthwhile to state that I had already gone through that exercise.
In the example you highlighted it shows using the LayoutManager basically to control which panel gets displayed through the show method. In my second post I stated that I tried that. It didn't change the result. I will look at it further tomorrow. But I question that it is related to that issue.
I don't see that it matters whether I have 1 or 100 menu items. All I am testing at the moment is essentially one. And it's the only one that has anything other than a simple text field identifying which panel it is. When I get this one working I'll go on to the next place holder and build content for it.
Navigation is obviously going to the correct panel because the input fields are the correct ones. That wasn't my question.
My question is why aren't they showing up until I move my mouse over them? What can I do to make the entire panel show up properly? And why is it that only the input fields show up and not the text fields? I have tried everything I can and have Googled everything that I can think of. That's why I asked my question on the forum. If that's not a description that can be understood adequately enough to intuit a possible solution I'll try to provide more information. But I doubt I can pare it down much more and still have it work.
It is difficult to include the entire set of classes involved with this problem. Mainly because of the amount of code generated by Matisse. I was hoping this might be a simple problem that an experienced Swing programmer might be able to answer.
I'll see about putting together an example for you. However, as I stated, I am using Eclipse Matisse and relying on it to generate the panel initialization code. Also I've seen too many programmers get into criticizing code style rather than help find a solution to a problem. That's why I shy away from providing complete modules.
Message was edited by:
GPuckett
# 4
> My question is why aren't they showing up until I move my mouse over them?
Generally this occurs when you add components to a panel after frame is visible. Something like this.
JPanel = new JPanel();
frame.getContentPane().add(panel);
frame.setVisible(true);
panel.add(someComponent);
> And why is it that only the input fields show up and not the text fields?
Again, I don't know what the difference between an "input field" and a "text field" is. I would think they are the same.
Thats why I ask for a SSCCE.
> If that's not a description that can be understood adequately ....
Again thats why I ask for a SSCCE. If I can execute the code I can see whats happening. "A picture is worth a thousand words".
> I am using Eclipse Matisse and relying on it to generate the panel initialization code
Well, maybe you should learn how to write your own code first. Then once you understand how it works you can compare your code with the Matisse code to see what the difference is.
# 5
I finally figured it out. I would assume what was causing this is a fairly common problem with Swing development.
After the individual JPanel classes were added to the CardLayout they were never set to not visible. In this situation I didn't know that it was necessary. I assume that since there was never a single component that had focus and they were stepping on each other. The result was that very little was displayed in the application.
I tried adding setVisible(false) immediately after the JPanel classes were instantiated and received the same result. It was only AFTER the instantiated classes were added to the appPanel with the CardLayout attribute that setVisible(false) made a difference. At this point the application started to work as expected.
I knew this had to be a very simple issue with an easy resolution.
Now that I know the behavior of this issue I think I would easily recognize it from any decent description without having to see the entirety of the code. I guess this either isn't as obvious to others or they just haven't experienced this small problem.
# 6
> After the individual JPanel classes were added to the CardLayout they
> were never set to not visible. In this situation I didn't know that it was necessary.
Its not. If you actually look at the Card Layout tutorial as I suggested you will see the the setVisible(..) method is never used, execpt on the frame
> Now that I know the behavior of this issue I think I would easily
> recognize it from any decent description without having to see the entirety of the code
Not true. But default all components (except JFrame, JDialog and JWindow) are set visibile by default. Never in a million years would I have guessed that you would be playing with the visibility of individual components because there is no reason to do so.
We are not mind readers. We can't guess what non standard code you are using. If you looked at the tutorial you have a working example and then you can compare your code with the working code to find the difference. We don't have this option if you don't post the SSCCE and we are not miracle workers.
I still don't understand your solution nor would I be able to recommend it to anyone else on the forum
