Append rows to a JTable using (Object[ ] [ ], Object[ ] ) default model

Hi JTable specialists,

I simply want to append new rows and columns to a JTable.

With new columns I succeeded, see complete code.

Who can help me?

Kind regards

Michael

import java.awt.Container;

import java.awt.HeadlessException;

import javax.swing.JFrame;

import javax.swing.JScrollPane;

import javax.swing.JTable;

import javax.swing.JTextField;

import javax.swing.table.TableColumn;

import javax.swing.table.TableColumnModel;

publicclass JTableTest02extends JFrame{

private JTable tab1, tab2, tab3;

private JTextField txt;

private Container cp = getContentPane();

publicstaticvoid main(String[] args){

JTableTest02 tabTest =new JTableTest02();

tabTest.setSize( 1200, 400 );

tabTest.setVisible(true );

}

public JTableTest02()throws HeadlessException{

super ("Tabellen - Test");

newTab01();

screen01();

}

privatevoid newTab01(){

Object [][] rows =new Object [10][4];

//[rows] [cols]: 4 (data) + 6 (empty) rows are shown

rows[0][0] =new String ("00/00");

rows[0][1] =new String ("00/01");

rows[1][0] =new String ("01/00");

rows[1][1] =new String ("01/01");

rows[2][0] =new String ("02/00");

rows[2][1] =new String ("02/01");

rows[3][0] =new String ("03/00");

rows[3][1] =new String ("03/01");

Object [] colHead =new Object [3];

colHead[0] =new String ("Head 1yyyy");

colHead[1] =new String ("Head 2");

colHead[2] =new String ("Head 3");

//colHead2[3] = new String ("Head 4");

tab2 =new JTable (rows, colHead);

//Append new column -- works --

TableColumnModel tabColMod = tab2.getColumnModel();

TableColumn newCol =new TableColumn(tabColMod.getColumnCount());

tabColMod.addColumn(newCol);

tab2.getColumnModel().getColumn(tabColMod.getColumnCount()-1).setHeaderValue("New Col");

// -

// Append new Lines --

// ((DefaultTableModel)tab2.getModel()).addRow(colHead2); // -> java.lang.ClassCastException

//This can't not work as it is based on a (Vector[][], Vector[]) model

}

privatevoid screen01(){

cp.setLayout(null );// kein layout manager, feste Positionen

JScrollPane scrP02 =new JScrollPane(tab2);

cp.add(scrP02);

scrP02.setBounds(40,10,900,200);

}

}

[4714 byte] By [DeutscherMichela] at [2007-11-26 19:31:55]
# 1

import java.awt.Container;

import java.awt.HeadlessException;

import javax.swing.JFrame;

import javax.swing.JScrollPane;

import javax.swing.JTable;

import javax.swing.JTextField;

import javax.swing.table.DefaultTableModel;

import javax.swing.table.TableColumn;

import javax.swing.table.TableColumnModel;

public class JTableTest02 extends JFrame {

private JTable tab1, tab2, tab3;

private JTextField txt;

private Container cp = getContentPane();

public static void main(String[] args) {

JTableTest02 tabTest = new JTableTest02();

tabTest.setSize( 1200, 400 );

tabTest.setVisible( true );

}

public JTableTest02() throws HeadlessException {

super ("Tabellen - Test");

newTab01();

screen01();

}

private void newTab01(){

//Make table column header and determine no. of data columns

Object [] colHead = new Object [2];

colHead[0] = new String ("Head 1");

colHead[1] = new String ("Head 2");

Object [][] rows = new Object [10][3]; // rows-index has to include planned new columns

//[rows] [cols]: 2 (data) + 8 (empty) rows are shown

rows[0][0] = new String ("00/00");

rows[0][1] = new String ("00/01");

rows[1][0] = new String ("01/00");

rows[1][1] = new String ("01/01");

tab2 = new JTable (rows, colHead);

//Append new column -- works --

rows[0][2] = new String ("00/02");

rows[1][2] = new String ("01/02");

TableColumnModel tabColMod = tab2.getColumnModel();

TableColumn newCol = new TableColumn(tabColMod.getColumnCount());

tabColMod.addColumn(newCol);

tab2.getColumnModel().getColumn(tabColMod.getColumnCount()-1).setHeaderValue("New Col");

// Append new row --

Object [][] rows2 = new Object [10][4];

rows2[0][0] = new String ("00/00");

rows2[0][1] = new String ("00/01");

rows2[0][2] = new String ("00/02");

rows2[1][0] = new String ("01/00");

rows2[1][1] = new String ("01/01");

rows2[1][2] = new String ("01/02");

rows2[2][0] = new String ("02/00");

rows2[2][1] = new String ("02/01");

rows2[2][1] = new String ("02/02");

rows2[3][0] = new String ("03/00");

rows2[3][1] = new String ("03/01");

rows2[3][1] = new String ("03/02");

((DefaultTableModel)tab2.getModel()).addRow(rows2); // ***** -> java.lang.ClassCastException

//This can't not work as it is based on a (Vector[][], Vector[]) model

}

private void screen01() {

cp.setLayout( null ); // no layout manager, fixed position

JScrollPane scrP02 = new JScrollPane(tab2);

cp.add(scrP02);

scrP02.setBounds(40,10,900,200);

}

}

DeutscherMichela at 2007-7-9 22:02:32 > top of Java-index,Desktop,Core GUI APIs...
# 2

You will have to add the DefaultTableModel yourself first, since the default table model used by JTable constructor you're using is AbstractTableModel and not DefaultTableModel.

Here's an example:

import java.awt.BorderLayout;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTable;

import javax.swing.UIManager;

import javax.swing.table.DefaultTableModel;

public class TableAddRowTest {

public static void main(String[] args) {

try {

JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

DefaultTableModel model = new DefaultTableModel(new String[][]{{"1", "2"}, {"3", "4"}}, new String[]{"col1", "col2"});

model.addRow(new String[] {"5", "6"});

JPanel panel = new JPanel(new BorderLayout());

final JTable table = new JTable(model);

panel.add(new JScrollPane(table), BorderLayout.CENTER);

frame.getContentPane().add(panel);

frame.pack();

frame.setVisible(true);

} catch (Exception e) {}

}

}

Rodney_McKaya at 2007-7-9 22:02:32 > top of Java-index,Desktop,Core GUI APIs...
# 3

Perfect, works,

instead of constructing a JTable like

...

tab2 = new JTable(rows, colHead);

I created a model:

...

DefaultTableModel model = new DefaultTableModel(rows, colHead);

tab2 = new JTable (model);

But the way to get there wasn't easy as appending columns didn't work anymore and led to an error.

First I had to create my own TableColumnModel and change other (strange) things.

Anyway, I now can append lines and rows to my JTable.

Thanks a lot

Michael

PS:

How to allocate duke dollars?

DeutscherMichela at 2007-7-9 22:02:32 > top of Java-index,Desktop,Core GUI APIs...
# 4
> First I had to create my own TableColumnModel No you don't. The DefaultTableModel also supports an addColumn() method.Here is a simple working example: http://forum.java.sun.com/thread.jspa?forumID=57&threadID=577919
camickra at 2007-7-9 22:02:32 > top of Java-index,Desktop,Core GUI APIs...
# 5

camickr ,

Your example ?perfect, it helped me much.

So I succeeded to append columns without creating a ColumnModel, my prohram became simpler and I could delete the 剆trange?things I had written before.

Thank you

Michael

PS

Have you got some more teaching examples (not only for JTable) or do you know where to get such ones?

DeutscherMichela at 2007-7-9 22:02:32 > top of Java-index,Desktop,Core GUI APIs...