JTextPaneにJSeparatorを追加する
Total: 5083
, Today: 1
, Yesterday: 0
Posted by aterai at
Last-modified:
概要
JTextPane
にセパレータとして、hr
要素やJSeparator
、MatteBorder
を設定したJLabel
などを追加します。
Screenshot
Advertisement
サンプルコード
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, Kotlin解説
hr
要素htmlEditorKit.insertHTML(doc, doc.getLength(), "<hr />", 0, 0, null)
などでhr
要素を追加
JSeparator
textPane.insertComponent(new JSeparator(JSeparator.HORIZONTAL))
で水平JSeparator
を追加
MatteBorder1
MatteBorder
を設定したテキスト無しのJLabel
を追加- 最大サイズが(幅:
JTextPane#getWidth()
、高さ:1px
)になるようJLabel#getMaximumSize()
をオーバーライド
MatteBorder2
MatteBorder
を設定したテキスト無しのJLabel
を追加- 推奨サイズと最大サイズが(幅:
JTextPane#getWidth()
、高さ:1px
)になるようJLabel#getPreferredSize()
とJLabel#getMaximumSize()
をオーバーライド
JSeparator.VERTICAL
textPane.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)
参考リンク
- How to Use Separators (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)