javaswingmouselistener

MouseListener inside loop isnt using updated variable values


I want to first apologize for my poor English. I made some JPanels which I'm putting inside one JPanel and this last JPanel I'm putting it in one JScrollPane, each Jpanel has some JLabels that have text inside them. I've done that and everything is working until now, then I added a mouseListener to System.out.print the text inside the JLabels. The texts are different, but on the console I'm getting the same text, here's my code:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package testa;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout; 
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import java.awt.event.*;

public class Test extends JFrame{ //see https://www.javatpoint.com/java-naming-conventions

    private JLabel[] titles;
    private JLabel[] descriptions;
    private JPanel [] panels;
    private JScrollPane jScrollPane1;
    private JPanel bigPanel;
    private final static int NUM_OF_RESULTS =10;
String identifier ;
String title;
String authors;
String resume;
String references;
    public Test() throws IOException {
        
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(1000,500);            
        //jScrollPane1.setSize(1000, 500);    and layouts. see following comments

        bigPanel = new JPanel();
        bigPanel.setBounds(30, 90, 950, 400);
        //set layout to
        GridLayout layout = new GridLayout(NUM_OF_RESULTS, 0);
        bigPanel.setLayout(layout);
        jScrollPane1 = new JScrollPane(bigPanel);
        jScrollPane1.setBounds(30, 90, 950, 400);
        getContentPane().add(jScrollPane1);

        requetezQuery();
        //pack(); //see https://stackoverflow.com/questions/22982295/what-does-pack-do
        setVisible(true); //set visible typically comes last
    }
    public void requetezQuery() {
            
int actual=0;


     

        
        
        titles = new JLabel[NUM_OF_RESULTS];
        descriptions = new JLabel[NUM_OF_RESULTS];
        panels = new JPanel[NUM_OF_RESULTS];

        for(int i = 0; i<NUM_OF_RESULTS; i++){
                       
                         title = "Title "+i;
                      resume = "Description "+i ;

            titles[i]= new JLabel();
            descriptions[i]= new JLabel();
            panels[i]= new JPanel();
            panels[i].setPreferredSize(new Dimension(250, 50));
            panels[i].setLayout(new FlowLayout()); //FlowLayout is default for JPanel
            titles[i].setText(title);
            
            descriptions[i].setText(resume.substring(0, Math.min(resume.length(), 100))+"...");
            titles[i].setForeground(Color.blue);
            descriptions[i].setForeground(Color.black);
            titles[i].addMouseListener(new MouseAdapter() { 
          public void mouseClicked(MouseEvent me) { 
            System.out.println(title);
          } 
        });
            panels[i].add(titles[i]);
            panels[i].add(descriptions[i]);
            bigPanel.add(panels[i],i, 0);
             
        }
        
    }
    

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

Solution

  • I believe your reference to title refers to the last created title at runtime. What you can do to fix this is instead of printing the title, print the labels text. Try putting this inside of your mouseClicked method.

    JLabel obj = (JLabel) me.getSource();
    System.out.println(obj.getText());