2

Introduction:

I am trying to show all files in a folder in a grid view, when clicked on the file, I should print out that name on the command line.

I have jpanels for all the files, so I am trying to detect click on the jpanel and assign action to that panel, in a loop.

My problem,

When using the for loop, I have variable "name", which keeps the name of file, I am trying to add mouse listener like this,

panel[i].addMouseListener(new MouseAdapter(){
    public void mouseClicked(MouseEvent e){
        System.out.println("MOUSE_RELEASED_EVENT:for "+name);
    }
});

As you can notice, I am iterating over i, adding mouselisteners to all panels thus, but the problem is the variable name itself is being used in the mouselisteners.

Example

If what written above is not the way to explain it, I mean say I have two files, A and B.

What I am looking for is, mouse listener for first is,

System.out.println("MOUSE_RELEASED_EVENT:for A"); //Since name="A" here.

For second file is,

System.out.println("MOUSE_RELEASED_EVENT:for B");  //Since name="B" here.

But it doesn't happen this way, variable name is itself attached to the mouse listeners, and what I get always is the final value of name, in my example "B" always. Since it was the last value of name in the loop.

How do I solve this problem?

As an extra, can someone tell if I can fix size in BorderLayout for child panels, they get resized no matter what I use, setSize, setPreferredSize, setMaximumSize, setMinimumSize.

Thanks for time and efforts.

mKorbel
  • 109,525
  • 20
  • 134
  • 319

3 Answers3

2

You can use JPanel's setName()/getName() methods storing the variable there.

Then in the listener just use ((JPanel)event.getSource()).getName();

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • Thanks. +1. Added new code as an answer here for future references, which ofcourse is derived from your answer. –  Aug 12 '13 at 14:06
1

Alternatively, make a GridLayout of JButton instances, with each button having a suitable Action, as shown here. Each Action can hold a reference to its own File.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanks, I got the answer, but in a jButton too, wont I use setactionCommand(name), and then the same problem would occur? name will always hold the last value in the loop. And I am still using gridlayout, for all child panels. –  Aug 12 '13 at 14:08
  • 1
    No, each `Action` can see its own `File` – trashgod Aug 12 '13 at 14:13
  • Okay. Thanks, however the problem solved, any suggestion for the sizing of panel, BorderLayout.CENTER holds the panels, so all are getting resized, if I only have two files, it gives a bad view.:( –  Aug 12 '13 at 14:18
  • `JList` makes a nice icon view, as shown [here](http://stackoverflow.com/a/13754697/230513); also consider `putClientProperty()` if more that a name is needed. – trashgod Aug 12 '13 at 14:22
0

For future references, my final code, as suggested by StanislavL, I first set the name for the jpanel,

and then retrieve it when clicked, and works fine.

    panel[i].setName(name);
panel[i].addMouseListener(new MouseAdapter(){
    public void mouseClicked(MouseEvent e){
        System.out.println("MOUSE_RELEASED_EVENT:for "+ ((JPanel)e.getSource()).getName());
    }
});