0

I'm trying to program a fully functioning Windows 7 calculator in programmer mode. Right now, I'm just working on getting the buttons to work properly. So I want for the buttons that will be used for hexa to be disabled until the radio button for hex is selected. So the A-F buttons will be disabled while in dec or Bint mode until it is selected otherwise.

Here is the button A:

JButton button_A = new JButton("A");
button_A.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        button_A.setEnabled(false);
        hexDisable();
        textField.setText(textField.getText() + "A");
    }
});

Here is the radio button:

JRadioButton rButton_Hex = new JRadioButton("Hex");
rButton_Hex.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        hexDisable();
    }
});
rButton_Hex.setBounds(9, 218, 54, 23);
contentPane.add(rButton_Hex);
Group1.add(rButton_Hex);

This is the method that will enable and disable the appropriate buttons.

public void hexDisable(){
    button_A.setEnabled(true);
}

I'm extremely new to using GUIs in Java.

Pang
  • 9,564
  • 146
  • 81
  • 122
  • 1
    If I may suggest, perhaps consider creating button ToolBars or individual button panels for your different calculator modes and simply hide (.setVisible(false)) the ones you don't currently need and show the one(s) you do need (.setVisible(true)). – DevilsHnd - 退職した Jun 19 '17 at 03:38
  • You could: Place groups of buttons in a `List` of some kind to make it easier to change their states; You could: Place the `List`s in some kind of `Map` to make it easier to manage – MadProgrammer Jun 19 '17 at 03:41

2 Answers2

0

Just make setOnCheckedChangeListener on your RadioGroup:

RadioGroup radG = (RadioGroup) findViewById(R.id.yourRadioGroup);        
radG.setOnCheckedChangeListener(new OnCheckedChangeListener() 
{
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        // checkedId is the RadioButton selected
        switch(checkedId) 
        {
        case R.id.hexa:     
        //enable or disable button
        break;

        case R.id.normal: 
        //enable or disable button        
        break;

        case R.id.other:  
        //enable or disable button       
        break;
        }
    }
});
Fady Saad
  • 1,169
  • 8
  • 13
0

It's simply importing the required directories then JRadioButton is being initialized as "NULL" for on and off and then just added to ButtonGroup!

Import Javax.swing.Jframe;
Private JRadioButton rdbtnOn=null;
Private JRadioButton rdbtnOff=null;
Private ButtonGroup bg = new ButtonGroup();
bg.add(rdbtnOn);
bg.add(rdbtnOFF);