JTable addRow...

6

6e

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();

}
}
 
O

Oliver Wong

6e said:
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();

}
}

The problem is that JTable's getModel() returns a TableModel, and not a
DefaultTableModel, which is why you get your class cast exception. The
TableModel interface doesn't define any way for you to add rows, so you
can't rely on whatever table model JTable is using in the background.
Instead, you should create your own TableModel, and then pass that in when
you construct a new JTable.

There's a tutorial on creating table models at
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#data

- Oliver
 

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,996
Messages
2,570,237
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top