简体   繁体   中英

Unable to set background color of checkbox at runtime

There is a class AgentHome which extends JFrame. AgentHome has a JPanel rem_panel. Checkboxes are added dynamically into rem_panel…number of checkboxes depending on the number of entries in the database table from where the text to be displayed by the textboxes are read.

AgentHome has an integer variable x and a checkbox arraylist rem_cbarr.

rem_cbarr stores the checkboxes as they are created and added to rem_panel. I am trying to set the background color of these checkboxes to red when the variable x is set to 1 as the program executes. I have implemented the TickerBehaviour of JADE framework to check if the variable x is set to 1.

I am unable to set the background color of the checkboxes to red. This is the code I have implemented. Please help. Thanks.

 public void setup()
{
  Behaviour loop = new TickerBehaviour( this, 2000 )
  {
     protected void onTick() {

      timer();
     }
  };


   addBehaviour( loop );
 }

  public void timer()
{
    AgentHome hm=new AgentHome();
           if(hm.x==1)
       {
           for (int i = hm.rem_cbarr.size()-1; i>=0; i--)
                   {
                       JCheckBox cb=hm.rem_cbarr.get(i);
                     cb.setBackground(Color.red);
                      hm.rem_panel.revalidate();
                     hm.rem_panel.repaint();
                   }
      }
}

GUI operations need to be done on the EDT (Event Dispatcher Thread). In java this happens by calling SwingUtilities.invokeLater(Runnable run) .

A number of things...

  • UI components should only ever be updated within the context of the Event Dispatching Thread
  • You should never perform any action which might block the Event Dispatching Thread (like using loops or Thread#Sleep to try and update the screen)
  • The Event Dispatching Thread is responsible for dispatching paint updates...
  • JCheckBox is transparent by default.

public class FlashCheckBox {

    public static void main(String[] args) {
        new FlashCheckBox();
    }

    public FlashCheckBox() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                frame.add(new FlashyCheckBox());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class FlashyCheckBox extends JCheckBox {

        private final Color defaultBackground;
        private int flash;
        private Timer flashTimer;

        public FlashyCheckBox() {

            defaultBackground = getBackground();

            flashTimer = new Timer(500, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    flash++;
                    if (flash % 5 == 0) {
                        setOpaque(false);
                        setBackground(defaultBackground);
                        flashTimer.stop();
                    } else if (flash % 2 == 0) {
                        setBackground(Color.YELLOW);
                        setOpaque(true);
                    } else {
                        setBackground(defaultBackground);
                        setOpaque(false);
                    }
                    repaint();
                }
            });

            flashTimer.setRepeats(true);
            flashTimer.setCoalesce(true);
            flashTimer.setInitialDelay(0);

            addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    flashTimer.restart();
                }
            });

        }

    }

}

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