Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setOpaque() method

Tags:

java

I would like to know what setOpaque() method do...

here is a part of small program:

    public class Buttons extends JFrame implements ActionListener
    {

     private JButton button;
     private JLabel label;
     private JTextArea text;
     private String t;
     public Buttons()
     {
      super("TESTING");
      label = new JLabel("Hello!!!!");
      button = new JButton("Color Change");
      text = new JTextArea("Test");
      setLayout(new FlowLayout());
      label.setOpaque(true);
      add(button);
      add(label);
      add(text);

            LabelHandler labelHandler = new LabelHandler();

      button.addActionListener(this);
            label.addMouseListener(labelHandler);

      setSize(300,200);
      setVisible(true);
     }

     public void actionPerformed(ActionEvent e)
     {
      if (e.getSource()==button)
      {
       label.setBackground(Color.red);
      }
      if (e.getSource()==text)
      {
       if (t == "\n")
       {
        setText(t);
        label.getText();
       }
      }
     }



    class LabelHandler extends MouseAdapter
    {
     public void mouseEntered(MouseEvent e)
     {
  label.setBackground(Color.GREEN);
     }
 }

Without the setOpaque it wont paint the label. Why? thanks in advance.

like image 306
firestruq Avatar asked Dec 23 '22 01:12

firestruq


1 Answers

The opaque flag is used by the Swing ComponentUI to test whether they should paint their background or whether they should not. If you set your background color, but fail to setOpaque(true), you will not see that bg color.

like image 68
akf Avatar answered Dec 28 '22 11:12

akf