0

I have a jtable with about 15 columns with different widths. Every thing is going well though when I scroll horizontally table headings are not moving with its data columns.

My Code :

    tblJournalBatchList.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);        

    String ColumnNames []= {"<HTML><B>Batch No.</B></HTML>",
                            "<HTML><B>Date</B></HTML>","<HTML><B>Description</B></HTML>",
                            "<HTML><CENTER><B>Ready to<BR>Post</B></CENTER></HTML>,.....MORE COLUMN HEADINGS...."};
    DefaultTableModel DTM = new DefaultTableModel(){        
    public boolean isCellEditable(int row, int column)
    {
        if (column == 0 || column == 2){
        return false;
        }
        return true;
    }        
    };        
    DTM.setColumnIdentifiers(ColumnNames);        
    DTM.setRowCount(10);
    DTM.setColumnCount(15);
    tblJournalBatchList.setRowHeight(20);
    tblJournalBatchList.setModel(DTM);
    tblJournalBatchList.getTableHeader().setPreferredSize(
                        new Dimension(tblJournalBatchList.getColumnModel().getTotalColumnWidth(), 50));                


    tblJournalBatchList.setPreferredSize(new Dimension(900,700));

    TableColumn col [] = new TableColumn [4];
    tblJournalBatchList.getColumnModel().getColumn(0);
    tblJournalBatchList.getColumnModel().getColumn(1);
    tblJournalBatchList.getColumnModel().getColumn(2);
    tblJournalBatchList.getColumnModel().getColumn(3);
    ....MORE.....

    col[0].setPreferredWidth(100);
    col[1].setPreferredWidth(100);
    col[2].setPreferredWidth(500);
    col[3].setPreferredWidth(100);
    ....MORE.....

I got the right code. Working fine.

    tblJournalBatchList.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);        

    String ColumnNames []= {"<HTML><B>Batch No.</B></HTML>",
                            "<HTML><B>Date</B></HTML>","<HTML><B>Description</B></HTML>",
                            "<HTML><CENTER><B>Ready to<BR>Post</B></CENTER></HTML>.....MORE COLUMNS....."};

    DefaultTableModel DTM = new DefaultTableModel(null,ColumnNames){        
    public boolean isCellEditable(int row, int column)
    {
        if (column == 0 || column == 2){
        return false;
        }
        return true;
    }        
    };                   
    DTM.setRowCount(100);
    DTM.setColumnCount(15);
    tblJournalBatchList.setRowHeight(20);
    tblJournalBatchList.setModel(DTM);              

    tblJournalBatchList.getColumnModel().getColumn(0).setPreferredWidth(70);
    tblJournalBatchList.getColumnModel().getColumn(1).setPreferredWidth(70);
    tblJournalBatchList.getColumnModel().getColumn(2).setPreferredWidth(300);
    tblJournalBatchList.getColumnModel().getColumn(3).setPreferredWidth(70);
    ..........MORE..............

    tblJournalBatchList.getTableHeader().setPreferredSize(new Dimension(10000,32));
Mahesh
  • 109
  • 1
  • 2
  • 12
  • Mind showing us some of your GUI's code so that we may spot the error? – Tdorno Jul 23 '13 at 05:27
  • 2
    why do you set the `preferredSize` of the `TableHeader`? – htz Jul 23 '13 at 05:58
  • coz I need to Highlight the header rather than rows. It gives nice appearance to the Table. – Mahesh Jul 23 '13 at 06:07
  • 2
    this code not talking something about your issue, is sterile cut_off, for better help sooner popst an [SSCCE](http://sscce.org/), short, runnable, compilable, caused a.m. issue – mKorbel Jul 23 '13 at 06:36
  • 2
    [don't use setXXSize](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi/7229519#7229519), ever! And please learn java naming conventions and stick to them – kleopatra Jul 23 '13 at 06:38
  • I got the right code. – Mahesh Jul 23 '13 at 14:23
  • @Mahesh, htz has given you the answer. Do NOT set the preferred size of the table header. The size of the header will automatically be calculated based on the width of every column in the table. – camickr Jul 23 '13 at 15:07
  • yes you right. I got my point. thanks. – Mahesh Jul 25 '13 at 07:57

0 Answers0