Simply add an ActionListener
to the JButton
:
JButton btn=new JButton("Do something");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// do something for button
}
});
If you want Netbeans to do it for you:
Drag a JButton
from the palette to your container (Select Windows > Palette to open the Palette if you don't see it. You use the Palette to drag and drop UI components to the design area)
Simply double click on the JButton
in question in the designer view and it will auto create an ActionListener
and override actionPerformed
. and in the method it creates is here you would do the work. It should generate something like:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//TODO: Add your handling code here:
}