• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How to navigate between multiple GUI classes

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you checked out CardLayout?
 
Harvi Khaira
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ive never heard of it, could you elaborate a little please?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That would be the java.awt.CardLayout class: How to Use CardLayout
 
Harvi Khaira
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
very simple demo of show(), next(), previous()


 
Harvi Khaira
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic