Jiayi Guo
Jiayi Guo

Reputation: 103

How to get value from ActionListener

I have created a JRadioButton Group, and I want to set a parameter to a value when a specific button is selected. I have added ActionListener to this button like the codes. But I how can I use the value in other actionlisteners?

grid_rb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
network_type = "--grid-net";
}
});

Upvotes: 0

Views: 3666

Answers (2)

ddmps
ddmps

Reputation: 4380

Buttons have a method isSelected(), that tells you if the button is selected or not.

Upvotes: 1

tckmn
tckmn

Reputation: 59273

Use a getter and setter:

String networkType;

grid_rb.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent arg0) {
        setNetworkType("--grid-net");
    }
});

public void getNetworkType() {
    return networkType;
}
public void setNetworkType(String nwt) {
    networkType = nwt;
}

Upvotes: 1

Related Questions