Home » Java » MouseListener inside loop isnt using updated variable values

MouseListener inside loop isnt using updated variable values

Posted by: admin October 29, 2018 Leave a comment

Questions:

i want to first apologize for my poor english.
I made some JPanels wich im putting inside one JPanel and this last JPanel im putting it in one JScrollPane, each Jpanel have some JLabels that have text inside em, 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 differents but on the console im 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();
    }
}
Answers: