JTextPane可以有列和行吗?

时间:2012-01-24 00:54:42

标签: java swing jtextpane tabular

我有来自Netbeans设计师部分的JTextPane。我想在其上添加列和行。但是,在JTextPane的属性窗口中无法添加列或行。还有另一种方法吗?

4 个答案:

答案 0 :(得分:5)

JTextPane支持格式化文档。它支持的一种格式是HTML。因此,可以使用HTML table来提供表格数据 - 列和行中的数据。


还要考虑使用JTable,它(当然)内置了对列和行的支持。

答案 1 :(得分:4)

JTextPane并不意味着拥有“列”和“行”:但是您可以通过编辑其text属性(单击 ... 属性旁边的> text ,以打开一个对话框,您可以在其中为JTextPane输入一些多行文字

答案 2 :(得分:3)

您可以添加自定义表格,如此处所述 http://java-sl.com/JEditorPaneTables.html

此处描述的row / cols插入 http://java-sl.com/JEditorPaneTablesRowColumnInsert.html

答案 3 :(得分:1)

您可以重写JTextPane类方法和
将列和行添加到JTextPane:

import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Insets;
import java.awt.Rectangle;

import javax.swing.JTextPane;
import javax.swing.SwingConstants;
import javax.swing.text.StyledDocument;

public class TextPane extends JTextPane {

    private static final long serialVersionUID = 712368129156703980L;

    private int rows, columns, rowHeight, columnWidth;

    public TextPane() {

        super();

        rows = 0;
        columns = 0;

    }

    public TextPane(StyledDocument doc) {

        super(doc);

        rows = 0;
        columns = 0;

    }

    public TextPane(int rows, int columns) {

        super();

        this.rows = rows;
        this.columns = columns;

    }

    public TextPane(StyledDocument doc, int rows, int columns) {

        super(doc);

        this.rows = rows;
        this.columns = columns;

    }

    public int getRows() {
        return rows;

    }

    public void setRows(int rows) {

        if (rows < 0)
            throw new IllegalArgumentException("rows less than zero.");

        if (rows != this.rows) {

            this.rows = rows;
            revalidate();

        }

    }

    public int getRowHeight() {

        if (rowHeight == 0) {

            FontMetrics metrics = getFontMetrics(getFont());
            rowHeight = metrics.getHeight();

        }

        return rowHeight;

    }

    public int getColumns() {
        return columns;

    }

    public void setColumns(int columns) {

        if (columns < 0)
            throw new IllegalArgumentException("columns less than zero.");

        if (columns != this.columns) {

            this.columns = columns;
            revalidate();

        }

    }

    public int getColumnWidth() {

        if (columnWidth == 0) {

            FontMetrics metrics = getFontMetrics(getFont());
            columnWidth = metrics.charWidth('m');

        }

        return columnWidth;
    }

    public Dimension getPreferredSize() {

        Dimension d = super.getPreferredSize();

        d = (d == null) ? new Dimension(400, 400) : d;
        Insets insets = getInsets();

        if (columns != 0)
            d.width = Math.max(d.width, columns * columnWidth + insets.left + insets.right);

        if (rows != 0)
            d.height = Math.max(d.height, rows * rowHeight + insets.top + insets.bottom);

        return d;

    }

    protected String paramString() {

        return super.paramString() + ",columns" + columns + ",columnWidth" + columnWidth + ",rows" + rows
                + ",,rowsHeight" + rowHeight;

    }

    public Dimension getPreferredScrollableViewportSize() {

        Dimension size = super.getPreferredScrollableViewportSize();
        size = (size == null) ? new Dimension(400, 400) : size;
        Insets insets = getInsets();

        size.width = (columns == 0) ? size.width : columns * getColumnWidth() + insets.left + insets.right;

        size.height = (rows == 0) ? size.height : rows * getRowHeight() + insets.top + insets.bottom;

        return size;


    }

     public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {

            switch (orientation) {

            case SwingConstants.VERTICAL:
                return getRowHeight();

            case SwingConstants.HORIZONTAL:
                return getColumnWidth();

            default:
                throw new IllegalArgumentException("Invalid orientation: " + orientation);

            }

        }



}