Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Can anyone help with this Label Array in Java

Options
  • 11-12-2011 12:25pm
    #1
    Registered Users Posts: 266 ✭✭


    I am trying to display a red image within a label when the mouse is clicked on that label in a gridlayout. All my other code is ok i.e. MouseListeners etc

    The only problems I'm having is with the array. Only one label is active and that is the last label in the array.
    The array which is public JLabel colorLabel[] = new JLabel[30]; is declared as global.

    I also declared a label colorBox: public JLabel colorBox = new JLabel();

    This is the grid layout: gridPanel.setLayout(new GridLayout(5,6));

    This is the array which I have called within the constructor:
    public void labelArray(){
    for (int i = 0; i < colorLabel.length; i++)
    {
    colorBox = new JLabel(" ");
    colorBox.setHorizontalAlignment(JLabel.CENTER);
    colorBox.setBorder(new LineBorder(Color.BLUE));
    gridPanel.add(colorBox);
    colorBox.addMouseListener(this);
    }
    }


    I've attached an image of my result.
    I believe I should be doing something with the index i but I cant seem to assign integer values to labels.

    Any help on this is appreciated as I'm two days at this impass.


Comments

  • Registered Users Posts: 1,994 ✭✭✭lynchie


    Its been a while since I used swing.. but set the name of each label in the loop to the index. Then add each label to the array at the end of the loop.

    Now all your labels are in the array. In your event handler you can query the name property to find out which label was clicked and do whatever you need then.


  • Registered Users Posts: 3,078 ✭✭✭onemorechance


    public JLabel colorLabel[] = new JLabel[30];
    

    This creates an array of 30 labels, colorLabel[0-29].
    public JLabel colorBox = new JLabel();
    

    This creates a single label, colorBox.
    public void labelArray(){
    for (int i = 0; i < colorLabel.length; i++)
    {	
    colorBox = new JLabel(" ");
    colorBox.setHorizontalAlignment(JLabel.CENTER);
    colorBox.setBorder(new LineBorder(Color.BLUE));
    gridPanel.add(colorBox);
    colorBox.addMouseListener(this);
    }
    }
    

    This creates a new and assigns a new JLabel object to the already created colorBox, 30 times.

    I think you mean to be adding the mouse listener to the colorLabel label array:
    colorLabel[i].addMouseListener(this);
    

    You only need to set up the colorBox once, not 30 times as you are, so no need to do it in a loop.

    Add a mouse listener to the 30 colorLabels and move the position of the colorBox label when one colorLabel is clicked on.


Advertisement