[英]Multiline JTable Cells with auto height- extra large first row
我正在使用Swing開發Java桌面應用程序(jdk1.6)。 我的問題是關於JTable中具有自動調整單元格高度屬性的多行單元格(文本換行)。
我已經可以用這種方式實現這個結構了:
關於這種結構的2個問題:
1)在程序執行期間,它可以對行進行計數並正確調整行高。
但是在第一次初始化時(第一個setModel()),它計算表的“第一個單元格”的行數,即(0,0),遠遠超過它。 我調試了代碼並發現它計算文本中的字母並乘以行高16.(好像單元格的寬度是1個字母)。 最后,我獲得了非常高的第一排。 其他行都可以。
當我沒有向(0,0)插入任何文本時,不會出現問題。
2)當我禁用表自動調整大小屬性並手動確定單元格寬度時,行計數不起作用。
這是我的單元格渲染器:
public class MultiLineCellRenderer extends JTextArea implements TableCellRenderer {
private JTable DependentTable;
public MultiLineCellRenderer() {
DependentTable=null;
setLineWrap(true);
setWrapStyleWord(true);
setOpaque(true);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
(...) //some background color adjustments
setText((value == null) ? "" : value.toString());
int numOfLines = getWrappedLines(this); // Counting the lines
int height_normal = table.getRowHeight(row);// read the height of the row
if(DependentTable == null) // for this case always null
{
if (height_normal < numOfLines*16)
{
table.setRowHeight(row,numOfLines*16);
}
}
else
(...)
return this;
}
這是我如何計算行數:
public static int getWrappedLines(JTextArea component)
{
View view = component.getUI().getRootView(component).getView(0);
int preferredHeight = (int)view.getPreferredSpan(View.Y_AXIS);
int lineHeight = component.getFontMetrics( component.getFont() ).getHeight();
return preferredHeight / lineHeight;
}
我將行調整代碼刪除到了類的外部。 該表現在有一個Model Listener。 第一行在這個時候並不是非常大。 調用此方法有效但問題是關於計算包裹的行。 每次我用非常長的文本填充單元格時,行都被正確包裝但我的計數器返回1.(包裝線計數器的代碼在上面。與渲染器中的相同。但它在那里工作正常)
這是我的模特聽眾:
public class ModelListener implements TableModelListener {
JTable mainTable;
JTable depTable;
public ModelListener(JTable m, JTable d) {
mainTable = m;
depTable = d;
}
public ModelListener(JTable m){
mainTable = m;
depTable = null;
}
public void tableChanged(TableModelEvent tme) {
int fRow = tme.getFirstRow();
int col = tme.getColumn();
JTextArea cellArea = (JTextArea)mainTable.getDefaultRenderer(Object.class);
int numOfLines = getWrappedLines(cellArea); //countLines();
int height_normal = mainTable.getRowHeight(fRow);
System.out.println("h normal:"+height_normal);
System.out.println("numLines:"+numOfLines);
System.out.println("value:"+mainTable.getModel().getValueAt(fRow, col));
System.out.println("width:"+cellArea.getPreferredSize().width);
if(depTable == null)
{
if (height_normal < numOfLines*16)
{
mainTable.setRowHeight(fRow,numOfLines*16);
}
}
else
{
//(---)
}
mainTable.repaint();
}
印刷品的結果:
- preferredHeight:15 //來自包裹線的功能
- lineHeight:15 //來自包裹線的功能
- 正常:25
- numLines:1
- 價值:looooooooooooooooooooooonng tessssssssssssssssssssssssssst //似乎是2行
- 寬度:104
提前致謝 :)
ISIL
只是為了立即獲得解決方案,即使在幾年后,它可能會幫助其他人。 您可以設置固定數量的行或使用適合內容的動態行計數。 你應該很容易以任何方式擴展它。
package com.mycompany;
import java.awt.Component;
import java.util.List;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableCellRenderer;
public class MultiLineCellRenderer extends JTextArea implements
TableCellRenderer {
private static final long serialVersionUID = 1L;
boolean limit = false;
public MultiLineCellRenderer() {
setLineWrap(true);
setWrapStyleWord(true);
setOpaque(true);
setBorder(new EmptyBorder(-1, 2, -1, 2));
this.limit = false;
setRows(1);
}
public MultiLineCellRenderer(int rows) {
this();
setRows(rows);
this.limit = true;
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
setText(value == null ? "" : value.toString());
int dynamicHeight = getLineCount() > getRows() && this.limit ? getRows() : getLineCount();
int newHeight = table.getRowHeight() * dynamicHeight;
if (table.getRowHeight(row) != newHeight) {
table.setRowHeight(row, newHeight);
}
if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
}
else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
return this;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.