Showing a picture in a GUI
I'm doing a lending register inwhich different types of objects (books, cd's and films) are represented. I have a GUI consisting of a AbstractTableModel with an ArrayList in it. The different classes (books etc.) all inherit from a class "Item" I've written. When the objects in the list are shown in teh GUI, I want different pictures to appear in the cells, depending of what kind of objet it is. (the first row of the table should show pictures). The classes all have the method getType, and I've written something like:
PHP Code:
public Object getValueAt(int row, int col){
Item i = ItemsTable.get(row);
switch (col){
case 0: return i.getType();
case 1: return i.getTitle();
case 2: return i.getOriginator();
case 3: return i.getPublicationYear();
case 5: return i.islended();
default: return null;
}
}
I've seen something like a method (i("randomfilename.jpg") ), and i'm trying to use that method, but how do I make it appear in the GUI? i.e what code would do that(in the switch-statement above)?
Re: Showing a picture in a GUI
You first need references to the images (probably in the form of an ImageIcon). You can then define a custom cell renderer for the column in the table you wish to have images in and do the image setting from there. See How to Use Tables (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components) and also google "images in JTable" and you'll pull up a number of examples.