aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes open another GUI with a button 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 "open another GUI with a button" Watch "open another GUI with a button" New topic
Author

open another GUI with a button

Katie Tuffrey
Greenhorn

Joined: Dec 14, 2011
Posts: 6
im really stuck!
im a complete newbie when it comes to java!
im using eclipse just so you know :/

basically i would like to know how to open another gui by clicking a button on the first gui...

example code really helps me!!

thanks so so so much in advance!
xx
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> basically i would like to know how to open another gui by clicking a button on the first gui...

the code you have to open the 1st GUI would be very similar to the code you'd have in the button's actionListener
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Hi Katie, welcome to CodeRanch!

Can you show us what you've tried so far?
Katie Tuffrey
Greenhorn

Joined: Dec 14, 2011
Posts: 6
i dont have an actionlistener yet....

i have the basic syntax written, but dont know what to type for an open command; i know an exit :
button3.addActionListener(new ExitHandler());
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

i have an old example i posted here that might help. it is here
this is probably the part you are interested in


SCJP
Visit my download page
Katie Tuffrey
Greenhorn

Joined: Dec 14, 2011
Posts: 6
public void actionPerformed (ActionEvent e){

if (e.equals ("button1"))
this.setVisible(false);
StudentProfileGui itemloader=new StudentProfileGui();
itemloader.setVisible(true);

if (e.equals ("button2"))
this.setVisible(false);
StudentProfileEditGui itemload=new StudentProfileEditGui();
itemload.setVisible(true);


thats what im using at the min... where the underline it it says void is an invalid type for the action actionPerformed and the other is missing syntax ( )
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> if (e.equals ("button1"))

this is just totally wrong.

'e' is an actionEvent, so cannot be equal to a button
button1 is probably the variable name for the button,
so should not be enclosed in quotes.

what you probably want is

if(e.getSource().equals(button1))

It looks like you are attempting to switch back and forth between frames,
so you'd probably be better off using a single frame and having the contentPane
set up as a CardLayout (which contains multiple panels, only one panel showing at a time),
then using a JMenu to switch between panels.



Katie Tuffrey
Greenhorn

Joined: Dec 14, 2011
Posts: 6
thanks so much for all your help, i managed to sort it earlier! got a new question now but will start a new topic

xx
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
How did you sort it out? Using getSource() is liable to lead you into non-object-oriented programming.

If there is any more discussion, I think it would sit better on our GUIs forum, so I shall move this thread.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

Using getSource() is liable to lead you into non-object-oriented programming.

true enough. that is what you do when you have lots of if-else statements in actionPerformed().
still, it is a common practice especially in small projects.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Just because it's common doesn't make it less bad.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: open another GUI with a button