gpt4 book ai didi

java - JTable 单元格可变高度和通过 HTML 标签着色字符串

转载 作者:行者123 更新时间:2023-12-02 05:16:30 25 4
gpt4 key购买 nike

我有一个JTable由于内容的原因,单元格的高度可变。我通过使用 JTextArea 来完成此操作在我的TableCellRenderer 。我想为 String 的部分着色以不同的颜色。 JTextPane支持 HTML 标签,而文本区域不支持,但对于文本 Pane ,无法更改单元格的高度。

知道如何使用可变单元高度和字符串颜色 JTable

public class LineWrapCellRenderer extends JTextPane implements TableCellRenderer {

int rowHeight = 0; // current max row height for this scan
final int paddingRight = 4;
final int paddingLeft = 4;
Border padding = BorderFactory.createEmptyBorder(5, paddingLeft, 5, paddingRight);


@Override
public Component getTableCellRendererComponent(
JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column){

setContentType("text/html");
setText(setHTML((String) value));
setSelectionColor(Color.BLUE);


this.setBorder(padding);//Abstände der Zeilen

//markieren fals selektiert
if (isSelected){
setBackground(table.getSelectionBackground());
// setForeground(table.getSelectionForeground());
}
else
{
setBackground(table.getBackground());
// setForeground(table.getForeground());
}

// current table column width in pixels
int colWidth = table.getColumnModel().getColumn(column).getWidth() + table.getIntercellSpacing().width;

// set the text area width (height doesn't matter here)
Dimension dim = new Dimension(colWidth, 1);
setSize(dim);

// get the text area preferred height and add the row margin
int height = getPreferredSize().height + table.getRowMargin();


// ensure the row height fits the cell with most lines
if (height != table.getRowHeight(row)) {
table.setRowHeight(row, height);
}

return this;
}

将此代码与 JTextPane 结合使用对单元格高度没有影响。使用与 JTextArea 相同的代码来调整高度。

最佳答案

您可以使用以下方法设置/更改每个单独行的高度:

JTable.setRowHeight(int row, int rowHeight);

对于单元格中显示的文本部分的着色,您可以简单地使用 HTML 代码,例如

String value = "<html>Following word is <font color=\"red\">RED</font>.</html>";

默认表格单元格渲染器 ( DefaultTableCellRenderer ) 使用/扩展 JLabel它可以正确处理/接受 HTML 代码。

手动高度

请参阅此示例:

JFrame f = new JFrame("Test");

JTable t = new JTable();
((DefaultTableModel)t.getModel()).setDataVector(new Object[][]{
{"<html>Next word is <font color=\"red\">RED</font>.</html>", "50px"},
{"<html>Following is <font color=\"blue\">BLUE</font>.<br><br>"
+ "Long lines are automatically wrapped"
+ " as this long line demonstrates it.</html>", "150px"},
}, new Object[]{"Formatted text","Height"});
t.setRowHeight(0, 50);
t.setRowHeight(1, 150);
f.add(new JScrollPane(t));

f.pack();
f.setVisible(true);

结果:

enter image description here

打包/Authoheight

如果您想“打包”所有行以获得该值所需的最小高度,您可以这样做:

JFrame f = new JFrame("Test");

JTable t = new JTable();
((DefaultTableModel) t.getModel()).setDataVector(new Object[][] {
{"<html>Next word is <font color='red'>RED</font>.</html>", "50px" },
{"<html><body style='width:200px'>Following is"
+ " <font color='blue'>BLUE</font>.<br><br>"
+ "Long lines are automatically wrapped "
+ "as this long line demonstrates it.</body></html>", "150px" }, },
new Object[] {"Formatted text", "Height"});

for (int i = 0; i < t.getRowCount(); i++)
t.setRowHeight(i, t.getCellRenderer(i, 0)
.getTableCellRendererComponent(t, t.getValueAt(i, 0), false, false, i, 0)
.getPreferredSize().height);

f.add(new JScrollPane(t));

f.pack();
f.setVisible(true);

基本上迭代行,并从渲染器询问首选高度,并将其设置为行高。

注意:这需要设置 width自动换行为多行的 HTML 值中的样式。如果您不这样做,HTML 值的首选高度将是不自动换行长行的首选高度(仍会考虑像 <br> 标签这样的手动换行)。

这将导致这样的结果:

enter image description here

关于java - JTable 单元格可变高度和通过 HTML 标签着色字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26903479/

25 4 0