Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

hear to all dynamically added jButton ActionCommand

 
Tai Yo
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • 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?
 
Campbell Ritchie
Marshal
Posts: 52621
119
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I would like to ask, this problem can be solved with AbstractButton.doClick()?
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post a small, complete program that compiles and executes for testing that shows the problem?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!