In java i have a panel and two buttons in it named b1 and b2. when i copy the panel and past it in the same frame, the button names become b3 and b4 but the code i wrote in the b1 doesn't shift to b3 ? how do i do this i.e. when creating a copy of the panel the code in b1 should be implemented in b3, also can it be done that suppose i have 'b2.doClick()' in the b1 actionperformed code turns to 'b4.doClick()' in b3 ActionPerformed when i duplicate the panel ? i am using netbeans(if this helps)
Asked
Active
Viewed 122 times
-1
-
4For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Sep 12 '12 at 08:54
-
1This looks something related to NetBeans GUI builder. – Guillaume Polet Sep 12 '12 at 09:28
-
1@AndrewThompson I don't think that this is a code-related question. More of a *how do I do this in netbeans?* – brimborium Sep 12 '12 at 12:59
1 Answers
1
One way to get b1
and b3
to do the same thing is give each one the same action.
JButton b1 = new JButton(new SomeAction());
JButton b3 = new JButton(new SomeAction());
class SomeAction extends AbstractAction {
public void actionPerformed(ActionEvent e) {
// do something
// call some other action
}
}

Catalina Island
- 7,027
- 2
- 23
- 42
-
+1 for `Action`; there's an example using `doClick()` [here](http://stackoverflow.com/a/5797965/230513). – trashgod Sep 12 '12 at 14:15