Question

I am adding some JLabels to a panel dynamically. When user clicks on the Label it should return the text it contains. I am trying the codes below:

 private JLabel l;
    private JPanel A;

    private void LoadLabels(){
        A.setLayout(new java.awt.GridBagLayout());
            java.awt.GridBagConstraints gbc;
            gbc = new java.awt.GridBagConstraints();
            gbc.fill = java.awt.GridBagConstraints.BOTH;
            gbc.anchor = java.awt.GridBagConstraints.CENTER;
            gbc.weighty = 0;
            gbc.ipadx = 10;
            gbc.ipady = 10;
            gbc.insets = new java.awt.Insets(1, 1, 1, 1);

            for(int gx = 0; gx<15; gx++){
            JLabel Sold = label(String.valueOf(gx+1));
            Sold.setBackground(Color.yellow);
            gbc.gridx = gx;
            gbc.gridy = 0;
            gbc.weightx = .5;
            A.add(Sold, gbc);
    }      

    // JLabel is declared here:
    private JLabel label(String x){
            l = new JLabel(x);
            l.setFont(new java.awt.Font("Tahoma", 0, 16));
            l.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
            l.setOpaque(true);
            l.setBorder(javax.swing.BorderFactory.createEtchedBorder());

            l.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));

            l.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
            lMouseClicked(evt);
            }
            });
            return l;
        }

    //Now I am writing mouseClick event like this:
    private void lMouseClicked(java.awt.event.MouseEvent evt) {                                     
    if(evt.getSource()==l){
    System.out.println(l.getText());
    } 

Why this mouse event always return the last JLabel Text? (output is always 14) How can I get the actual value of every JLabel ? Please help.

Was it helpful?

Solution

Your private JLabel l is pointing to the last JLabel added and it will return the text in the last label only.Try creating an array and define reference for each and every label created.

Found a lot of mistakes in your code,follow proper naming conventions . Just run this code and check ,just create reference for each and every JLabel created.

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Color;


class Test { 
    private int i;

       private JPanel A;
       private JFrame f;
       private JLabel []l;

       Test(){
           f=new JFrame();
           A=new JPanel();
           f.setVisible(true);
           f.setSize(800, 800);
           f.setLayout(null);
           f.add(A);

           LoadLabels();
       }

    private void LoadLabels(){
         A.setLayout(new java.awt.GridBagLayout());

             l=new JLabel[15];
         for(int gx = 0; gx<15; gx++){
                     l[gx]= label(String.valueOf(gx+1));
                     l[gx].setBackground(Color.yellow);
                     l[gx].setBounds(30*gx, 30, 30, 20);
                     f.add(l[gx]);
         }             

     }      

    // JLabel is declared here:
       private JLabel label(String x){
            final JLabel l;
               l = new JLabel(x);
               l.setFont(new java.awt.Font("Tahoma", 0, 16));
               l.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
               l.setOpaque(true);
               l.setBorder(javax.swing.BorderFactory.createEtchedBorder());

               l.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));

               l.addMouseListener(new java.awt.event.MouseAdapter() {
               @Override
               public void mouseClicked(java.awt.event.MouseEvent evt) {

                   System.out.println(l.getText());
               }
               });
               return l;
           }

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

}

(or)

you can do one more thing,

Just replace the below method in your code

 private void lMouseClicked(java.awt.event.MouseEvent evt) {                                     
    if(evt.getSource()==l){
    System.out.println(l.getText());
    } 
 }

with

    private void lMouseClicked(java.awt.event.MouseEvent evt) {                                     
    //This will refer to current JLabel being called
    System.out.println(((JLabel)evt.getSource()).getText());

    } 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top