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

时间:2014-11-13 07:27:50

标签: java swing jtable jtextarea jtextpane

我有一个JTable单元格因为内容而具有可变高度。我在JTextArea中使用了TableCellRenderer来完成此操作。我想以不同颜色为String的部分着色。 JTextPane支持HTML标记和文本区域,但是在文本窗格中无法更改单元格的高度。

任何想法如何使用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相同的代码调整高度。

1 个答案:

答案 0 :(得分:2)

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

JTable.setRowHeight(int row, int rowHeight);

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

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

默认表格单元格渲染器(DefaultTableCellRenderer)使用/ extends 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

如果你想&#34;打包&#34;所有行都具有值所需的最小高度,您可以这样做:

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);

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

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

这将导致:

enter image description here

相关问题