Article View: borland.public.jbuilder.java.swing
Article #7805Re: JTable Questions
From: John McGrath TeamB
Date: Sun, 31 Dec 2000 05:56
Date: Sun, 31 Dec 2000 05:56
101 lines
5460 bytes
5460 bytes
On Sat, 30 Dec 2000 13:10:38 -0600, James A. Schulz wrote:
> I need to display a grid (55x25) of characters, and I need
> to control the colors (foreground and background) of each cell.
>
> Question 1: Should I use a table of Strings for this purpose?
> Or would a table of Characters be better?
My first reaction was to use Character objects, since they are only
supposed to hold a single character. But since the renderer will be
doing a toString() on the object whenever it renders it, using a String
to store the character would be more efficient. The toString() method
for Character must construct a new String but the toString method for
String just returns itself.
> If the latter is preferable, how do I initialize my table with
> empty cells?
Whether your cell objects are Strings or Characters or something else,
if value is null, the default renderers will leave the JTable cell
blank. You can do the same in your custom renderer.
How you actually store the objects depends on your table model.
> Or, since I need to control and maintain the colors of each cell,
> would a table of Object (where the object would contain one char,
> and two ints for the colors) be the best choice?
It is hard to say for certain without knowing more about the problem,
but something like that is probably a good choice. However, rather
than think of these objects as objects to be displayed in a table, it
probably makes more sense to think of the object in terms of the
problem domain.
Let us assume that your table is a chess board, so the object to be
displayed in the table cells are chess pieces. You should create a
Piece class to represent a chess piece, so that you can move the pieces
around on the board in a legal fashion. Piece will probably be an
abstract class with subclasses King, Queen, Bishop, etc.
This class will have attributes describing how to move the piece, its
relative strength, and any other attributes you need to play chess. Do
not design the class to display a piece, since how you display the
piece is likely to change, and the rules of chess are not. How a piece
is displayed is not fundamental to what a piece is.
The display functionality belongs in the renderer, so you would create
a PieceRenderer class that knows how to render a Piece. This renderer
would ask the Piece for whatever attributes it needs, and render the
JTable cell accordingly.
Note that it is not especially bad for the Piece to provide some of the
display attributes. For example, a Piece could have DisplayString and
Color attributes, and the renderer would use them in order to render
the Piece. But depending on what you want to do with the program, it
may be better to get that information elsewhere.
For example, if you want to internationalize the program, the letters
"K", "Q", "B", "N", "R", and "p" may not be appropriate. And you may
want to provide alternate rendering mechanism that would not use a
single letter, such as an image. And you might provide a choice of
colors, in case the user does not like black and white.
A good approach to handling this would be to have the Piece provide an
ID string which would be used to look up the display resource (letter,
image, color, etc.).
> Question 2: I know how to replace the DefaultCellRenderer,
> and I have seen several examples which show how to change
> cell colors based on cell value, but I need to control cell
> colors based solely on position in the grid and I do not
> see how, in the custom renderer's code, I can check the row
> and column of the cell being rendered.
There are two methods in JTable you can override to handle this:
The getCellRenderer(row,column) method can be used to specify the
renderer to use, and it can also be used to set attributes of the
renderer based on the row and column.
The prepareRenderer(renderer,row,column) method is what actually gets
the rendering Component from the renderer. So you can override this
method to set attributes in the renderer or the actual component. For
the chess example, you might do something like:
public Component prepareRenderer( TableCellRenderer renderer,
int row, int column )
{
Component c = super.prepareRenderer( renderer, row, column );
c.setBackground( ( ((row+column)%2) == 0 )
? Color.white : Color.black );
return c;
}
--
Regards,
John McGrath [TeamB]
---------------------------------------------------
Before sending me e-mail, please read:
http://www.JPMcGrath.net/newsgroups/e-mail.html
Technical Details
Message-ID:
<MPG.14b89345ccc5b0c7989e08@forums.borland.com>Path:
arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!supernews.com!newsfeed.online.be!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!bornews.borland.com!not-for-mailReferences:
<92lbpr$6ro1@bornews.inprise.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,7fa15b59fdc30449 X-Google-Attributes: gid111d45,public X-Google-ArrivalTime: 2000-12-30 22:06:48 PST Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!supernews.com!newsfeed.online.be!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!bornews.borland.com!not-for-mail From: John McGrath [TeamB] <ng-b@JPMcGrath.net> Newsgroups: borland.public.jbuilder.java.swing Subject: Re: JTable Questions Date: Sun, 31 Dec 2000 00:56:32 -0500 Organization: Another Netscape Collabra Server User Lines: 102 Message-ID: <MPG.14b89345ccc5b0c7989e08@forums.borland.com> References: <92lbpr$6ro1@bornews.inprise.com> NNTP-Posting-Host: dslcv1-60.fast.net X-Newsreader: MicroPlanet Gravity v2.30