Re: JButton ActionListeners
Hmm. I'm sure you could get to the buttons somehow through getting all the components of the JPanel, but it'd be far easier to just go "JButton b1=new JButton(); b1.addActionListener()".
Re: JButton ActionListeners
The way you're doing it now, you create a new JButton and pass it directly into the JPanel.add() method.
You need to keep around a reference to each JButton, at least long enough to add an ActionListener to it. Something like:
Code:
JPanel tableButtonPanel = new JPanel();
JButton addButton = new JButton("Add thing");
addButton.addActionListener(listener);
tableButtonPanel.add(addButton);
//same for delete
//same for modify
Re: JButton ActionListeners
All Swing's JComponents are Containers; you can get all Components from a Container; a bit of casting on the Components should do the trick but I find it an ugly hack ...
kind regards,
Jos
Re: JButton ActionListeners
Okay someone is going to kick my butt for this. But in the interest of exploring boundaries and possibilities:
Code:
public class MyButton extends JButton {
public MyButton(String label, ActionListener listener){
super(label);
addActionListener(listener);
}
}
When the standard API doesn't provide a direct way, you can always bodge something together yourself.
Re: JButton ActionListeners
Quote:
Originally Posted by
gimbal2
When the standard API doesn't provide a direct way ...
Code:
tableButtonPanel.add(new JButton("Add thing") {
{
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// respond to button click
}
});
}
});
:8):
Re: JButton ActionListeners
Hey thanks everyone for your suggestions they are a great help :) finally i can move on from this task :D
Re: JButton ActionListeners
A note: the last two responses are in the way of a joke. While valid code, this is not the way to write a maintainable application.
Kevin's response at #3 is the way to go.
db