aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How to navigate between multiple GUI classes 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 "How to navigate between multiple GUI classes" Watch "How to navigate between multiple GUI classes" New topic
Author

How to navigate between multiple GUI classes

Harvi Khaira
Greenhorn

Joined: Feb 26, 2009
Posts: 17
Hello all

Ive got to replicate a sat nav system for my dissertation, and ive got numerous screens, but i would like assistance on how to move between the screens.
I need the system to close the current screen and open the next screen and move in between etc, but i dont know how to do that

Can anyone help???

Harv
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

Have you checked out CardLayout?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Harvi Khaira
Greenhorn

Joined: Feb 26, 2009
Posts: 17
Ive never heard of it, could you elaborate a little please?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42276
    
  64
That would be the java.awt.CardLayout class: How to Use CardLayout


Ping & DNS - my free Android networking tools app
Harvi Khaira
Greenhorn

Joined: Feb 26, 2009
Posts: 17
Thanks for that

I had a look at the cardlayout, but i dont think thats what i need - unless someone can explain exactly how it is what i need

Basically, im coding in Java (obviously) using NetBeans 6.5 to replicate a sat nav system, in which i have numerous GUIs to represent different states in the system (e.g. Home/Main Menu, a keypad screen for address input etc) and on each of these screens i have "Home" and "Next" buttons which need to link to the relevant GUI/class.

When those buttons have been pressed, that GUI needs to close and the relevant one needs to open, or appear on top of the previous interface. The cardlayout (from what i read) is based on the use of JPanels, whilst im using JFrames (created and layout formatted via NetBeans)

All advice/help is greatly appreciated
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Think of how most programs you run behave. Do you often have apps that close main windows and open others all the time? Or is it more common for the app to show different screens on the same app when the app's state changes, or in other situations, show a main window and then open dialog windows when necessary?

I believe that you are significantly limiting your program's design options by coding to the JFrame. You are far better off to strive to code to a JPanel rather than a root container such as a JFrame. If you create a GUI that's based on a JPanel, then you have the freedom of displaying it in a JFrame or if you'd prefer a JDialog, or a JApplet, or as in this case a JDialog.

So the bottom line (in my opinion) is to not let the limitations of your design dictate how your app will run. Free your design up with JPanels and use a CardLayout. Notice that this opinion is shared by many here in this thread who are much smarter than me, one a moderator and the other an author of Java texts.

As long as I'm on a soapbox, let me tell you how I think use of NetBeans-generated code actively hinders a student's ability to learn Swing and how your very problem is an example of this,... but if I get started, I might not be able to stop! To cut this short, my other recommendation is for you to not use NetBeans to generate your code but instead to go through the Swing tutorials (a link was provided above). You'll not regret this if you do it.
Harvi Khaira
Greenhorn

Joined: Feb 26, 2009
Posts: 17
oh well my 2nd year lecturer taught me f**k all then, he said that a JPanel has to be in a JFrame all the time and thats the only way it works etc

Well ive taken your advice and kinda started again but using JPanels instead, however, as im pretty useless at Java GUI i need some help.

How do i get the JPanel to run? ive got a main method in the class, and its basically the same as what i had in the JFrame version but nothing comes up on screen (to help mecheck that my buttons do what they should do)
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
sounds like you've missed the point of a cardlayout - it's just a layout manager, very similar to a JTabbedPane, but without the tabs.
or, think of an installation wizard - next/previous etc

so, you add all your navigation screens (panels) to another JPanel (set as a cardlayout)
this cardLayoutPanel you then add to a JFrame.
rest is per normal, except you might have buttons or a JMenu to navigate between the screens
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Harvi Khaira wrote:...he said that a JPanel has to be in a JFrame all the time and thats the only way it works etc

A JPanel needs to either be held by a root container such as a JFrame, JWindow, JApplet, or JDialog, or it needs to be held by something whose container holding hierarchy leads ultimately to a root container -- for instance your JPanel held by a JScrollPane held in another JPanel, held in another JPanel, and finally that JPanel held in the root conainer. But what I'm getting at is that when creating your display, you only worry about the JPanel for now, and later after you've created it, worry about how you will show it in a root container.

Well ive taken your advice and kinda started again but using JPanels instead, however, as im pretty useless at Java GUI i need some help.

Most of all you need to read the Sun Swing tutorials from the start. Nothing we can do can put the knowledge into your head. It will only get there through your diligence and hard work.

How do i get the JPanel to run? ive got a main method in the class, and its basically the same as what i had in the JFrame version but nothing comes up on screen (to help mecheck that my buttons do what they should do)

Please read the tutorials. You are missing some basic concepts that are required for us to even begin to explain things to you.
Harvi Khaira
Greenhorn

Joined: Feb 26, 2009
Posts: 17
Ive been reading the tutorials etc and it seems to be making some sense

However, i couldnt grasp the concept of how the cardlayout demo changes the cards/display. Do the events need to be the ItemStateChanged event or can they be the ActionPerformed event? I've never use the state change one before, only the ActionPerformed event and as im using buttons i thought that was the standard way to do things, and as you already know im useless/uneducated about Java GUIs

Also linking to that, is it possible to have an IF or CASE statement from which the program recognises which 'screen' it came from and which different state of a 'screen' it requires, e.g. when i need to add an address or favourite, i need to input a name, address and city, whereas if i was to go down the 'Input destination' path and then choose to save the destination, i would need to bring up the keypad but only require a name to be saved to that address? is that possible or extremely complicated and no-one on here has the patience to teach that to a Java amateur
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

1) you can call the "switching" methods from virtually any code. Whether this is an ActionListener triggered by a button or menu item, or a MouseEvent triggered by a click, it doesn't matter. An itemStateChanged event should be just fine.

2) If you give each component its own unique name (hint: Component already provides getName() and setName(String) methods), you can use that as the names for the cards. That way, there is an easy mapping between a card and a component.
Harvi Khaira
Greenhorn

Joined: Feb 26, 2009
Posts: 17
im confused by the comment above ^^^

so if i choose to do the ItemStateChanged() of a button in the relevant .java class, that would be ok?
also, would i just write in the method something like


whatever the show or setDisplay method is etc
cardx being the card i wish to display
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
very simple demo of show(), next(), previous()


Harvi Khaira
Greenhorn

Joined: Feb 26, 2009
Posts: 17
cheers, that helped quite alot to be honest
however, i still have one query. Ive got buttons in different classes that have the actionperformed() method, how would i do the actionlistener for them to link to the layout class i have? would i be best off trying to pass a parameter that allowed the program know which button has been pressed and what it should do etc
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
if you look at the actionPerformed in the demo, the button needs a reference to:
the cardlayout manager
the cardlayout panel
also 'currentlyShowing', but this is only to have next/previous to stop at the last/first panel,
otherwise it would wrap, which may be what you want.

the above can be obtained by a bunch of get...'s, but far easier to pass a reference
Harvi Khaira
Greenhorn

Joined: Feb 26, 2009
Posts: 17
Now im getting confused and frustrated!

I just cant seem to get this to work so heres my code:

This is a single button from the Home.java class:

I used to get an error when i had the code


but as you can see, i got rid of the code

Now this is my Layout.java class code:



All help is greatly appreciated!!!

I just seem to have no luck and/or clue
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
see if this makes any sense.

there a 2 'AnotherFrame' classes, one has a single button, and changes the layout via next(),
other one has a button for each card. note comments in changeCard()

(also, changed Home(), keyPad() etc to normal panels - I don't have your satnav classes)

Harvi Khaira
Greenhorn

Joined: Feb 26, 2009
Posts: 17
Cheers, the changeCard() makes sense actually, dont know why i did it the stupid long way

I looked at your AnotherFrame class and seen that you created an ActionListener which did the work of changing the display by passing a parameter to the changeCard() method. I was thinking that it maybe the best way for me to do it and just access and add that AL to all of the buttons and pass a parameter to indicate which card to display next etc but i cant seem to understand how to do it. I copied your AL and created it in the constructor of the layout.java class so it could be access by other classes but the actual code in the ActionPerformed section doesnt seem to come together for me.

I thought that it could be something like:

with stateDesired being a String variable that is changed by the button itself when clicked, if that makes any sense

would that seem a good way of doing it by any chance or is there a much simpler/efficient way of doing this?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
if I'm reading it right, the button has:
an actionListener to change the variable returned from getStateDesired()
an actionListener for layout.changeCard(..)

I don't think there's any guarantee that the actionListeners will fire in the order you want
i.e. getStateDesired() might return the name of the currently showing card (prior to the variable being updated)

could be that you should add layout.changeCard(..) to the end of the actionListener that handles this
"with stateDesired being a String variable that is changed by the button itself when clicked"
Harvi Khaira
Greenhorn

Joined: Feb 26, 2009
Posts: 17
the 'Go' button, for example, has:

theres nothing else in the code for it

i havent implemented the 'stateDesired' variable as i thought that it might be implementing too much data redundancy, and that every button will change the variable which may cause the system to backfire
im not 100% about it though

Do you think it could be a good idea?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
not sure I'm following your descriptions properly.

can you put together a sample app - just use JPanels with an id (titledBorder perhaps) for your satnav screens,
so we can see exactly how you're doing it
Harvi Khaira
Greenhorn

Joined: Feb 26, 2009
Posts: 17
actually its ok, ive managed to sort it out, touchwood, the program displays the relevant cards that i want

But thanks to everyone who helped, greatly appreciated

Ill probably be back with more queries but thanks once again
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to navigate between multiple GUI classes