Domanda

I want to display some things in a JTable.

The problem is that the JTable columnnames are "A", "B", "C", ...

my code is like this:

import javax.swing.JTable;

public class View extends JFrame implements Observer {

    private JTable contentTable;

    public View() {
        ...

        String[][] s = {{"test","test","test", "test}};
        String[] columnNames = { "Name", "Category", "Start", "End" };
        this.contentTable = new JTable(new MyTableModel(columnNames, s));
        this.contentPanel.add(new JScrollPane(this.contentTable));

        ...
    }

}

And here is the MyTableModel class

import javax.swing.table.AbstractTableModel;

    public class MyTableModel extends AbstractTableModel {

        private String[] columnNames;
        private Object[][] data;

        public MyTableModel(String[] columns, Object[][] dat){
            this.columnNames = columns;
            this.data = dat;
        }

    ...

    }

If i don't use a TableModel it works f.e.: this.contentTable = new JTable(s, columnNames);, But i dont know how to Change the Table if there is some new Data... That's why i use a TableModel.

I don't know why the Columns aren't displayed but the data is displayed...

Kind Regards!

È stato utile?

Soluzione

In MyTableModel you need to override

public String getColumnName(int column)

to return your data, e.g.

public String getColumnName(int column) {
  return columnNames[column];
}

You will also want to override

getColumnCount();

and findColumnName();

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top