• Post Reply Bookmark Topic Watch Topic
  • New Topic

DefaultTableModel and Vector  RSS feed

 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to use DefaultTableModel in my JTable. Two-dimensional String array for data and String array for columns. With this set-up everything works fine once I add arrays to DTM with setDataVector(). However, when I am trying to use regular vectors insted of String arrays, I get ClassCastException on my setDataVector().
What's the problem? Do I have to set-up my vectors in some special way?
code:
====
public class tblExample extends JFrame {
Vector vData = new Vector();
Vector vCols = new Vector();
public JButtonTableExample(){
super( "JButtonTable Example" );
DefaultTableModel dm = new DefaultTableModel();
String[][] data = {{"button 1","foo"}, {"button 2","bar"}};
String[] cols = {"Button","String"};
vData.add(data);
vCols.add(cols);
dm.setDataVector(vData,vCols);
JTable table = new JTable(dm);
table.getColumn("Button").setCellRenderer(new JTextFieldRenderer());
//table.getColumn("Button").setCellEditor(new JTextFieldEditor(new JTextField()));
JScrollPane scroll = new JScrollPane(table);
getContentPane().add( scroll );
setSize( 400, 100 );
setVisible(true);
}
thanks,
Alex
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to change the way you load the Vector.
DefaultTableModel dm = new DefaultTableModel();
String[][] data = {{"button 1","foo"}, {"button 2","bar"}};
String[] cols = {"Button","String"};
For instance, the columns vector should not contain 1 element which is a String[] but the number of strings that you have (2).
Rows the way you are doing it contains 1 element of a 2 dimensional string array. It should be in your case 2 elements each containing a string array.
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks,
but what if I have a different set-up, where data and cols vector contains just string in each element of that array. For example data vector is {2,3,4,11} and cols vector is {"one","two"}. I tried it and it gives me the same error.
- Alex
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alex,
You need to have your data vector be a vector of vectors. You can't just add a 2 dimensional String array into it and be done. Try the example code below and it should work fine.

Regards,
Manfred.
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, but it still gives me an error: IllegalArgumentException, on line: dm.setDataVector(vData,vCols);
it seems that this error occurs when I add two vectors to DefaultTableModel...
=================================================================
public class JButtonTableExample extends JFrame {
Vector vData = new Vector();
Vector vCols = new Vector();
public JButtonTableExample(){
super( "JButtonTable Example" );
String[][] data = {{"button 1","foo"}, {"button 2","bar"}};
String[] cols = {"Button","String"};
for( int i = 0; i < data.length; ++i )
{
Vector v = new Vector();
for( int j = 0; j < data[i].length; ++j)
{
v.addElement( data[i][j] );
}
vData.add( v );
}
Vector v = new Vector();
for( int i = 0; i < cols.length; ++i )
{
v.addElement( cols[i] );
}
vCols.add( v );
DefaultTableModel dm = new DefaultTableModel();
dm.setDataVector(vData,vCols);
JTable table = new JTable(dm);
table.getColumn("Button").setCellRenderer(new JTextFieldRenderer());
//table.getColumn("Button").setCellEditor(new JTextFieldEditor(new JTextField()));
JScrollPane scroll = new JScrollPane(table);
getContentPane().add( scroll );
setSize( 400, 100 );
setVisible(true);
}
[This message has been edited by Alex Kravets (edited November 01, 2001).]
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alex,
The code works great for me int JDK1.2 and JDK1.3. Make sure you are using javax.swing.table.DefaultTableModel and not some other DefaultTableModel.
Manfred.
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm...but that's what I am using. And you tried running the same code?
(question of a desparate man).
thanks,
Alex
 
eric moon
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might try subclassing AbstractTableModel. That worked a lot better for me. You only have to implement a few methods: getColumnName(), getColumnCount(), getRowCount(), isCellEditable(), setValueAt() and getValueAt(). You can use a vector of vectors, or an array of arrays, or a hashtable of treemaps, whatever works for your data. Just figure out how the methods affect your data, and you're set. All the listeners to the Tree are set up by AbstractTableModel().
HTH
eric
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!