Reputation: 297
How to perform different action on each click with only one JButton.. please help me.. Here is my code..
btn1.addActionListener(new ActionListener(){
int clicks;
@Override
public void actionPerformed(ActionEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
Object source = e.getSource();
if(source == btn1){
clicks++;
if(clicks==1){
txt1.setText("a");
clicks=0;
}
if(clicks==2){
txt1.setText("b");
clicks=0;
}
if(clicks==3){
txt1.setText("c");
clicks=0;
}
}
}
});
please help me..
Upvotes: 0
Views: 1808
Reputation: 2877
You are resetting click
with every action and thus only get action "a".
btn1.addActionListener(new ActionListener(){
int clicks;
@Override
public void actionPerformed(ActionEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
Object source = e.getSource();
if(source == btn1){
if(clicks%3 == 0){
txt1.setText("a");
}
if(clicks%3 == 1){
txt1.setText("b");
}
if(clicks%3 ==2){
txt1.setText("c");
}
clicks++;
}
}
});
Upvotes: 0
Reputation: 35011
You should use a MouseListener/Adapter
rather than an ActionListener
, implement mouseClicked
, and use MouseEvent.getClickCount()
Upvotes: 1