dynamically adding a row to a jtable... well an extended jtable class...

Hi,

I've been trying to dynamically add a row to an extended JTable class.

Basically when the user completes the previous row, I try to add a new

row. I have the lojic in place, only I still can't figure out how to

successfully add a row to a jtable...

I've been trying to solve this problem for a while now, unfortunately

it seems I have hit a brick wall. I've tried many things, but the

general line of thought is what is represented in the code. That is to

convert it to the defaultTableModel and call addRow. This does not

work for me. I recieve the following error:

"

Exception in thread "main" java.lang.ClassCastException:

javax.swing.JTable$1

at myTable.myAddRow(myTable.java:17)

at test.main(test.java:28)

"

The following code is compilable, and a simplified version of the

problem.

I would appreciate any advice that you may have on how to add and

display a new row to a Jtable without causing it to have a problem.

Thanks.

-myTable.java-

import java.util.Vector;

import javax.swing.JTable;

import javax.swing.event.ListSelectionListener;

import javax.swing.event.TableModelListener;

import javax.swing.table.DefaultTableModel;

public class myTable extends JTable

implements ListSelectionListener, TableModelListener {

public myTable(Object[][] data, String[] columnNames){

super(data,columnNames);

}

public void myAddRow (){

((DefaultTableModel)this.getModel()).addRow(new Vector());

}

}

test.java (main class)-

import java.awt.Container;

import javax.swing.JFrame;

public class test {

static JFrame gJfrWindow;

public static void main(String[] args) {

String[] columnNames = {"Format Label", "Role", "Name", "", "", "",

"", ""};

Object[][] data = {

{" ", " ", " ", " ", " ", " ", " ", " "},

};

gJfrWindow = new JFrame("Jamaican Bobsled");

Container contentPane = gJfrWindow.getContentPane();

myTable x = new myTable(data,columnNames);

//x.myAddRow();

contentPane.add(x);

gJfrWindow.setSize(800,575);

gJfrWindow.setLocation(0,0);

gJfrWindow.setVisible(true);

gJfrWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

x.myAddRow();

}

[2454 byte] By [rufus1a] at [2007-10-2 3:02:01]
# 1

Don't extend JTable. Simply create a subclass of AbstractTableModel, create a addRow method, making sure that you fire the appropriate event when you add a row.

You really shouldn't EVER have to subclass JTable unless you are adding some significant new functionality. If you think you have to subclass to do what you want, think again, read docs, read examples, etc. and seriously reconsider. Subclassing the swing components is seldom necessary if you are using them the proper way (which of course is not clear at all many times).

gweedoa at 2007-7-15 21:28:16 > top of Java-index,Desktop,Core GUI APIs...
# 2
Check out this posting: http://forum.java.sun.com/thread.jspa?forumID=57&threadID=582184
camickra at 2007-7-15 21:28:16 > top of Java-index,Desktop,Core GUI APIs...
# 3

ok, so I wrote a myTableModel that extends abstractTableModel.

In the tableModel I wrote the myAddRow function in addition to the general required functions...

Now how can i call the myAddRow function from the tableModel?

I tried to cast the myTable.getModel() to myTableModel, but it is not accepted as a valid cast.

So now that I have a tableModel that implements myAddRow, how do I call it? (addRow?)

rufus1a at 2007-7-15 21:28:16 > top of Java-index,Desktop,Core GUI APIs...
# 4

> ok, so I wrote a myTableModel that extends abstractTableModel.

I gave you a working example. Why would you write your own TableModel when it already exists in the DefaultTableModel? If you don't understand how my example works the I can't help you any more because I don't understand your confusion.

camickra at 2007-7-15 21:28:16 > top of Java-index,Desktop,Core GUI APIs...
# 5

This is all I can post. I am not allowed to post the full code

Hope this helps.

private javax.swing.table.DefaultTableModel modelUserStatus;

private javax.swing.JTable tbUserStatus;

String head[]={"Username","Status"};

modelUserStatus = new javax.swing.table.DefaultTableModel(head,0);

tbUserStatus = new javax.swing.JTable(modelUserStatus);

String data[] = new String [2];

data[1] = "Online";

while(st2.hasMoreTokens()){

data[0] = st2.nextToken();

modelUserStatus.addRow(data);

}

st2 is a StringTokenizer object

hasMoreTokens() and nextToken() is methods in StringTokenizer class

this has the logic use it

1.create a string array

2.create a DefaultTableModel with the string array and row count 0

3.create table withs the DefaultTableModel

4.put ur data to a String array

5.DefaultTableModel.addraw(String array);

Check the DefaultTableModel class there lot of other methods two

Sameera_Abeysinghea at 2007-7-15 21:28:16 > top of Java-index,Desktop,Core GUI APIs...