0

我有一个大约 15 列不同宽度的 jtable。尽管当我水平滚动表格标题时,一切都进展顺利,但它的数据列并没有移动。

我的代码:

    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.....

我得到了正确的代码。工作正常。

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

0 回答 0