Re: DefaultTableModel() is a crap / How to hide a Table column?

From: David (nodeo_at_sk.sympatico.ca)
Date: 03/06/05


Date: 6 Mar 2005 11:05:02 -0600


"Mark Sizzler" <marksiz@email.org> wrote in message
news:d0f8tb$mmk$04$1@news.t-online.com...
> It all started with my search for a possibility to store data values in a
JTable without displaying it.
> I read somewhere in the Sun docs that columns without a header are not
displayed.
> Ok, I thought, according to a good coding style they are not displayed but
they are stored.
> But what a crap, the data is even worse deleted. See the simplified
scenario below:
>
> String rowData[][] = {
> { "firstrowfirstcol", "firstrowsecondcol" },
> { "secondrowfirstcol", "secondrowsecondcol" }
> };
>
> String columnNames[] = {
> "col1head"
> };
>
> DefaultTabelModel mytm = new DefaultTableModel(rowData, columnNames);
>
> After performing the last command not only mytm does not contain the
values of the second column
> but also the values of the second column are removed from the rowData!!!
> What a poor system architecture. It costs me a half day to find it out
because I could not believe it.
>
> Do I really have to setup a storage outside of DefaultTableModel to be
able to store
> additional data ?

Take a look at AbstractTableModel in the Java API. You can create your
own TableModel and use it in place of the default one in the JTable.
Use an inner class or something.

 private class FileTableModel extends AbstractTableModel {

  // make a vector or something for storage of data and
  // column names ect.

  // // overide these minimum
  public int getRowCount();
  public int getColumnCount();
  public Object getValueAt(int row, int column);

}

The table calls the above methods when it's filling itself up
for display.

You can overide getColumnCount() to return one (or more) less
than the amount of actual data columns, and that's all that will
show.

Make a getValueAt(int row, int column) method to return the
appropriate values, which the table will then display.

You can add alternative methods for your own use to return
the values which the table doesn't display ect.

It's kind of neat, and not too complicated if your table is
not too involved. You have to fiddle a bit with the column
name arrays (or vector) and methods as well if I remember
right, so that appropriate values will be returned for specific
columns.

David Otte

>
> Mark
>



Relevant Pages

  • Re: Columns not showing on JFrame
    ... > correctly although it doesnt actually display the column headings. ... public int getColumnCount() ... public String getColValue ...
    (comp.lang.java.help)
  • Re: converting look-up fields
    ... The tables in a database are to store data and to maintain the data's ... They do not exist to display or directly enter data directly. ... have to check each place the name occurred and fix it. ...
    (microsoft.public.access.tablesdbdesign)
  • Re: Validation rule between 2 fields
    ... Tables store data, forms display it for add/edit. ... data) to add validation code. ... "Amir" wrote in message ...
    (microsoft.public.access.tablesdbdesign)
  • Re: Capitalise first letter
    ... store data, Access forms display (and allow data entry). ... Jeff Boyce ...
    (microsoft.public.access.tablesdbdesign)
  • Re: Count and display entries
    ... I know you do not store data in forms, and I know forms are used to display ... have the number of entries displayed on the form. ... individual entry, and display the number in the "Number of Entries" field. ...
    (microsoft.public.access.formscoding)