0

I stumbled upon a problem with my program.

What it does: Listens for a row selection in a JTable, uses 'getSelectedRow' to fetch the content of a cell, like so:

 public void valueChanged(ListSelectionEvent e) {   

    int row = resultTable.getSelectedRow();
    String val = resultTable.getValueAt(row,2).toString();

    System.out.println("The value of cell 2 at selected row is: " + val);

 }

What is not shown in the above example is that cell 2 of the JTable contains local paths to images, these paths are fetched from a locally run MySQL Database.

What I want it to do: I wish to use the paths contained in cell 2 of my JTable to draw and display the image in an external JFrame whenever valueChanged(displayed above) is triggered.

Taking the above into consideration (and the fact that I'm fairly new at this), how would I go about passing this 'val' to an ImageIO.read (that I assume will be running in a seperate class?)

Thank you for your time, any help is appriciated!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Bittein
  • 31
  • 1
  • 6

1 Answers1

2

You don't really need ImageIO for JPEG images. Just add this instead of your System.out.println:

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JLabel label = new JLabel(new ImageIcon(val));
frame.setContentPane(label);
frame.pack();
frame.setVisible(true); 

If the val parameter contains a valid path, then the image will be displayed.

If the image is opened more than once, check the value of getValueIsAdjusting() for the ListSelectionEvent

mavroprovato
  • 8,023
  • 5
  • 37
  • 52
  • Thank you for your reply, I will test it straight away. A follow up question though, the default path (if I were to put only the filename instead of a complete path) would be the same as the home dir of the .java file it's run from, right? – Bittein May 23 '12 at 13:24
  • I don't really understand what you mean by default path. The files will be wherever you store them. If all files are in the same directory, you could put that directory in a configuration file and append this path to val inside valueChanged. I think it would be better if you stored the images as CLOBs in the database, google for CLOB if you don't know what it is. – mavroprovato May 23 '12 at 13:39
  • Oh Sorry if I overcomplicated my question, what I meant to ask was, if 'val' is 'example.jpg' where should 'example.jpg' be placed to be displayed? – Bittein May 23 '12 at 13:41
  • Disregard from that question, I found out myself, thank you for your code, It works exactly as I wanted it to with the exception that it opens the image twice (however that's related to my previous code) – Bittein May 23 '12 at 13:52
  • If you are sure that the db server and the client will always be installed on the same machine, then you can put the files wherever you like, add this directory as a configuration parameter of your application and append it to the file name. If there are not in the same machine (a more realistic scenario if you want to support multiple clients), then either store the images as CLOBs (so the image is stored in the DB) or use some network storage and use the same approach as before (configurable parameter for the path) – mavroprovato May 23 '12 at 13:54
  • I intend to eventually implement BLOBS or CLOBS, but at this stage it is just 'learning by experimenting' thank you for all the valuable information, now I just have to find out why my eventhandler is triggered twice! – Bittein May 23 '12 at 13:59
  • Thanks for the edit! When you say check, you mean test if it returns true or false right? again, sorry I'm new to this – Bittein May 23 '12 at 14:05
  • `if (! e.getValueIsAdjusting())` did the job, thanks for everything mavroprovato! – Bittein May 23 '12 at 14:07