Article View: borland.public.jbuilder.java.swing
Article #8966Re: Buttons in JTable
From: John P. McGrath
Date: Sat, 17 Jun 2000 00:00
Date: Sat, 17 Jun 2000 00:00
36 lines
2102 bytes
2102 bytes
On Fri, 16 Jun 2000 10:38:14 +0200, Andreas Schnell wrote:
> I'd like to have some Buttons in front of each line of a
> JTable. The Buttons are for editing or deleting the line.
> I created a button renderer with the buttons, where each
> of them has an ActionListener. The Button looks good, but
> I couldn't get any event, when clicking on them.
In order to understand what is happening, you have to understand how a
JTable works. A table can have a fairly large number of cells. If the
JTable created a separate component for each cell, that could take up
quite a bit of resources. A 100x100 table is not too unreasonable, and
that would 10,000 cells, which would consume megabytes of memory. So a
JTable (and a JTree) does its painting using Cell Renderers.
When a cell needs to be painted, it asks the cell renderer to provide it
with a component to use to render the cell. Then, it creates a graphics
context that points to the cell position and asks the renderer component
to paint itself on that graphics context. In essence, it is virtually
placing the component in the cell position for the duration of the paint
operation.
So you can see that adding a listener to a cell renderer component will
not work, because the component is not in the cell. If you click on the
cell, the JTable will get the event, not the cell renderer component.
But there are ways to accomplish what you want. The following site has
many examples of doing strange things with JTables, including "placing"
buttons in cells.
http://www2.gol.com/users/tame/
--
John P. McGrath
<JPMcGrath@fast.net>
<jpm@nscs.fast.net>
Technical Details
Message-ID:
<MPG.13b4ca55ff6c6cb39899d5@forums.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<3949e5cb@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,91e2d22682262c2e X-Google-Attributes: gid111d45,public From: John P. McGrath <JPMcGrath@fast.net> Subject: Re: Buttons in JTable Date: 2000/06/17 Message-ID: <MPG.13b4ca55ff6c6cb39899d5@forums.borland.com> X-Deja-AN: 635608158 References: <3949e5cb@dnews> Organization: Another Netscape Collabra Server User Newsgroups: borland.public.jbuilder.java.swing