JTable

L

Liz

I have

double data[][];

And want to put this in a table with

JTable table = new JTable(data[][], columnNames[]);

but this wants data to be an Object[][]
I have been trying different things without luck.
How can I create the table without having to copy my data?
TIA
 
S

Sudsy

Liz said:
I have

double data[][];

And want to put this in a table with

JTable table = new JTable(data[][], columnNames[]);

but this wants data to be an Object[][]
I have been trying different things without luck.
How can I create the table without having to copy my data?
TIA

Double instead of double, perchance? That makes it an Object.
 
L

Liz

Sudsy said:
Liz said:
I have

double data[][];

And want to put this in a table with

JTable table = new JTable(data[][], columnNames[]);

but this wants data to be an Object[][]
I have been trying different things without luck.
How can I create the table without having to copy my data?
TIA

Double instead of double, perchance? That makes it an Object.
The code belos works, but I don't like to have to copy. Is there some
trick to cast data[][] to Object[][]

Object[][] tableData;
tableData = new Object[numRows][numCols]; // allocate space
// copy data -- there must be a better way
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
Double d = new Double(data[j]);
tableData[j] = (Object)d;
}
}
 
A

Andrew Harker

Liz said:
Liz said:
I have

double data[][];

And want to put this in a table with

JTable table = new JTable(data[][], columnNames[]);

but this wants data to be an Object[][]
I have been trying different things without luck.
How can I create the table without having to copy my data?
TIA

Double instead of double, perchance? That makes it an Object.

The code belos works, but I don't like to have to copy. Is there some
trick to cast data[][] to Object[][]

Object[][] tableData;
tableData = new Object[numRows][numCols]; // allocate space
// copy data -- there must be a better way
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
Double d = new Double(data[j]);
tableData[j] = (Object)d;
}
}

If you want to keep the 'double data[][]' without the above
copying or using 'Double data[][]' then maybe use a table model.
However, you still create Objects this way too as that is what
JTable displays. Maybe take the opportunity to use a number
format for locale

class DataTableModel extends AbstractTableModel {
double data[][] = { {1100.0, 12.0, -13.0}, {21.0, 2200.0, 23.0} };
NumberFormat numberFormatter;
DataTableModel () {
numberFormatter = new DecimalFormat("#,##0.00");
}
public int getRowCount() { return data.length; }
public int getColumnCount() { return data[0].length; }
public Object getValueAt(int row, int column) {
return numberFormatter.format(data[row][column]);
}
}
 
L

Liz

very useful info, tnx a bnch

Andrew Harker said:
Liz said:
Liz wrote:

I have

double data[][];

And want to put this in a table with

JTable table = new JTable(data[][], columnNames[]);

but this wants data to be an Object[][]
I have been trying different things without luck.
How can I create the table without having to copy my data?
TIA

Double instead of double, perchance? That makes it an Object.

The code belos works, but I don't like to have to copy. Is there some
trick to cast data[][] to Object[][]

Object[][] tableData;
tableData = new Object[numRows][numCols]; // allocate space
// copy data -- there must be a better way
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
Double d = new Double(data[j]);
tableData[j] = (Object)d;
}
}

If you want to keep the 'double data[][]' without the above
copying or using 'Double data[][]' then maybe use a table model.
However, you still create Objects this way too as that is what
JTable displays. Maybe take the opportunity to use a number
format for locale

class DataTableModel extends AbstractTableModel {
double data[][] = { {1100.0, 12.0, -13.0}, {21.0, 2200.0, 23.0} };
NumberFormat numberFormatter;
DataTableModel () {
numberFormatter = new DecimalFormat("#,##0.00");
}
public int getRowCount() { return data.length; }
public int getColumnCount() { return data[0].length; }
public Object getValueAt(int row, int column) {
return numberFormatter.format(data[row][column]);
}
}
 

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

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,627
Members
45,329
Latest member
InezZ76898

Latest Threads

Top