0
我從MySql
拉數據庫中的記錄,我能夠做到這一點使用下面的方法顯示結果集的JTable中的細胞具有多
private void txt_clientNameKeyReleased(java.awt.event.KeyEvent evt) {
String sql = "Select client_id as 'Client ID', name_lastName as 'Full Name', case_number as 'Case #', hearing_date as 'Hearing Date', rtc_mtc as 'RTC/MTC', place as 'Place', status as 'Status', note as 'Notes', billing as 'Billed' from hearing_history where name_lastName like '%"+ txt_clientName.getText() +"%' ";
try{
if(conn==null){
conn = ConnectDB.ConDB();
}else{
conn = ConnectDB.ConDB();
}
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
jTable1.setModel(DbUtils.resultSetToTableModel(rs));
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}finally {
close();
}
}
它工作正常,但我有應該有一個多行一列或者包裝單詞以增加單元格的高度。
我現在使用java swing 4個月了,我還沒有看到/遇到了高級技巧來操作jtable
,因爲我希望它能夠執行。
我搜索這個網站,發現從這個環節做到最簡單的辦法
Auto adjust the height of rows in a JTable
從camickr
private void updateRowHeights()
{
try
{
for (int row = 0; row < table.getRowCount(); row++)
{
int rowHeight = table.getRowHeight();
for (int column = 0; column < table.getColumnCount(); column++)
{
Component comp = table.prepareRenderer(table.getCellRenderer(row, column), row, column);
rowHeight = Math.max(rowHeight, comp.getPreferredSize().height);
}
table.setRowHeight(row, rowHeight);
}
}
catch(ClassCastException e) {}
}
這確實是一個不錯的代碼,我想,但我試過了,我注意到高度從16
(默認行高)增加到20
,所以我仍然擁有e在單元格中的一行。
我行
jTable1.setModel(DbUtils.resultSetToTableModel(rs));
updateRowHeights();
我真的很感激任何幫助,將會把我的腳在正確的軌道上,使這項工作對我以後調用此方法..提前
謝謝..
謝謝alex2410,但我需要的代碼應該自動提供行的高度。行高將取決於字符串的長度。一些將有兩條線,一條將有三條或更多,一些只有一條(單條)線。我仍然試圖找出它,並會欣賞,如果有人可以展示一種方式來做到這一點:) – pandoy 2014-10-21 12:51:07
你可以做到這一點,只需在'getTableCellEditorComponent' /'getTableCellRendererComponent'返回'area',但你需要使用'JScrollPane' 'JTeatArea'。 – alex2410 2014-10-21 12:56:47
@pandoy嘗試我編輯的代碼。 – alex2410 2014-10-21 13:02:42