1

I have a class , called boardGUI , it has a list of 64 labels (like a chess board). Every label coresponds to a specific tile on the board.

List<JLabel> labelList = new ArrayList<>();

In another class, I'm trying to set some of this labels opaque, with setOpaque(true) method , whenever I click on one of the labels (inside mouseClicked method).

JLabel l1 = boardGUI.labelList.get(1);
l1.setOpaque(true);

The problem is that although l1 refers the right label in labelList (I checked with the debugger) , it doesn`t make any visual change (on the GUI ).

But, if I'm trying to set opacity of the labels in the boardGUI class , it's working.

for (int i=0;i<64;i++)
   labelList.get(i).setOpaque(true);

Where can the problem be?

here is the class where I'm trying to apply the changes :

public class Controller {

    private Board board = new Board();
    private BoardGUI boardGUI = new BoardGUI();


    public Controller () {
        boardGUI.setVisible(true);
        boardGUI.addLabelListener(new LabelListener());

    }

    class LabelListener implements MouseListener{


        @Override
        public void mouseClicked(MouseEvent arg0) {
            JLabel l1 = boardGUI.labelList.get(1);
            l1.setOpaque(true);
        }

BoardGUI class (there's more code , but it's not relevant) :

public class BoardGUI extends JFrame{

    List<JLabel> labelList = new ArrayList<>();

    public BoardGUI() {
        createView();
    }

    public void createView() {
            createLabels(mainPanel);                            
        }
    public void createLabels(JPanel mainPanel) {
        int startX = 100;
        int startY = 87;
        int x = 100;
        int y = 87;
        int j = 0;
        for (int i=0;i<64;i++) {
             JLabel label = new JLabel(); 
             label.setBounds(x , y , 62, 62);
             labelList.add(label);
             mainPanel.add(label);
             if ( (i == 7*(j+1) +j )) {
                 x = startX;
                 y = startY + 62 *( i / 7);
                 j=j+1;
             }
             else {
                 x = x+62;
             }   
        }

    }
biafas
  • 127
  • 7
  • Are you setting the background color of the labels? – JensS Sep 25 '17 at 11:00
  • How did you got the instance of class which is containing the labels? It seems that you might be working on different instances of the class which contains 64 labels. Also your labelList is not static which means that there might be multiple instances of list existing in memory. – Naveed Kamran Sep 25 '17 at 11:00
  • @NaveedKamran . The class where I`m trying to apply the changes to the labels is actually the Controller of the GUI . `private BoardGUI boardGUI = new BoardGUI();` – biafas Sep 25 '17 at 11:08
  • @JensS no, I didn't set the color – biafas Sep 25 '17 at 11:11
  • OK, basically we would need to see more code to see the problem - not too much, just a shortened version of how you set up the GUI.Can you provide that please. – JensS Sep 25 '17 at 11:17
  • @JensS . i edited the main post, with what you asked for. – biafas Sep 25 '17 at 11:19
  • @Tudor first, set labels opaque to false when you create it, second set some backgroung color to each label – Antoniossss Sep 25 '17 at 11:30
  • @Antoniossss. I tried to set the labels opaque to false, but it still doesn't work. Related to the second sugestion, I saw here, on stackoverflow that most of the users use setOpaque, to set the background color. [link] (https://stackoverflow.com/questions/2380314/how-do-i-set-a-jlabels-background-color) – biafas Sep 25 '17 at 11:37
  • Youy have to have background color in order for label to be opaque. Beeing opaque===having background drawn. If there is nothing to draw, setting opaque will change nothing. – Antoniossss Sep 25 '17 at 11:45

1 Answers1

1

You need to set both background color and opaqueness; here's an example to show how these play together:

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new FlowLayout());
    frame.getContentPane().setBackground(Color.GREEN);

    JLabel label1 = new JLabel("label1");
    label1.setBackground(Color.RED);
    label1.setOpaque(false);
    frame.addMouseListener(new MouseListener() {
        @Override
        public void mouseClicked(MouseEvent e) {
            label1.setOpaque(!label1.isOpaque());
            label1.setBackground(label1.getBackground() == Color.RED ? Color.BLUE : Color.RED);
        }
        public void mouseReleased(MouseEvent e) {}
        public void mousePressed(MouseEvent e) {}
        public void mouseExited(MouseEvent e) {}
        public void mouseEntered(MouseEvent e) {}
    });

    frame.add(label1);
    frame.pack();
    frame.setVisible(true);
}

The label is initially transparanet, then changes to BLUE and opaque and back with every MouseClick. So basically, you would need to set the background color together with opaque (the RED color is just to demonstrate that it is never shown as the label is never both opaque and RED).

JensS
  • 1,151
  • 2
  • 13
  • 20
  • thank you. Still one question, how comes that when I try to set opaqueness in BoardGUI class (where I created the labels ) it actually makes the labels opaque, without setting the background color? – biafas Sep 25 '17 at 11:52
  • The default value for opaque is false, but according to the documentation of setOpaque it is "look-and-feel dependent" for most components; so if it is true in your application (without you setting it), it is probably due to a UIManager. – JensS Sep 25 '17 at 12:09