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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Can anyone help with this Label Array in Java

  • 11-12-2011 11:25am
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 Posts: 2,013 ✭✭✭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, Registered Users 2 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