• Post Reply Bookmark Topic Watch Topic
  • New Topic

JButton Quick Help  RSS feed

 
Ben Jass
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Last question for the day, how do I make it so that when I click a button like "apply", it automatically closes the current JFrame that is up, and activates all of the actionListener's within the JFrame.
I have a feeling you can just invoke the actionListener by creating the object of that class, pretty simple.
I just am curious about how to close the current JFrame? I know how to use setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);, but that doesn't seem to work if I put that in for an actionListener for the apply button.

I appreciate the help.
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and activates all of the actionListener's within the JFrame


Frames don't support ActionListener, so that doesn't make sense.

do I make it so that when I click a button like "apply", it automatically closes the current JFrame that is up


 
Ben Jass
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that Frames don't support actionListeners. I'm just saying that components that are within the JFrame hold actionListeners. Thank you for the second problem help.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Jass wrote: I know that Frames don't support actionListeners. I'm just saying that components that are within the JFrame hold actionListeners. Thank you for the second problem help.


Okay. So keep a list of the components which do have ActionListeners, and iterate through that list calling the ActionListener of each component. Although I can't conceive of a design in which closing a frame has the side effect of essentially pressing every button which happens to be in the frame. Is that really what you meant? I would approach the general requirement ("Do something when a JFrame is closed") in a more normal way, namely by providing the JFrame subclass with a "close" method which does the required things, and by having whatever closes the JFrame also call that method.

Or in your particular case, the method would be called "apply" or something sensible like that.
 
Ben Jass
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand where you are coming from, and you were answering the question in the way I asked it. I'm rather new to the whole GUI thing, the only way I can activate something when let's say I put some text in the JTextField is by pressing "enter" on my keyboard. I want it to be somewhat professional by pressing the apply button which triggers these actions, maybe there is a better way? Also I'm having some trouble with saving the information on the set up screen if I were to re-open it. The set up screen is where this is all taking place.

First setup screen:


Second time I bring up the setup screen after closing it once:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!