• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 182
  • Last Modified:

Swing GUI - Image contain id# of database element

Okay, I wasn't sure how to title my question but I think that one fits.  What I am trying to do is query a database (Oracle) and select the id#, first name and last name of users.  I can do this with a simple SQL statement.  I then want to display all the current contacts in the list but first and last name (alphabetically by last name) and beside those names there are icons which say View, Edit, Delete.


Bill Adams    VIEW  EDIT  DELETE
John Doe      VIEW  EDIT  DELETE
Bob Zee       VIEW  EDIT  DELETE

I understand how to access the names and know how to print the data off into a normal terminal screen by using a very basic while loop.

while(resultSet.next()) {
     System.out.println(resultSet.getString(1) + " " +
and that prints the names just fine.  I believe I have an idea on how to do that with the GUI as well but haven't fully tested it out.  What I need to know is how do I set it up so that the same 3 images appear for all the names but so that each image is uniquely identified to the name it appears beside.  I was thinking of doing this with the id# but I know that images cannot hold this other information.  Another idea I had was to store the images in the database along with the names but this would be an extreme waste of space because all rows in the database would contain the same image.
I need it so that if I click on VIEW for instance for the name John Doe I would get all of the information from the database that is listed under John Doe.

Any help would be great.  Thanks.

Let me know if I didn't make any sense I'll try to explain better.
  • 2
1 Solution
You did make sense :P

In GUI if you want to display the results there are many options.

You could try printing the data in a JTable. SO basically a JTable would contain 6 columns like:
in the first 3 columns you can get the results from the query, and for the last 3 you can display the images. Then you can add a mouse listener to the JTable and see on which row the mouse was cliked. then from that row get the value from column 1 that is the, then you can simple query the database again like select * from table where id=value;
To accomplish this you would be using you own tableModels, and cellRenderers/

You could display the result in a JList. After you query the database you can create a local record of the results return, for eg
class Users {
  private int id;
  private firsName, lastName;
  public Users(int id, String fn, String ln) {
    this.id = id;
    firstName = fn;
    lastName = ln;

  // create setters and getters;

  public String toString() {
    return lastName + ", " + firstName;

then you you can add these Users to the JList and can have only three JButtons with the respective buttons and their own listeners. On their actionListeners you can see which record is currently selected in the JList, get that User and getID() of that user and do you sql statements again.

I hope you get the picture.
just a small suggestion ... you'll have to figure out the implementation urself ... i think its possible

What you can do is use JButtons and set the images as the background of the JButton. You mite have to extend the JButton class and tweak around a bit. If you manage to get the JButtons working ... your problem is solved.

One advantage of this JButtong approach is that you will be using fewer objects ... if you can have the background image as a static Image object .... there will always be just one Image object irrespective of the number of JButtons.

See if that can be implemented.
Cujo_34Author Commented:
Hey Glottis,

Thanks for the answer.  I've been coding on and off and have it functioning okay I guess.  Having troubles with getting the images to be displayed in columns but that's secondary now.  I'm just going to finish the rest of the codin and if time permits add the pictures (currently just using words).

I used the JTable method in case you were wondering.  It was the first one I saw so I thought I'd try it first.
After you have used JTables, you would find using JList easy too, because their functionality in customizing works sort of the same.

and your welcome :)

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now