This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

hear to all dynamically added jButton ActionCommand

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have JFrame Main which contains in left side JXTaskPainContainer (content menu) and right side cardLayout panel. Content menu has a JButton btn "data", when we click btn it shows on cardLayout panel -> JPanel Test. Under content menu there is addButton, which adds dynamic btn -> "data2", "data3",.... Here is Main class and Test JPanel class:

The problem that I am facing is when I added dynamically "data2", "data3",..., then when I click jButton2 which must call Test.jButton1, output it prints (or hears) only last clicked Test.jButton1.
I would like to ask how can hear all Test.jButton1 action commands in Main class?
 
Marshal
Posts: 74371
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not quite sure I understand the problem correctly, but try listing all listeners with the getActionListeners() method. You can create listeners with references, and in Java8 you can shorten them no end by using a λ. You can add and remove action listeners at any point in the code.That is what you can do; whether it is a good idea to change functionality of a button to something different from what the user expected is a different question.
 
Tai Yo
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Campbell Thank you for your reply. Sorry if I don't formulated clearly my question.
The layout of Main frame as follows:

jButton2 will call Test.jButton1:

but when I click jButton2 it calls only last clicked Test.jButton1.
For example, if I added dynamic data2 and data3, then only jButton1 of data3 is clicked, for data2 is no sense.
I don't know how to call all the Test.jButton1 action commands of dynamically added data2, data3,... by jButton2.
 
Master Rancher
Posts: 4507
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post a small, complete program that compiles and executes for testing that shows the problem?
 
Tai Yo
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to ask, this problem can be solved with AbstractButton.doClick()?
 
Norm Radder
Master Rancher
Posts: 4507
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post a small, complete program that compiles and executes for testing that shows the problem?
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic