🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #9402

Re: problems displaying an n x m table of images

#9402
From: John P. McGrath
Date: Tue, 11 Apr 2000 00:00
69 lines
3489 bytes
On Sun, 09 Apr 2000 15:44:51 +0200, Cees Pieters wrote:

> I want to display an n x m matrix with GIF or JPG icons, which
> I must be able to change.  I thought that I could use a JTable
> component to achive this, but I keep drawing a blank.
    :
>           public Object getValueAt(int row, int col)
>           {
>             return new ImageIcon("table.jpg");
>           }
    :
> The eventual picture does display a string reference:
> "javax.swing.ImageIcon@......."

The default class for all cells in a JTable is String, so it is 
converting your Icon to a string using its toString() method.

You can specify the class for one column with the getColumnClass() 
TableModel method.  Since all of your cells are Icons, you can just 
ignore the 'column' argument:

    public Class getColumnClass( int column ) {
        return Icon.class;
    }

> Another problem is, that I don't seem to get any methods to 
> change the images with this construction.

Managing the data is the job of the TableModel.  It needs to keep track 
of the number of rows and columns, and the values in the cells.

DefaultTableModel has methods to do a lot of this work for you, and may 
be the appropriate subclass for you to use.  But you overrode the methods 
that access the data, so you are not really using the functionality of 
DefaultTableModel.

These methods are commonly overridden when you extend AbstractTableModel, 
which does not provide the functionality for managing the data, but does 
provide support for managing the TableModelListeners.

If you want to extend AbstractTableModel, you would use the methods you 
have defined.  You would then add the mechanisms to store the icons and 
to retrieve them from the data structure of your choice.  When you change 
the value of a cell, you would need to call fireTableCellUpdated(row,col) 
to notify listeners (i.e. the JTable) of the change.

You may want to use DefaultTableModel, which provides an implementation 
of TableModel based on a Vector of Vectors.  In that case, the easiest 
way to initialize the data would be to pass a two dimensional array of 
icons into the constructor.  Then you can modify the value of a cell 
using setValueAt(value,row,col).  With DefaultTableModel, you probably do 
not want to override getValueAt(), getRowCount(), or getColumnCount().

>           public Object getValueAt(int row, int col)
>           {
>             return new ImageIcon("table.jpg");
>           }

One more note:  If you extend AbstractTableModel and provide your own 
getValueAt() method, you probably do not want to be doing any significant 
amount of work (such as creating an ImageIcon) every time this method is 
called.  It gets called quite often (whenever the JTable has to repaint a  
cell), so it would be best to compute the data ahead of time, or "lazily" 
create the data so that it only has to be created once.

-- 
John P. McGrath
<JPMcGrath@fast.net>
<jpm@nscs.fast.net>
Technical Details
Message-ID: <MPG.135c741880fe6e0f98986b@forums.borland.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <38F08953.C45FE8EA@wxs.nl>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,4ff159c2976a8b73
X-Google-Attributes: gid111d45,public
From: John P. McGrath <JPMcGrath@fast.net>
Subject: Re: problems displaying an n x m table of images
Date: 2000/04/11
Message-ID: <MPG.135c741880fe6e0f98986b@forums.borland.com>
X-Deja-AN: 609409853
References: <38F08953.C45FE8EA@wxs.nl>
Organization: Another Netscape Collabra Server User
Newsgroups: borland.public.jbuilder.java.swing