2

I searched for a while but I cant find a solution for my problem.

I want to display the Values of an ArrayList in a JTable, i'm pretty new to this and cant fix the error.

package Statistik;

import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class Statistic {

    private static ArrayList<String> rowA = new ArrayList();
    private static ArrayList<String> rowB = new ArrayList();
    private static ArrayList<String> rowC = new ArrayList();
    private static ArrayList<String> titel = new ArrayList();
    private static ArrayList<Object> table = new ArrayList();

    public static void main(String[] args) {

        titel.add("Name");
        titel.add("Art der Bearbeitung");
        titel.add("Datum");

        addRows("buchung", "Created", "10.10.10");
        addRows("buchung", "Created", "10.10.10");
        addRows("buchung", "Created", "10.10.10");
        addRows("buchung", "Created", "10.10.10");
        addRows("buchung", "Created", "10.10.10");

        table.add(rowA);
        table.add(rowB);
        table.add(rowC);


        // Das JTable initialisieren
        JTable EndTable = new JTable( table , titel );

        JFrame frame = new JFrame( "Demo" );
        frame.getContentPane().add(new JScrollPane( EndTable ) );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible( true );
    }

    public static void addRows(String rowa, String rowb, String rowc) {

        rowA.add(rowa);
        rowB.add(rowb);
        rowC.add(rowc);

    }

}

I can't set the ArrayList table as first Value in my EndTable, but i dont know how I should do otherwise.

Thank you all for trying to answer my problem.

Edit

My goal is to make a List with

Entity-Name, art of change, Date

so I thought it would be the best to use an ArrayList because it's flexible. It have to be flexible because we dont know how much the user will change.

3 Answers 3

5

You should create custom TableModel for your table see the following link for more details Custom JTable Model

Here's some code to start with:

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

public class Test extends JFrame {
    public Test() {
        setBounds(100, 100, 500, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JTable table = new JTable(new ModelData());
        add(new JScrollPane(table));
        setVisible(true);
    }

    public static void main(String[] args) {
        new Test();
    }
}

class ModelData extends AbstractTableModel {
    List<Data> data = new ArrayList<Data>();
    String colNames[] = { "Name", "Type", "Date" };
    Class<?> colClasses[] = { String.class, String.class, Date.class };

    ModelData() {
        data.add(new Data("name 1", "type 1", new Date()));
        data.add(new Data("name 2", "type 2", new Date()));
        data.add(new Data("name 3", "type 3", new Date()));
    }

    public int getRowCount() {
        return data.size();
    }

    public int getColumnCount() {
        return colNames.length;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        if (columnIndex == 0) {
            return data.get(rowIndex).getName();
        }
        if (columnIndex == 1) {
            return data.get(rowIndex).getType();
        }
        if (columnIndex == 2) {
            return data.get(rowIndex).getDate();
        }
        return null;
    }

    public String getColumnName(int columnIndex) {
        return colNames[columnIndex];
    }

    public Class<?> getColumnClass(int columnIndex) {
        return colClasses[columnIndex];
    }

    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return true;
    }

    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        if (columnIndex == 0) {
            data.get(rowIndex).setName((String) aValue);
        }
        if (columnIndex == 1) {
            data.get(rowIndex).setType((String) aValue);
        }
        if (columnIndex == 2) {
            data.get(rowIndex).setDate((Date) aValue);
        }
        fireTableCellUpdated(rowIndex, columnIndex);
    }
}

class Data {
    String name;
    String type;
    Date date;

    public Data(String name, String type, Date date) {
        super();
        this.name = name;
        this.type = type;
        this.date = date;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
}

And this is the result:

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Extend AbstractTableModel to get the required event handling, for example.
+1 for the update, but don't forget to fire the appropriate event in setValueAt().
3

Nope since , JTable has no reserved argument for ArrayList, but a trick will solve it ! you know that the arguments for JTable is also (Object[][], object[])

      Object[] tempTitel = titel.toArray(); // return Object[]

      String[][] tempTable = new String[table.size()][]; 

     int i = 0;
     for (List<String> next : table) {
      tempTable[i++] = next.toArray(new String[next.size()]); // return Object[][]
    }



   JTable EndTable = new JTable(tempTable,tempTitel);

Note that I change ArrayList<String> table= new ArrayList() to ArrayList<ArrayList<String>> table = new ArrayList(); so when combine it :

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class Statistic {

    private static ArrayList<String> rowA = new ArrayList();
    private static ArrayList<String> rowB = new ArrayList();
    private static ArrayList<String> rowC = new ArrayList();
    private static ArrayList<String> titel = new ArrayList();
    private static ArrayList<ArrayList<String>> table = new ArrayList();

    public static void main(String[] args) {

        titel.add("Name");
        titel.add("Art der Bearbeitung");
        titel.add("Datum");

        addRows("buchung", "Created", "10.10.10");
        addRows("buchung", "Created", "10.10.10");
        addRows("buchung", "Created", "10.10.10");
        addRows("buchung", "Created", "10.10.10");
        addRows("buchung", "Created", "10.10.10");

        table.add(rowA);
        table.add(rowB);
        table.add(rowC);

          Object[] tempTitel = titel.toArray();
          String[][] tempTable = new String[table.size()][];
       int i = 0;
       for (List<String> next : table) {
       tempTable[i++] = next.toArray(new String[next.size()]);
        }

       JTable EndTable = new JTable(tempTable,tempTitel);

        JFrame frame = new JFrame("Demo");
        frame.getContentPane().add(new JScrollPane(EndTable));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public static void addRows(String rowa, String rowb, String rowc) {

        rowA.add(rowa);
        rowB.add(rowb);
        rowC.add(rowc);

    }

}

5 Comments

Wow...just WOW! Thank you so much! This helped my alot! but i have one adding question Why: private static ArrayList<ArrayList<String>> ?
ArrayList<ArrayList<String>> is same like String[][] in casual array, and we want to convert it to Object[][]
aahh i could have thought of this... thank you for your explaination!
Are you buggy that why only 3 rows being seen ??? thats because when you declare addRows("buchung", "Created", "10.10.10")' 5 times , you are not adding the rows 5 times but instead you add datas AS it 5 columns (try to understand my words correctly ), and because you only create 3 columns by "Name","Art der Bearbeitung", "Datum" , so the rest 2 datas (for 2 columns) are ignored
i understand now, i got the problem that i need more columns, i understand it so far but i cant figure out how i can make the rows individual (say: sometimes 3, sometimes 15 entries) but i try to figure it out at my own first! thank you Fev^^
0

Perhaps you should post your error. I can see from JTable constructor in the Java API that it does not take an ArrayList as arguments.

JTable(Object[][] rowData, Object[] columnNames) Constructs a JTable to display the values in the two dimensional array, rowData, with column names, columnNames.

Perhaps you should try using primitive arrays?

1 Comment

Would be interested to know why my answer was down voted, as far as I can tell this is correct. If I am wrong I'd like to know for my own understanding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.