• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

open another GUI with a button

 
Katie Tuffrey
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 5334
48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Katie, welcome to CodeRanch!

Can you show us what you've tried so far?
 
Katie Tuffrey
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4363
2
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have an old example i posted here that might help. it is here
this is probably the part you are interested in
 
Katie Tuffrey
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48381
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4363
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20493
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just because it's common doesn't make it less bad.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic