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