0

I am trying to open the image file whenever user double clicks on the selected row. I tried this code but its not working.

tab = new JTable(rows, column);
tab.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
tab.addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
        if (e.getClickCount() == 2) {

            int selectedRow = tab.getSelectedRow();

            try {
                String file = rows[0][2];
                String path = "C:\\XX\\YYY\\Gallery\\" + file;
                JLabel fileLable = new JLabel();
                fileLable.setBounds(500, 600, 300, 300);
                fileLable.setIcon(new ImageIcon(path));
                pan.add(fileLable);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }
});
Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159
  • 1
    Please describe what is not working. The file is not loaded ? Is an exception thrown ? The mouseClicked does not get invoked ? –  Mar 30 '17 at 11:57
  • tab.getSelectedRow() returning -1 –  Mar 30 '17 at 11:58
  • you may refer the below link, hope you would find what you need. http://stackoverflow.com/questions/14829005/displaying-image-in-jtable – Thomson Ignesious Mar 30 '17 at 12:07
  • @Raj if for some reasons `tab.getSelectedRow()` returning -1 you can try `tab.rowAtPoint(e.getPoint());`. But I think, you have another problem, because the method `getSelectedRow()` in your case should also return a correct value. – Sergiy Medvynskyy Mar 30 '17 at 12:17
  • Oh thank you now its working(using rowAtPoint(e.getPoint())).Do no how. –  Mar 30 '17 at 15:12

0 Answers0