Question

I have a Gridlayout filled with images loaded from File and I want to add a different, for example, popup, on mouseEvent mouseClicked for each image. How do I determine which image I'm clicking? I've tried adding GetComponentAt(Point), but Eclipse keeps showing that as an unidentified method for the mouseAdapter, and how do I determine the fields for the if statement?

This is what I have:

public class testclass implements ItemListener  {

JPanel template;
final static String title = "Title";

public void testclass (Container window){

    JPanel index = new JPanel();

    String index2[] = {title};
    JComboBox index3 = new JComboBox(index2);
    index3.setEditable(false);
    index3.addItemListener(this);
    index.add(index3);

     File folder = new File("images/");
        File[] listOfFiles = folder.listFiles();

        String nr;
        final JPanel panel = new JPanel(new GridLayout(4, 4, 4, 4));

        for (int i = 0; i < listOfFiles.length; i++) {
                nr = "images/" + listOfFiles[i].getName();
                final ImageIcon images = new ImageIcon(nr);
                final JLabel display[] = new JLabel[1];

            for (int n = 0; n < 1; n++){
                display[n] = new JLabel(images);
                panel.add(display[n]);  
         } }
            panel.addMouseListener(new MouseAdapter()

            { public void mouseClicked (MouseEvent e)
            {   //JPanel panel = (JPanel) getComponentAt(e.getPoint());
                JOptionPane.showMessageDialog(null, "Message");
            }}); 


    template = new JPanel(new CardLayout());
    template.add(panel, title);

    window.add(index, BorderLayout.PAGE_START);
    window.add(template, BorderLayout.CENTER);
}
public void itemStateChanged(ItemEvent event){

    CardLayout change = (CardLayout)(template.getLayout());
    change.show(template, (String)event.getItem());
}

private static void userinterface() {

    JFrame showwindow = new JFrame("Window");
    showwindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    testclass show = new testclass();
    show.testclass(showwindow.getContentPane());

    showwindow.pack();
    showwindow.setVisible(true);
}

public static void main(String[] args) {
    try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
          } catch(Exception e){
          }
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            userinterface();
        }
    });
}

}

Was it helpful?

Solution

Edit: Proper Answer

Thankyou for providing code. I have adjusted some things so hopefully you can understand them.

  • Firstly I have added a new object, ImageButton. From when I added the actionListener the functionality you wanted was already done (minus the good looks, you will have to play around with that, or ask another question)
  • Added a 'dir' string so that you don't have to keep copy and pasting the directory address.
  • I had to adjust the size of the window through the userInterface() method, you should look at being able to shorten the sequence of being able to adjust the parameters, maybe another GUI object to keep all that information together.

A couple of things:

The code you wrote was good but it would need for you to adjust alot of (getting the size of windows and repeatedly check for different sizes if you adjusted the window size where a mouse click could click on other image that you don't want!) in order for a mouseListener to work, I am guessing with a wide range of images you would be providing.

Putting comments in your code can help both you and someone trying to help you.

Anyways, please upvote/accept answer if this helps out, which I'm sure it does.

Good Luck!


Put these files into your eclipse and run them it should work if you adjust the dir string to follow your original directory path.

testclass.java

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;

public class testclass implements ItemListener {

JPanel template;
final static String title = "Title";
final String dir = "images/";

public void testclass(Container window) {

    JPanel index = new JPanel();

    String[] index2 = { title };
    JComboBox index3 = new JComboBox(index2);
    index3.setEditable(false);
    index3.addItemListener(this);
    index.add(index3);
    index.setSize(500, 500);

    File folder = new File(dir);
    File[] listOfFiles = folder.listFiles();

    String nr;
    final JPanel panel = new JPanel(new GridLayout(4, 4, 4, 4));

    for (int i = 0; i < listOfFiles.length; i++) {
        nr = dir + listOfFiles[i].getName();

        panel.add(new ImageButton(nr));

    }

    template = new JPanel(new CardLayout());
    template.add(panel, title);

    window.add(template, BorderLayout.CENTER);
    window.add(index, BorderLayout.NORTH);

    window.setVisible(true);
}

public void itemStateChanged(ItemEvent event) {

    CardLayout change = (CardLayout) (template.getLayout());
    change.show(template, (String) event.getItem());
}

private static void userinterface() {

    JFrame showwindow = new JFrame("Window");
    showwindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    testclass show = new testclass();
    show.testclass(showwindow.getContentPane());

    showwindow.pack();
    showwindow.setVisible(true);
    showwindow.setSize(500, 500);
}

public static void main(String[] args) {
    try {
        UIManager
                .setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    } catch (Exception e) {
    }
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            userinterface();
        }
    });
}
}

ImageButton.java

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;

public class ImageButton extends JButton {

private String fileName;
private ImageIcon image;
private JButton button;

public ImageButton(String fileName) {

    setFileName(fileName);
    setImage(new ImageIcon(fileName));
    setButton(new JButton(getImage()));

    this.setIcon(getImage());

    this.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent ae) {

            JOptionPane.showMessageDialog(null, ae.getSource());
        }
    });

}

public String getFileName() {
    return fileName;
}

public void setFileName(String fileName) {
    this.fileName = fileName;
}

public ImageIcon getImage() {
    return image;
}

public void setImage(ImageIcon image) {
    this.image = image;
}

public JButton getButton() {
    return button;
}

public void setButton(JButton button) {
    this.button = button;
}

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