0

I'm using this code for my application: https://stackoverflow.com/a/8187799. I need to display a table with custom titles on the columns AND the rows, hence the JTableRowHeader.

I understand how to change the rows name with this code, but I can't find where I'm supposed to change the column's name. I'm kinda struggling with this since I'm not really familiar with the concept.

I tried to add this code in the model = new DefaultTableModel() but it doesn't work, the columns are still labelled as A, B, C, etc.:

@Override
public String getColumnName(int column) {
    switch (column) {
    case 0: //First column name:
        return "Name1";
    case 1: //Second column name:
        return "Name2";
    //case 2: More names ....
    default: // other columns that are not defined above
    // using default in a switch statement is always the best practice
    return "";
    }
}  

EDIT: My code is the same as the one that I linked, except that I added the method getColumnName(int index) here, but it doesn't work:

model = new DefaultTableModel() {

        private static final long serialVersionUID = 1L;

        @Override
        public int getColumnCount() {
            return 1;
        }

        @Override
        public boolean isCellEditable(int row, int col) {
            return false;
        }

        @Override
        public int getRowCount() {
            return JTableRow.this.getRowCount();
        }

        @Override
        public Class<?> getColumnClass(int colNum) {
            switch (colNum) {
            case 0:
                return String.class;
            default:
                return super.getColumnClass(colNum);
            }
        }

        @Override
        public String getColumnName(int column) {
            return "test";
        }


    };
Community
  • 1
  • 1
pioupiou1211
  • 353
  • 4
  • 16
  • did you try `repaint` table and container? – Sarz Apr 05 '17 at 07:31
  • @Sarz Nop, I just learned the existence of JTable yesterday so I don't really know when to perform this? – pioupiou1211 Apr 05 '17 at 07:32
  • "I tried to add this code in the `model = new DefaultTableModel()`" - how did you add it? Did you create a subclass of `DefaultTableModel`? Did you actually replace the model? It might be best if you'd post a [mcve]. – Thomas Apr 05 '17 at 07:37
  • 1
    Consider providing a runnable example which demonstrates your problem - it removes most of the guess work – MadProgrammer Apr 05 '17 at 07:39
  • I edited my post. I didn't give the entire code because it is the exact same as the linked one, except for the change that I explained (made in order to try to rename the columns). If you still need one I can post it but I though that it wouldn't be necessary. – pioupiou1211 Apr 05 '17 at 07:41
  • Try something like this: `jTable1.getColumnModel().getColumn(columnNumber).setHeaderValue(newColumnName); jTable1.getTableHeader().resizeAndRepaint();` – DevilsHnd - 退職した Apr 05 '17 at 07:52

1 Answers1

2

If you have a look at the code you seem to have copied you'll notice that there are 2 tables being used: table for the actual data and headerTable for the row headers. The model you've been changing is used for headerTable only so it won't affect the columns you see since those are provided by table.

I won't/can't comment on why 2 tables are used (it still seems odd) so I'll just focus on the column names: set them on table.

One way would be to provide your own table model, another might be to readjust them afterwards:

for( int i = 0; i < table.getColumnCount(); i++ ) {
  table.getColumnModel().getColumn( i ).setHeaderValue( "Column " + i );
}
Thomas
  • 87,414
  • 12
  • 119
  • 157
  • Thanks for you help, it works great (I chose to rename the columns afterward). For the 2 tables, I thought that it was necessary in order to have titles on my rows. If you have a shorter solution, I'll be glad to read it! – pioupiou1211 Apr 05 '17 at 07:53
  • 1
    @pioupiou1211 well, I'd probably provide my own table model that contains my data as well as the row headers as column 0. Then I'd probably set the cell and header renderers like the code you've been using but there would be no need to use 2 synchronized tables. I don't have the time to provide full code but I'd probably not do it anyways, you're to learn something yourself after all and imo people learn best if they try themselves :) – Thomas Apr 05 '17 at 08:07
  • well thanks for the overview! It give me a pretty good idea of what to do. I'll give it a try if I got some time to get back to it. – pioupiou1211 Apr 05 '17 at 08:25