2012-10-21 18 views

回答

2

使用JTable.setRowHeight方法的基礎上JTextArea

+2

我不明白這將如何幫助他自動調整排***寬度***。你有沒有完全閱讀他的問題? –

+0

我的不好:)由於某種原因,我把它理解爲身高。他仍然可能意味着身高,所以我會留下答案,直到他爲我們清理它。 –

+0

@HovercraftFullOfEels a _row_不能有寬度,IMO .. – kleopatra

0

這取決於所呈現什麼,如果你知道電話號碼,如果像素...

改變列寬度將影響組合和單元格渲染由列自動調整大小政策。

如果你想設置的像素寬度...

int columnIndex = //... the index of the column 
TableColumnModel columnModel = table.getColumnModel(); 
TableColumn tableColumn = columnModel.getColumn(columnIndex); 
tableColumn.setWidth(pixelWidth); 

如果列渲染文本,你知道該列將顯示你可以得到的字體規格爲渲染的字符數/表...

// If you're using a cell renderer, you will need to get the cell renderer 
// to get th font metrics 
FontMerics fm = table.getFontMetrics(table.getFont()); 
int pixelWidth = fm.stringWidth("M") * characterCount; 

如果你不使用基於文本的渲染器,您可以使用渲染器來獲得寬度的一些想法...

TableCellRenderer renderer = table.getCellRenderer(0, columnIndex); 
// You can also use table.getDefaultRenderer(Class) 
Component component = renderer.getTableCellRendererComponent(table, 
            prototypeValue, // Some value the best represents the average value 
            false, 
            false, 
            0, 
            columnIndex); 
int width = component.getPreferredWidth().width; 
+0

-1爲fontMetrics ..這是你的東西_never_(模仿通常非常奇怪的角落案例;-)也不需要做:簡單地查詢準備好的渲染器的前綴。 – kleopatra

相關問題