This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Another day in Java land. I am working on a little Swing/SQL application to hone my skills. It is for keeping data and pictures of chickens (I keep chickens)
It loads the data from the MySQL into a table using DefaultTableModel (will implement my own model later, on the ToDo list)
Now users can also show a picture of the chicken as filepaths are very annoying to type, I want to use a JFileChooser so users can select the picture, then save path into DB.
So I "bound" a TableModelListener to the table, once a TMEvent fires I ask it to check for the row/column if these match the picture cell (selection of course set to single_selection) it starts the JFileChooser. However, this only starts AFTER a users has navigated away or hit "enter" in the table text cell that holds picture data. I want it to start right away, "on click".
Is there a simple way to do what I want, or will it be Swings and roundabouts? If there's a class/interface or combination thereof, just say which ones to use and I will try to figure out the rest myself.
Thanks for reading
See code below.
Woohoo passed SCJP 1.6, that's the theory exam passed now for the practice ;)
If you are saying that the user can select any image from anywhere on the file system, then using a JFileChooser may be the best approach. However instead of using a TableModelListener you will need to use a "custom editor" to display the file chooser. Read the JTable API and follow the link to the Swing tutorial where you will find an example that display a JColorChooser as the editor. You should be able to customize the code for the file chooser.
If you are limiting the user to select an image from a given directory, it may be easier to use a combo box as the editor and to preload all the images into the combo box. Then the user can just select the image from the combo box.
Joined: Apr 06, 2009
Ah makes sense, don't use the table but use the component that displays the image to "listen" and open the JFileChooser.