JTextPaneにJSeparatorを追加する
Total: 5694, Today: 2, Yesterday: 1
Posted by aterai at
Last-modified:
Summary
JTextPaneにセパレータとして、hr要素やJSeparator、MatteBorderを設定したJLabelなどを追加します。
Screenshot

Advertisement
Source Code Examples
HTMLEditorKit kit = new HTMLEditorKit();
HTMLDocument doc = new HTMLDocument();
textPane.setEditorKit(kit);
textPane.setDocument(doc);
textPane.setEditable(false);
textPane.setText("<html><hr>:<hr />");
textPane.insertComponent(new JLabel("JSeparator: "));
textPane.insertComponent(new JSeparator(JSeparator.HORIZONTAL));
insertBR(kit, doc);
textPane.insertComponent(new JLabel("MatteBorder1: "));
textPane.insertComponent(new JLabel() {
@Override public void updateUI() {
super.updateUI();
setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.RED));
}
@Override public Dimension getMaximumSize() {
return new Dimension(textPane.getSize().width, 1);
}
});
insertBR(kit, doc);
textPane.insertComponent(new JLabel("MatteBorder2: "));
textPane.insertComponent(new JLabel() {
@Override public void updateUI() {
super.updateUI();
setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.GREEN));
}
@Override public Dimension getPreferredSize() {
return new Dimension(textPane.getSize().width, 1);
}
@Override public Dimension getMaximumSize() {
return this.getPreferredSize();
}
});
insertBR(kit, doc);
textPane.insertComponent(new JLabel("JSeparator.VERTICAL "));
textPane.insertComponent(new JSeparator(JSeparator.VERTICAL) {
@Override public Dimension getPreferredSize() {
return new Dimension(1, 16);
}
@Override public Dimension getMaximumSize() {
return this.getPreferredSize();
}
});
textPane.insertComponent(new JLabel(" TEST"));
View in GitHub: Java, KotlinDescription
hr要素htmlEditorKit.insertHTML(doc, doc.getLength(), "<hr />", 0, 0, null)などでhr要素を追加
JSeparatortextPane.insertComponent(new JSeparator(JSeparator.HORIZONTAL))で水平JSeparatorを追加
MatteBorder1MatteBorderを設定したテキスト無しのJLabelを追加- 最大サイズが(幅:
JTextPane#getWidth()、高さ:1px)になるようJLabel#getMaximumSize()をオーバーライド
MatteBorder2MatteBorderを設定したテキスト無しのJLabelを追加- 推奨サイズと最大サイズが(幅:
JTextPane#getWidth()、高さ:1px)になるようJLabel#getPreferredSize()とJLabel#getMaximumSize()をオーバーライド
JSeparator.VERTICALtextPane.insertComponent(new JSeparator(JSeparator.VERTICAL))で垂直JSeparatorを追加JSeparator#getPreferredSize()、JSeparator#getMaximumSize()をオーバーライドしないと表示されない- How to Use Separators (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
In most implementations, a vertical separator has a preferred height of 0, and a horizontal separator has a preferred width of 0. This means a separator is not visible unless you either set its preferred size or put it in under the control of a layout manager such as BorderLayout or BoxLayout that stretches it to fill its available display area.
- How to Use Separators (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
Reference
- How to Use Separators (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)