Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can I make a JPanel perform an action?

 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By adding an ActionListener to a button, we can make the button affect things it doesn't have privelage to talk to itself.

Is it possible to use a similar system with a JPanel and the container the JPanel is in? Have the container add an ActionListener to a JPanel, and then make the JPanel fire an action event?

Thanks,
Nick
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to Use Key Bindings
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
Once I've added something to the action map, how can I fire it whenever I like? I don't want it to be a response to keyboard input.
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There might be a lot of possibilities depending on what you are trying to do. Can you say some more about what you want to do?
 
learner
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to use a similar system with a JPanel and the container the JPanel is in? Have the container add an ActionListener to a JPanel, and then make the JPanel fire an action event?

Ofcourse, you can make a JPanel listen to events. If you have a button and textarea in the JPanel, then whenever an event occurs in the text area or button, the same action takes place.
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My deal is, say I have 2 classes which extend JPanel in one big JPanel. I want to make it so that when JPanel A is modified it automatically updates JPanel B, but I'd rather not give JPanel A a pointer to JPanel B. My dream is that from the big JPanel I could add an action to A which updates B.
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two possibilities come to mind for this
1 — THE OBSERVER PATTERN
2 — a PropertyChangeListener which is discussed here: How to Write a Property Change Listener.
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I think that'll work.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic