简体   繁体   中英

How to make a jButton change it's color when all the options are selected?

Hello everyone :) I want my JButton to change it's color when all the options are selected but I don't know how...

I have a ButtonGroup for 6 Radio Buttons, 6 Check Boxes, and a Text Field

When a radio button is selected AND one or more check boxes are selected AND the text field has a number in it (or anything), the JButton changes it's color to green. I don't know where to put the action listener or how to begin, so if anyone is nice enough to help me, I would be very grateful.

Ok, you can find here some of the code, as you asked, but I don't think it's relevant. Essentially, this is a continuation of my previous question.

If you simply have a method that adjust the color of the button based on the state of the buttons and the text field, eg

public void updateButtonColor(){
  if ( isAtLeastOneRadioButtonSelected() && isAtLeastOneCheckBoxSelected() && 
       textFieldContainsNumber() ){
      changeButtonColor( Color.green );
   } else {
      changeButtonColor( Color.red );
   }
}

you can add listeners to each of the radio buttons, check boxes and text field to call that updateButtonColor method when the relevant state of the component changes (eg an ItemListener to the JCheckBox to detect changes in selection)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM