Links

Lists

Latest Updates

Ruby On Rails List
Python list
Advanced Java
The JavaScript List
Apache Users
Full Disclosure
Linux Security

Search the archives!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Fwd: JTable query!]


  • From: terry.williams@xxxxxxxxxxxxx (Terry Williams)
  • Subject: [Fwd: JTable query!]
  • Date: Tue, 30 Nov 1999 11:29:39 +0000

Carlos Villavieja Prados wrote:

> Hi Terry
> I'm updating a Jtable by method setValueAt(Object, col , row)
> but this update does not appear till I click with the mouse, do you know
> what can I do?
>
> Can I work without a table model?
>
> Thanks in advanced!
>
> On Tue, 30 Nov 1999, Terry Williams wrote:
>
> > Hi,
> >     if you want more than the default behaviour of the DefaultTableModel
> >
> > I'm afraid you'll have to create your own TableModel. Not as hard as it
> > seems,
> >
> > Here's a basic sample
> >
> > public ReadOnlyTableModel extends AbstractTableModel
> > {
> >     // stuff here that initialises columnNames & data
> >
> >
> >     public boolean isCellEditable(int row, int col) { return false; }
> >
> >     public Class getColumnClass(int c) { return getValueAt(0, c);
> >
> >     public Object getValueAt(int row, int col)
> >     {
> >         return data[row][col];
> >         // or however you've stored your info if its a Vector of Vectors
> >
> > it maybe something like this
> >         // Vector rowVec = (Vector)data.elementAt(row);
> >         // return rowVec.elementAt(col);
> >     }
> >
> >     public int getRowCount() { return data.length; } // or data.size if
> > using vector of vectors
> >
> >     public getColumnCount() { return columnNames.size(); }
> >
> >     public getColumnName(int col) { return columnNames[col]; }
> >
> > }
> >
> > I think that's all that is needed for an AbstractTableModel, I'm sure
> > someone will correct me if I'm wrong.
> >
> > This may seem complex for such a small change but the DefaultTableModel
> > is
> > a very simplistic model with little flexibility, the flexibility comes
> > from being able to plug in your own table model - see the Swing
> > documentation (and Tutorial) for a more in depth discussion of
> > AbstractTableModel.
> >
> > BTW all swing components have detault "something" models associated with
> >
> > them, if you write you own versions then you can begin to appreciate the
> >
> > full power of swing. I get a little tired of people telling me that
> > swing
> > is useless and inflexible - you only get out of it what you put into it!
> >
> > Terry
> >
>
> ----------------------------------------------------------------------------
>                        Carlos Villavieja Prados
>                           cvprados@xxxxxxxxxxxx
>                     http://www.salleURL.edu/~cvprados
> ----------------------------------------------------------------------------

Carlos,
    You probably aren't going to like this but this is roughly how I do it.

Create an appropriate TableModel and in your setValueAt method fire a
TableModelEvent

ie.

public void setValueAt(int row, int col)
{
    // set your value here

    fireTableChanged(new TableModelEvent(this, row, row, col);
}

private void fireTableChanged(TableModelEvent tme)
{
    // notify all listeners that the TableModel has chabged (i.e. tell your
JTable that the model has changed)

    for (int i=0; i<listeners.size(); i++)
    {
        TableModelListener tml = (TableModelListener)listener.elementAt(i);
        tml.tableChanged(tme);
    }
}

and in your app

myTableModel.addTableModelListener(new ClickableTableListener);

public class ClickableTableListener implements TableModelListener
{
    public void tableChanged(TableModelEvent tme)
    {
        // do your stuff in here

        repaint();
    }
}

a bit tortuous the first time but obviously once you've done it once you don't
have to do it again (at least for this operation). I haven't used the
DefaultTableModel in around 18 months - always replace it with your own that
does what you want it to.

Sorry that this is nastier than you were hoping for but it really is easy once
you've used them for a bit

Terry


---
To unsubscribe, mail advanced-java-unsubscribe@xxxxxxxxxxxxxxxx
To get help, mail advanced-java-help@xxxxxxxxxxxxxxxx