我有一个jtable,大约有15列,宽度不同。虽然当我横向滚动表格标题时没有移动其数据列,但每件事情都进展顺利。
我的代码:
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));