so i have jradiobuttons and their listeners contains like this:
if(VotePresidentPanel.rbPres1.isSelected()){
int[] incrementVote={1};
for (int v : incrementVote){
resultP1 = (pvotes[v - 1]+=1);
}
ResultPresidentPanel.lblResPres1.setText(Integer.toString(resultP1));
}
but i want the listeners of my jradiobuttons to only function AFTER i clicked a certain jbutton. i tried putting the listeners of jradiobutton inside the listener of jbutton but it still doesn't work. here's the code of what i mentioned that i tried doing:
if(e.getSource().equals(VoteButtonsPanel.btnVote)){
if(VotePresidentPanel.rbPres1.isSelected()){
int[] incrementVote={1};
for (int v : incrementVote){
resultP1 = (pvotes[v - 1]+=1);
}
ResultPresidentPanel.lblResPres1.setText(Integer.toString(resultP1));
}
//...and other listeners for the other jradiobuttons
}
please help thank you so much :)
i have these jradiobuttons
() Pres1
() Pres2
() Pres3
() Pres4
i want them to be still clickable as it is a voting system, but my problem is every time i click on the radio buttons, the votes will increment even tho i still haven't clicked the VOTE
button. i want the votes to increment only if after i clicked the VOTE
button.
the listeners of jradiobuttons are like this/the functions of increment:
if(VotePresidentPanel.rbPres1.isSelected()){
int[] incrementVote={1};
for (int v : incrementVote){
resultP1 = (pvotes[v - 1]+=1);
}
ResultPresidentPanel.lblResPres1.setText(Integer.toString(resultP1));
}
and i tried putting them inside the block of my VOTE
listener, but still doesn't work. thank you for any help :)
EDIT 2 this is what i tried base from the reply of copeg
boolean enableJRadioButton = false;
if(e.getSource().equals(VoteButtonsPanel.btnVote)){
enableJRadioButton=true;
}
if(VotePresidentPanel.rbPres1.isSelected()){
if(enableJRadioButton==true){
int[] incrementVote={1};
for (int v : incrementVote){
resultP1 = (pvotes[v - 1]+=1);
}
ResultPresidentPanel.lblResPres1.setText(Integer.toString(resultP1));
}
}
but i want the listeners of my jradiobuttons to only function AFTER i clicked a certain jbutton.
If you wish to have the JRadioButton still enabled, but not have it's ActionListener fire you can use a boolean flag that is evaluated in the JRadioButton
ActionListener, initialize it to false
and set to true
in the JButton
ActionListener
boolean enableJRadioButton = false;
...
final JRadioButton myRadioButton = new JRadioButton("Do Something");
myRadioButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
if ( enableJRadioButton ){
//do something
}
}
});
myButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
enableJRadioButton = true;
//do something else if necessary
}
});
If you don't mind if the JRadioButton is enabled, consider enabling/disabling the JRadioButton
s only after the JButton
is clicked - eg in an ActionListener
added to the JButton
.