0

I am using Jbuttons for a game called who wants to be a zillionaire in an assignment. I am using the buttons for selecting an answer. Here is the code for some of the buttons. Code:

}

        });
        Answer2.setBounds(220, 105, 188, 25);
        panel.add(Answer2);

        Answer1 = new JButton("A");
        Answer1.setBackground(Color.YELLOW);
        Answer1.setHorizontalAlignment(SwingConstants.LEFT);
        Answer1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Answer1.setBackground(Color.BLUE);
                Answer1.setForeground(Color.WHITE);
            }
        });
        Answer1.setBounds(20, 105, 188, 25);
        panel.add(Answer1);

        Answer4 = new JButton("D");
        Answer4.setBackground(Color.YELLOW);
        Answer4.setHorizontalAlignment(SwingConstants.LEFT);
        Answer4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
        Answer4.setBackground(Color.BLUE);
        Answer4.setForeground(Color.WHITE);
    }
});

At the moment multiple buttons can be selected and I wish to prevent this. It would be helpful if you could show me an example of code that could be used to do this.

Michael_Scharf
  • 33,154
  • 22
  • 74
  • 95

1 Answers1

0

Use the setEnable() method to make unwanted buttons disable or make a Boolean class level variable and make it true when the desired button is clicked. Then before invoking other buttons' algorithms simply check whether this variable's value is true.

Edited

   public class YourClassName{

       static boolean isAnswerSelected = false;

       //your codes goes here

       Answer1.setHorizontalAlignment(SwingConstants.LEFT); 
       Answer1.addActionListener(new ActionListener() { 

         public void actionPerformed(ActionEvent e) { 

             if(!isAnswerSelected){  //this line will check whether an answer is already selected
                  isAnswerSelected = true;   // this will make other answers unselectable
                  Answer1.setBackground(Color.BLUE); 
                  Answer1.setForeground(Color.WHITE); 
             }
         } 
       });

   }  

Use the above if statement and isAnswerSelected = true; line for all your actionPerformed() methods for all buttons.

Thush-Fdo
  • 506
  • 1
  • 9
  • 28
  • Hi, was just wondering is you could give an example thanks. If so that would be great thanks – James Faulkner Apr 02 '16 at 21:11
  • private JButton Answer1; private JButton Answer2; – James Faulkner Apr 02 '16 at 21:12
  • Answer1 = new JButton("A"); Answer1.setBackground(Color.YELLOW); Answer1.setHorizontalAlignment(SwingConstants.LEFT); Answer1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Answer1.setBackground(Color.BLUE); Answer1.setForeground(Color.WHITE); } }); Answer1.setBounds(20, 105, 188, 25); panel.add(Answer1); – James Faulkner Apr 02 '16 at 21:12
  • Answer2 = new JButton("B"); Answer2.setBackground(Color.YELLOW); Answer2.setHorizontalAlignment(SwingConstants.LEFT); Answer2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Answer2.setBackground(Color.BLUE); Answer2.setForeground(Color.WHITE); } }); Answer2.setBounds(220, 105, 188, 25); panel.add(Answer2); – James Faulkner Apr 02 '16 at 21:13
  • If possible could you give an example with the code I have added in comments by showing how I could use the SetEnable method. – James Faulkner Apr 02 '16 at 21:14
  • @jehangir Please check the edited section. You can use this type of format to make other questions un selectable if one is selected. – Thush-Fdo Apr 03 '16 at 12:15