0

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!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 1
    this question isn't answerable, feels like as give me code, please to read [Oracles JTable tutorial](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data), then edit your question with an [SSCCE](http://sscce.org/), short, runnable, compilable, just about JFrame with JTable inside JScrollPane, – mKorbel Apr 30 '13 at 21:15
  • "A", "B", "C" are default values implemented in API, in the case when value for JTableHeader isn't accesible – mKorbel Apr 30 '13 at 21:17
  • 1
    @mKorbel i made the SSCCE but got the answer now so i won't add it... Thank you for the tip .. – XenonUnlimited Apr 30 '13 at 21:26

1 Answers1

2

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();

user949300
  • 15,364
  • 7
  • 35
  • 66
  • In your case you can probably use a DefaultTableModel to save a little work. But, in general, DefaultTableModel stinks - better to do what you did and write your own subclass of AbstractTableModel. – user949300 Apr 30 '13 at 21:30