-1

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)

Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117

1 Answers1

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