This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TableModelEvent question: fires AFTER user has navigated away from cell, not what I need

 
J Westland
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello -

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.

 
Rob Camick
Ranch Hand
Posts: 2542
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
J Westland
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah makes sense, don't use the table but use the component that displays the image to "listen" and open the JFileChooser.

That will work, thank you
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic