• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 310
  • Last Modified:

How to get a JButton in Collection to Modify another JButton

I've created a collection of extended JButtons in a JPanel.
Upon clicking a button, the button performs an action, however, I also need it to modify preference on a few adjacent buttons namely the iconfield.
The restriction I have is that the extended button is not directly aware of adjacent buttons because it may or many not have them. The JPanel does however does know about the buttons because of the collection.
I've tried to add a Listener on the JPanel but it only gets triggered during an add or removal of a button.
I've tried to use a MouseListener on the JPanel but I'm not getting the index of the button that performed the click.
Does anyone have any suggestions on how to perform this action?

0
Tag_Markman
Asked:
Tag_Markman
  • 5
  • 3
  • 3
3 Solutions
 
aozarovCommented:
Make your pannel an ActionListener on each one of your buttons that you need.
button.addActionListener(your_pannel);
0
 
aozarovCommented:
Actually I mean make an anonymous ActionListener and add it to your buttons.
0
 
CEHJCommented:
You could send a custom event from the button to tell the panel to modify some other buttons:

http://javaalmanac.com/egs/java.util/CustEvent.html
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
aozarovCommented:
Something like that:
In your JPanel:
ActionListener actionListener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
change the properties of the other buttons based on the event.
}
};

button.addActionListener(actionListener);
0
 
Tag_MarkmanAuthor Commented:

CEHJ:
Thank you but, I searched the Javaalmanac first and attempted this example before I posted this question. I'm not trying to create a new listener or action for the button. I'm trying to get both the JButton and the JPanel to wake up and perform an action when a JButton is pressed. I thought I could put a listener on the JPanel but I can't get it to work. I can get the JButton to do whatever, I can't get the JPanel to respond.

aozarov:
I'm not sure I understand.
I have a button, that is unaware of other buttons. I've added a listener to the JPanel similar to your example:

      java.awt.event.ActionListener actionListener = new java.awt.event.ActionListener()
      {
           public void actionPerformed(java.awt.event.ActionEvent e)
          {
               // perform action
          }
      };


However,
Button.addActionListener(myPanel);
produces and error saying that an addActionListener can not be applied to a JPanel

Any other suggestions?




0
 
aozarovCommented:
>> Button.addActionListener(myPanel);
Right, I corrected myself in the second post.
you create the anonymous action listener as a JPanel variable but then you add it as a listener to the button.
Hence:
// In the JPanel code:
  java.awt.event.ActionListener actionListener = new java.awt.event.ActionListener()
      {
           public void actionPerformed(java.awt.event.ActionEvent e)
          {
               // perform action to handle the button event
          }
      };

    Button.addActionListener(actionListener);
0
 
Tag_MarkmanAuthor Commented:

Works except for one part:

>>I'm not getting the index of the button that performed the click

I'm getting an event triggered and the panel is aware of it, but the panel does not know which button in the jpanel's components list was clicked.
0
 
aozarovCommented:
Two ways:

1. in the event listener you can call
JButton button = (JButton) e.getSource();
to get the button that was pressed.

2. you can call for each button the setActionCommand(<with a name that you pick>)
and then in the event listener calling e.getActionCommand() will return that string.
0
 
CEHJCommented:
If you need to know the index of the button, you can keep your indices in a Map. Something like:

final int NUM_BUTTONS = 10;
Map indexMap = new HashMap();

JButton[] buttons = new JButton[NUM_BUTTONS];
for(int i = 0;i < NUM_BUTTONS.length;i++) {
      buttons[i].addActionListener(yourPanel);
      indexMap.put(buttons[i], new Integer(i));
}

...

// (In the listener)
public void actionPerformed(ActionEvent e) {
      Object o = e.getSource();
      if (o instanceof JButton) {
            int index = ((Integer)indexMap.get(o)).intValue();
      }
}
0
 
Tag_MarkmanAuthor Commented:
Bingo!

This is perfect!

aozarov:
Thank you so very much. I got it running.

CEHJ:
Thank you very much as wellI needed to get them in an index map.

I upped the points so I could split it better.
0
 
CEHJCommented:
:-)
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 5
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now