Help with jTable

S

stacey

Hello everyone,

I am encountering the following problem. I want to initialize a jTable
with data. The thing is that i have the data that will be inserted,
into 4 Vectors. Each Vector will be a column of the table. So i want
to insert the columns one by one with their data.
I know that there must be a way, but i haven't found anything yet,and
as i am new to Java, i am lost with all these models and stuff.

Can you give me an example code?

The solution i have found until now, is to make a String[][] data and
a String[] columnNames, and use do:
jTable1.setModel(new
javax.swing.table.DefaultTableModel(data,columnNames));


Thank you very much in advance,

Stacey Venturas.
 
M

Matt Humphrey

|
| Hello everyone,
|
| I am encountering the following problem. I want to initialize a jTable
| with data. The thing is that i have the data that will be inserted,
| into 4 Vectors. Each Vector will be a column of the table. So i want
| to insert the columns one by one with their data.
| I know that there must be a way, but i haven't found anything yet,and
| as i am new to Java, i am lost with all these models and stuff.
|
| Can you give me an example code?
|
| The solution i have found until now, is to make a String[][] data and
| a String[] columnNames, and use do:
| jTable1.setModel(new
| javax.swing.table.DefaultTableModel(data,columnNames));

That's not a bad solution, but if you really want to avoid translating your
data, implement your own table model. It's not that difficult to make an
object that accepts your 4 vectors as parameters and then answers a few key
operations. Subclass AbstractTableModel because it already does the
bookkeeping. You have to make it answer the number of columns, the number
of rows, what the columns names and contents are and what the value of each
cell is. See the Javadocs for full details.

class VectorTableModel extends AbtractTableModel

getColumnCount () { How many columns?
getColumnClass (int column) {
return String.class; // Or whatever your data is

getColumnName (int column)
getRowCount () { // how many rows?
getValueAt (int row, int col). Figure out which vector and cell.

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top