I can't get ArrayList to work in my JTable

I want to be able to dynamically add and remove data (and rows) from my JTable, and so I wanted to just Lists when I implemented it. I've tried 2 ways and they both didn't work. Can someone show me some sample code to do this?
I tried 2 different methods:
method 1:
JTable Data_Table = new  JTable(data_Vector, Collumn_Names_Vector);when I started the applet I got an error saying Applet init error
But the error could also have happened in 2 other places here. 1, I created 2 Lists instead of Vectors and converted them to Vectors when passing them to JTable using:
new Vector(data)
and 2, I may have created my 2 dimensional data List incorrectly. I tried:
List Parent = new ArrayList();
List child1 = new ArrayList();
List child2 = new ArrayList();
child1.add(data1)
child2 ...
Parent.add(child1)
Parent.add(child2)and I tried:
List Parent = new ArrayList();
Object[] temp = { new Integer(); new Integer()};
Parent.add(temp.asList);But as I said method 1 did not work.
method 2:
I tried creating my own TableModel.
In this way, the applet would start, and I can see my collumn names, but the data is blank and all cells are invisible or nonexistent, I don't know which. How do I initialize the Table values? I'd like to have one row with all 0's when the Applet opens? I'd also like to see a row so I can edit it.
then I used this code (inside my model):
        public int getColumnCount() {
            return Analog_columnNames.length;
        public int getRowCount() {
            return Analog_data.size();
        public String getColumnName(int col) {
            return Analog_columnNames[col];
        public Object getValueAt(int row, int col) {
            java.util.List aRow = (ArrayList)Analog_data.get(row);
         return  aRow.get(col);
        public void setValueAt(Object value, int row, int col) {
            if (getValueAt(0,col) instanceof Integer                       
                    && !(value instanceof Integer)) {                 
                try {
                     java.util.List tmp =  (java.util.List) Analog_data.get(row);
              tmp.set(col, (Object)new Integer(value.toString()));
              fireTableCellUpdated(row, col);
                } catch (NumberFormatException e) {
            } else {
                 java.util.List tmp =  (java.util.List) Analog_data.get(row);
          tmp.set(col, (Object)new Integer(value.toString()));
                fireTableCellUpdated(row, col);
        }Analog_columnNames is a normal array

this may help You have to remove the button from the columns
otherwise You need som other classes.
       jTable1.addextrarowtotable(Name, aantal , prijs);CopyCalcTable.java
import javax.swing.*;
import java.util.Vector;
import javax.swing.table.*;
import java.text.DecimalFormat;
public class CopyCalcTable extends JTable{
    protected CopyCalcTableModel model;
    CopyCalcTable(){
        super();
        Vector columns= new Vector();
        columns.add("Type");
        columns.add("Omschrijving");
        columns.add("P/S");
        columns.add("Aantal");
        columns.add("Prijs");
        columns.add("Verwijderen");
        model = new CopyCalcTableModel(columns,1);
        setModel(model);
    public void setColumSize(){
        TableColumn column;
        int width = getWidth();
        column = getColumnModel().getColumn(0);
        column.setResizable(false);
        column.setMinWidth((width*2)/14);
        column.setPreferredWidth((width*2)/14);
        column = getColumnModel().getColumn(1);
        column.setResizable(false);
        column.setMinWidth((width*6)/12);
        column.setPreferredWidth((width*2)/14);
        column = getColumnModel().getColumn(2);
        column.setMinWidth((width*1)/14);
        column.setResizable(false);
        column.setPreferredWidth((width*1)/14);
        column = getColumnModel().getColumn(3);
        column.setResizable(false);
        column.setMinWidth((width*1)/14);
        column.setPreferredWidth((width*1)/14);
        column = getColumnModel().getColumn(4);
        column.setResizable(false);
        column.setMinWidth((width*1)/14);
        column.setPreferredWidth((width*1)/14);
        column = getColumnModel().getColumn(5);
        column.setResizable(false);
        column.setMinWidth((width*2)/14);
        column.setPreferredWidth((width*2)/14);
    public void HideColum(){
        TableColumn column;
        int width = getWidth();
        column = getColumnModel().getColumn(0);
        column.setResizable(false);
        column.setMinWidth((width*2)/14);
        column.setPreferredWidth((width*2)/14);
        column = getColumnModel().getColumn(1);
        column.setResizable(false);
        column.setMinWidth((width*6)/12);
        column.setPreferredWidth((width*2)/14);
        column = getColumnModel().getColumn(2);
        column.setMinWidth((width*1)/14);
        column.setResizable(false);
        column.setPreferredWidth((width*1)/14);
        column = getColumnModel().getColumn(3);
        column.setResizable(false);
        column.setMinWidth((width*1)/14);
        column.setPreferredWidth((width*1)/14);
        column = getColumnModel().getColumn(4);
        column.setResizable(false);
        column.setMinWidth((width*1)/14);
        column.setPreferredWidth((width*1)/14);
        column = getColumnModel().getColumn(5);
        column.setResizable(false);
        column.setMinWidth(0);
        column.setPreferredWidth(0);
    public void removefirstrow(){
        model.removeRow(0);
    public void removeTotalrow(){
        model.removeRow(model.getRowCount()-1);
    public void addTotalrow(){
        Vector data;
        String Col_Bediening = "";
        String Col_Uitboek ="";
        String Col_Rectoverso="";
        String Col_Formaat="";
        String Col_Kleur="";
        String Col_Gewicht="";
        Double Col_PS;
        String Col_Aantal;
        Double Col_Prijs;
        data = new Vector();
        Col_Prijs = new Double("0.00");
        double totaal= Col_Prijs.doubleValue();
        // number of extra's
        java.text.NumberFormat nf =  java.text.NumberFormat.getInstance();
        // set whether you want commas (or locale equivalent) inserted
        nf.setGroupingUsed(true );
        // set how many places you want to the right of the decimal.
        nf.setMinimumFractionDigits(2 );
        nf.setMaximumFractionDigits(2 );
        // set how many places you want to the left of the decimal.
        nf.setMinimumIntegerDigits(1 );
        int rows = model.getRowCount();
        for (int i = 0;i < rows; i++){
            Double d = new Double((String) model.getValueAt(i, 4));
            totaal += d.doubleValue();
        Col_Prijs = new Double(totaal*1.00);
        data.add("Totaal : EUR");
        data.add("");
        data.add(new String(""));
        data.add("");
        data.add(nf.format(Col_Prijs));
        //data.add(Col_Prijs.toString());
        javax.swing.JButton button = new javax.swing.JButton("EUR");
        data.add(button);
        model.addRow(data);
        setColumSize();
    public void addkopierowtotable(Integer aantal, double prijs, String omschrijving){
        Vector data;
        CopyCalcTableModel model = (CopyCalcTableModel) getModel();
        Double Col_PS;
        String Col_Aantal;
        Double Col_Prijs;
        //model.addColumn("Verwijderen");
        data = new Vector();
        Col_PS = new Double(prijs);
        Col_Aantal = new String(aantal.toString());
        Col_Prijs = new Double(prijs*aantal.intValue());
        data.add("Kopie�n");
        data.add(omschrijving);
        data.add(Col_PS.toString());
        data.add(Col_Aantal);
        data.add(Col_Prijs.toString());
        javax.swing.JButton button = new JButton("Verwijderen");
        button.addMouseListener(new MyJButtonMouseListener(this));
        data.add(button);
        removeTotalrow();
        model.addRow(data);
        addTotalrow();
    public void addextrarowtotable(String Name, Integer aantal, double prijs){
        Vector data;
        Double Col_PS;
        String Col_Aantal;
        Double Col_Prijs;
        data = new Vector();
        Col_PS = new Double(prijs);
        Col_Aantal = new String(aantal.toString());
        Col_Prijs = new Double(prijs*aantal.intValue());
        data.add("Extra");
        data.add(Name);
        data.add(Col_PS.toString());
        data.add(Col_Aantal);
        data.add(Col_Prijs.toString());
        javax.swing.JButton button = new javax.swing.JButton("Verwijderen");
        button.addMouseListener(new MyJButtonMouseListener(this));
        data.add(button);
        removeTotalrow();
        model.addRow(data);
        addTotalrow();
    public void addspiraalrowtotable(String Name, Integer aantal, double prijs){
        Vector data;
        Double Col_PS;
        String Col_Aantal;
        Double Col_Prijs;
        //model.addColumn("Verwijderen");
        data = new Vector();
        Col_PS = new Double(prijs);;
        Col_Aantal = new String(aantal.toString());
        Col_Prijs = new Double(prijs*aantal.intValue());
        data.add("Spiraal");
        data.add(Name);
        data.add(Col_PS.toString());
        data.add(Col_Aantal);
        data.add(Col_Prijs.toString());
        javax.swing.JButton button = new javax.swing.JButton("Verwijderen");
        button.addMouseListener(new MyJButtonMouseListener(this));
        data.add(button);
        removeTotalrow();
        model.addRow(data);
        addTotalrow();
    public void addklemrugrowtotable(String Name, Integer aantal, double prijs){
        Vector data;
        Double Col_PS;
        String Col_Aantal;
        Double Col_Prijs;
        //model.addColumn("Verwijderen");
        data = new Vector();
        Col_PS = new Double(prijs);
        Col_Aantal = new String(aantal.toString());
        Col_Prijs = new Double(prijs*aantal.intValue());
        data.add("Klemrug");
        data.add(Name);
        data.add(Col_PS.toString());
        data.add(Col_Aantal);
        data.add(Col_Prijs.toString());
        javax.swing.JButton button = new javax.swing.JButton("Verwijderen");
        button.addMouseListener(new MyJButtonMouseListener(this));
        data.add(button);
        removeTotalrow();
        model.addRow(data);
        addTotalrow();
    public void addplastificerenrowtotable(String Name, Integer aantal, double prijs){
        Vector data;
        Double Col_PS;
        String Col_Aantal;
        Double Col_Prijs;
        //model.addColumn("Verwijderen");
        data = new Vector();
        Col_PS = new Double(prijs);
        Col_Aantal = new String(aantal.toString());
        Col_Prijs = new Double(prijs*aantal.intValue());
        data.add("Plastcificeren");
        data.add(Name);
        data.add(Col_PS.toString());
        data.add(Col_Aantal);
        data.add(Col_Prijs.toString());
        javax.swing.JButton button = new javax.swing.JButton("Verwijderen");
        button.addMouseListener(new MyJButtonMouseListener(this));
        data.add(button);
        removeTotalrow();
        model.addRow(data);
        addTotalrow();
    public int getRowCount() {
        if ( model == null ){
            return 0;
        }else {
            return model.getRowCount();
class CopyCalcTableModel extends AbstractTableModel {
    Vector data;
    Vector columnNames;
    CopyCalcTableModel(Vector head_vec, int rows) {
        columnNames = (Vector) head_vec.clone();
        //    coldata = new Vector(columnNames.size());
        data = new Vector(rows);
    CopyCalcTableModel(Vector head_vec) {
        columnNames = (Vector) head_vec.clone();
        //    coldata = new Vector(columnNames.size());
        data = new Vector();
    CopyCalcTableModel(Vector col_vec, Vector head_vec) {
        Vector coldata;
        columnNames = (Vector) head_vec.clone();
        data = (Vector) col_vec.clone();
    public int getColumnCount() {
        //        System.out.println(columnNames);
        return columnNames.size();
    public int getRowCount() {
        //        System.out.println(data);
        return data.size();
    public void removeRow(int row) {
        //        System.out.println(data);
        data.removeElementAt(row);
        fireTableStructureChanged();
    public String getColumnName(int col) {
        return (String) columnNames.elementAt(col);
    public Object getValueAt(int row, int col) {
        Vector v_row;
        Vector v_col;
        v_row = (Vector) data.elementAt(row);
        return v_row.elementAt(col);
     * JTable uses this method to determine the default renderer/
     * editor for each cell.  If we didn't implement this method,
     * then the last column would contain text ("true"/"false"),
     * rather than a check box.
    public Class getColumnClass(int c) {
        // return getValueAt(0, c).getClass();
        int rowIndex = 0;
        Object o = getValueAt(rowIndex, c);
        if (o == null) {
            return Object.class;
        } else {
            // System.out.println(o.getClass());
            return o.getClass();
     * Don't need to implement this method unless your table's
     * editable.
    public boolean isCellEditable(int row, int col) {
        //Note that the data/cell address is constant,
        //no matter where the cell appears onscreen.
        /*Vector v_row;
        v_row = (Vector) data.elementAt(row);
        if (col == 10 ) {
            return true;
        } else {
            return false;
        return false;
     * add collumn
    public void addColumn(String label, Object value){
        columnNames.add(label);
        for ( int i=0;data.size()>i;i++){
            Vector row = (Vector) data.elementAt(i);
            row.add(value);
            data.setElementAt(row, i);
        fireTableStructureChanged();
    public void addRow(Vector values){
        data.add(values);
        fireTableStructureChanged();
     * Don't need to implement this method unless your table's
     * data can change.
    public void setValueAt(Object value, int lrow, int lcol) {
}

Similar Messages

Maybe you are looking for

  • Can't edit movie in QT Pro

    I have a mpg that I took on my digital camera. It plays fine in QT but the editing controls are not showing up as I roll over the timeline. Also when i import into iMovie, I get the video but there is no audio. This is a movie of my 4 month old which

  • I cant delet all the itunes files o my cp to download the

    I cant delet all the bonjur files to download the new. Itunes what do i do

  • Steps in Validation

    Hi, Can somebody forward me the steps related to Validation & substitution, its urgent. In T. code GGB0 ==> Line Item > Steps> check it is defined as : NOT BSEG-HKONT IN ZBANK What is this ZBANK ... is it the variable. How is my BANK GL Account relat

  • How do you find what objects you modified?

    I am leaving my current company soon and they want me to document all objects that I touched.  Is there a simple way to do this?  I am thinking of a program that I can run (create) by searching z-objects (programs, structures, tables, forms, etc...)

  • Why do I have 13 versions of httpd running?

    I realize that I am running a MAMP server, but for the life of me I can't figure out why 13 instances of httpd are running in Activity Manager!.  All but one are attributed to my user, and one of them is tied to root.  They seem to take no CPU, but I