I am having trouble implementing a simple boolean celleditor:
- I am using subclasses of TableColumn for columns which also know about
their data (the model's getValueAt method asks the appropriate column
for a data object using getData(row)).
- for the column that should represent a yes/no descision using
checkboxes, I implemented a CellRenderer that returns a new JCheckBox
each time
- for the CellEditor, I am keeping an Array of JCheckBoxes which also
capture the state
=> Problem: it doesn't work the way I implemented it: when I click on
another column, the cell that was selected last will be colored white
(empty). On my real application I have even more problems with this
implementation.
Minimal example is attached.
I know a boolean cell editor is easy/automatic, but I use a self-made
model (!= DefaultTableModel and such) so I went for the solution of
implementing a custom cell editor. Is there an easier way?
I guess DefaultCellEditor is difficult in this setup, because I'd like
to have other data in the cells too (and sort by these values).
If this is not possible, I'd be happy with a column that only contains
a JCheckBox too.
Thanks in advance!
--
Felix Natter
As you noted, using separate columns for each data element is relatively
easier. It's only slightly harder to use a composite type with a custom
renderer and editor, as suggested here:
<http://sites.google.com/site/drjohnbmatthews/table>
The critical thing is to keep the model and view thoroughly separate.
The class Value combines a Boolean and a Double; moreover, it implements
Comparable, which allows the default RowSorter to work as desired.
Making compareTo() consistent with equals should be straightforward, if
required.
I'm not sure how to make your TableColumn approach work.
--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
Thanks for your answer. Since using CellEditors correctly seems to be
really difficult, I used a renderer which gets Mouse events forwarded
from the JTable-Object.
This seems to work ok.
Thanks again,
--
Felix Natter
> "John B. Matthews" <nos...@nospam.invalid> writes:
>
> > In article <87bpi44...@etchy.mobile.lcn>,
> > Felix Natter <felix....@smail.inf.fh-brs.de> wrote:
> > [...]
> > As you noted, using separate columns for each data element is
> > relatively easier. It's only slightly harder to use a composite
> > type with a custom renderer and editor, as suggested here:
> >
> > <http://sites.google.com/site/drjohnbmatthews/table>
> >
> > The critical thing is to keep the model and view thoroughly
> > separate. The class Value combines a Boolean and a Double;
> > moreover, it implements Comparable, which allows the default
> > RowSorter to work as desired. Making compareTo() consistent with
> > equals should be straightforward, if required.
> >
> > I'm not sure how to make your TableColumn approach work.
>
> Thanks for your answer. Since using CellEditors correctly seems to be
> really difficult, I used a renderer which gets Mouse events forwarded
> from the JTable-Object.
>
> This seems to work ok.
I empathize, but I would argue that it's only relatively more difficult.
The extra effort helps keep the MVC architecture straight, and it
creates fewer L&F problems in cross-platform maintenance and use; key
bindings are especially important to many users.