I have JTextPane
from Netbeans designer's section. I want to add columns and rows on it. However, there is no option for adding columns or row in the property window of JTextPane
. Is there another way to do this?
问题:
回答1:
JTextPane
supports formatted documents. One format it supports is HTML. Therefore, it is possible to use an HTML table
to provide tabular data - data in columns and rows.
Also consider using a JTable
, which (of course) has inbuilt support for columns and rows.
回答2:
JTextPane
is not meant to have "columns" and "rows": however you can alter its content through the NetBeans designer by editing its text
property (click on the ...
next to the text
property in order to open a dialog where you can enter some multiline text for your JTextPane).
回答3:
You can add your custom tables like described here http://java-sl.com/JEditorPaneTables.html
and
row/cols insert described here http://java-sl.com/JEditorPaneTablesRowColumnInsert.html
回答4:
you can override JTextPane class methods and
add columns and rows to 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);
}
}
}