Skip to main content

JButons and their listeners

2 replies [Last post]
bplusg
Offline
Joined: 2010-05-06

Hi to all!
Im trying to add to a JPanel 2 Buttons, but im getting alot of problems with the listener.
One of then is set to disable, and should be enabled when the user click in the second button.
I have checked and follow some manuals, and copy almost all the code, but i'm getting some strange behavior.
I have extended my class to implements ActionListener, and i defined the actionPerformed method. But when i clicked the second button (cancel button), nothing happens.
A extract from my code is bellow, can someone helps? What i'm doing wrong?

/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setEnabled(false);
jButton.setName("Ok");
jButton.setText("Ok");
jButton.setMnemonic(KeyEvent.VK_UNDEFINED);
jButton.setActionCommand("ok");
jButton.addActionListener(this);
}
return jButton;
}

/**
* This method initializes jButton1
*
* @return javax.swing.JButton
*/
private JButton getJButton1() {
if (jButton1 == null) {

jButton1 = new JButton();
jButton1.setText("Cancel");

jButton.setActionCommand("cancel");
jButton.addActionListener(this);
}
return jButton1;
}

public void actionPerformed(ActionEvent e){

System.out.println(":: actionPerformed()");

if ("cancel".equals(e.getActionCommand())) {
System.out.println("Close Button Cliked");
modelTerrenos.addElement("yey");
jButton.setEnabled(true);
}
if(e.getActionCommand().equals("ok")){
System.out.println("Ok Button Cliked");
}
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
umeshrama
Offline
Joined: 2009-01-02

Dear Friend,

where did u initialize the JButton1,
lots of confusion in u r code, can u send me the entry code,

bplusg
Offline
Joined: 2010-05-06

Ups! I make the button1 initialization wrong! my mistake :s