Look at the source code for JTable:

    public JTable(final Vector rowData, final Vector columnNames) {
        this(new AbstractTableModel() {
            public String getColumnName(int column) { return
columnNames.elementAt(column).toString(); }
            public int getRowCount() { return rowData.size(); }
            public int getColumnCount() { return columnNames.size(); }
            public Object getValueAt(int row, int column) {
                return ((Vector)rowData.elementAt(row)).elementAt(column);
            }
            public boolean isCellEditable(int row, int column) { return
true; }
            public void setValueAt(Object value, int row, int column) {
                ((Vector)rowData.elementAt(row)).setElementAt(value,
column);
                fireTableCellUpdated(row, column);
            }
        });
    }

In the code where 'nothing happens', try resizing the window. If the data
appears, it is because the table is not being told that its model's data is
changed. (The constructor you are using creates a model for you, based on
the values you supply it.)

I would always use a custom table model. Typically this would extend
DefaultTableModel. I tend to have an object for each row, with data members
for each column. So to change data, I have some method which alters the
contents of the model, then calls a fire* method (defined in
AbstractTableModel).

I think the reason it works when you add an already-populated table to a gui
container is that you arent changing the contents of a table already
displayed. (This requires an event to be fired by the model, to tell the
table to repaint itself.) Resize your window, and check this.

-----Original Message-----
From: Alexander Wallace <[EMAIL PROTECTED]>
To: Greg Munt <[EMAIL PROTECTED]>
Cc: [EMAIL PROTECTED] <[EMAIL PROTECTED]>
Date: Wednesday, September 12, 2001 04:35
Subject: Re: Setting the contents of a JTable


>Wow! that was fast! Thanks... So I guess that instead of using the
>constructor I'm using I need to creat the model first and then use the
>setModel... Or is the model being created but not refreshed?
>
>Thanks again!
>
>On Wed, 12 Sep 2001, Greg Munt wrote:
>
>> To change the data in an existing table, you need to reassign its model
>> (with JTable.setModel), or call some method on the object returned by
>> JTable.getModel().
>>
>> -----Original Message-----
>> From: Alexander Wallace <[EMAIL PROTECTED]>
>> To: [EMAIL PROTECTED] <[EMAIL PROTECTED]>
>> Date: Wednesday, September 12, 2001 04:24
>> Subject: Setting the contents of a JTable
>>
>>
>> >Hi there! I'm new to the list, and to java... I'm trying to fill up a
>> >JTable with the result of an SQL Query...
>> >
>> >I create all the Swing components at the beginning of the program, in
the
>> >constructor, and I think this may be where the problem is...
>> >
>> >I'm trying to use the JTable(Vector data, Vector columns) constructor to
>> >fill up the Jtable after I put all the right information from an
database
>> >table in the vectors, using
>> >
>> >jtblMyTable = new JTable(data, columns);
>> >
>> >And nothing shows up...
>> >
>> >If I follow some tutorials that create an instance of JTable using the
>> >same constructors or the (object data, object columns) constructor and
>> >after that add the JTable to the JFrame or whatever container, it
works...
>> >
>> >But how can I add the data to an already existing JTable??? Just like
you
>> >do with other components like JText, JTextArea, etc...
>> >
>> >I hope I was clear... Thanks in advance for any help!
>> >
>> >_______________________________________________
>> >Swing mailing list
>> >[EMAIL PROTECTED]
>> >http://eos.dk/mailman/listinfo/swing
>>
>> _______________________________________________
>> Swing mailing list
>> [EMAIL PROTECTED]
>> http://eos.dk/mailman/listinfo/swing
>>
>
>_______________________________________________
>Swing mailing list
>[EMAIL PROTECTED]
>http://eos.dk/mailman/listinfo/swing

_______________________________________________
Swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/swing

Reply via email to