2 Replies Latest reply on May 13, 2008 7:08 AM by 843806

    Help with JTable and Vectors...

    843806
      So I am just trying to use this constructor (I have already played with the rest) JTable(Vector<Object> data, Vector<String> columnNames).

      I can't get it to work!

      the code...
      import java.util.Vector;
      
      public class Driver {
           public static void main(String[] args) {
                javax.swing.JFrame jf = new javax.swing.JFrame("Table Test");
                jf.setSize(500, 500);
                jf.setLocationRelativeTo(null);
                jf.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
                
                Vector<Object> data = new Vector<Object>();
                Vector<String> columnNames = new Vector<String>();
                
                data.addElement(new Object());
                data.addElement(new Object());
                
                columnNames.addElement("Column 1");
                columnNames.addElement("Column 2");
                
                TablePanel tp = new TablePanel(data, columnNames);
                
                jf.add(tp);
                
                jf.setVisible(true);
           } // end main
      } // end class
      
      import java.awt.BorderLayout;
      import java.awt.Color;
      import java.lang.reflect.InvocationTargetException;
      import java.util.Vector;
      
      import javax.swing.JButton;
      import javax.swing.JPanel;
      import javax.swing.JScrollPane;
      import javax.swing.JTable;
      import javax.swing.ListSelectionModel;
      import javax.swing.SwingUtilities;
      import javax.swing.border.LineBorder;
      import javax.swing.table.*;
      
      public class TablePanel extends JPanel {
           private static final long serialVersionUID = -896108142958197454L;
      
           public JTable table;
           public JScrollPane scrollTable;
           public int rows = 0, columns = 0;
           public Vector<String> columnNames;
           public Vector<Object> data;
           public Object[][] data2;
           public Object[] columnNames2;
           
           private DefaultTableModel defaultTableModel;
           
           public TableCellEditor tableCellEditor;
           public TableCellRenderer tableCellRenderer;
           public TableColumnModel tableColumnModel;
           public TableModel tableModel;
           public ListSelectionModel listSelectionModel;
           public JTableHeader tableHeader;
           public TableColumn tableColumn;
           public TableRowSorter<TableModel> tableRowSorter;
           public TableStringConverter tableStringConverter;
           
           public JPanel buttonPanel;
           public JButton clear, submit;
           
           private int choice = 0;
                
           /**
            * Overloaded Constructor
            * @param data the data to be used for the table
            * @param columnNames the names of the columns
            */
           public TablePanel(Vector<Object> data, Vector<String> columnNames) {
                choice = 6;
                
                this.data = data;
                this.columnNames = columnNames;
                
                try {
                     // Synchronized event dispatching thread
                     SwingUtilities.invokeAndWait(this);
                } catch(InterruptedException ie) {
                     // Asynchronous event dispatching thread
                     SwingUtilities.invokeLater(this);
                } catch(InvocationTargetException ite) {
                     // Asynchronous event dispatching thread
                     SwingUtilities.invokeLater(this);
                } // end try catch block
           } // end overloaded constructor
                
           /**
            * Used to construct the GUI components
            */
           public void createAndShowGUI() {
                setLayout(new BorderLayout());
                
                clear = new JButton("Clear");
                submit = new JButton("Submit");
                
                buttonPanel = new JPanel(new BorderLayout());
                
                // only load the components defined by the constructor 
                switch(choice) {               
                     case 6:
                          table = new JTable(data, columnNames);
                          
                          // assign values to table components
                          tableModel = table.getModel();
                          tableColumnModel = table.getColumnModel();
                          tableCellEditor = table.getCellEditor();
                          tableColumnModel = table.getColumnModel();
                          listSelectionModel = table.getSelectionModel();
                          tableHeader = table.getTableHeader();
      
                          scrollTable = new JScrollPane(table);
                          scrollTable.setBorder(new LineBorder(Color.BLACK));
                          
                          buttonPanel.add(clear, BorderLayout.WEST);
                          buttonPanel.add(submit, BorderLayout.EAST);
                          
                          add(scrollTable, BorderLayout.CENTER);
                          add(buttonPanel, BorderLayout.SOUTH);
                     break;
                     
                     default:
                          
                     break;
                } // end switch
           } // end createAndShowGUI
      
           /**
            * Used by a synchronized event dispatching thread or by a asynchronous event
            * dispatching thread. The synchronized one is tried first, if there is an 
            * exception then the asynchronous one is used
            */
           public void run() {
                createAndShowGUI();
           } // end run
      } // end class
      the error...
      Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.Object cannot be cast to java.util.Vector
           at javax.swing.table.DefaultTableModel.justifyRows(Unknown Source)
           at javax.swing.table.DefaultTableModel.setDataVector(Unknown Source)
           at javax.swing.table.DefaultTableModel.<init>(Unknown Source)
           at javax.swing.JTable.<init>(Unknown Source)
           at asa.gui.TablePanel.createAndShowGUI(TablePanel.java:345)
           at asa.gui.TablePanel.run(TablePanel.java:385)
      I don't get the error!