現在、さまざまなストリームからのテキストを表示するJTextPaneがあります。ユーザーがテキストがどのストリームから来たかを知る方法は、各ストリームからのテキストがそれに異なるスタイルを持っているということです。さまざまなテキストを除外できるように、テキストを非表示にするスタイルを作成する方法はありますか?
ありがとうございました。
0のフォントサイズを使用し、コンポーネントの背景を一致させることで、(一種の)偽物を作成できます。
public static void main(String[] args) throws Exception {
JTextPane pane = new JTextPane();
Style regular = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
Style invisible = pane.getStyledDocument().addStyle("invisible", regular);
StyleConstants.setFontSize(invisible, 0);
StyleConstants.setForeground(invisible, pane.getBackground());
pane.getStyledDocument().insertString(pane.getStyledDocument().getLength(),
"Hello, ", null);
pane.getStyledDocument().insertString(pane.getStyledDocument().getLength(),
"cruel ", pane.getStyledDocument().getStyle("invisible"));
pane.getStyledDocument().insertString(pane.getStyledDocument().getLength(),
"world!", null);
pane.setPreferredSize(new Dimension(500, 500));
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.add(pane, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack(); frame.setVisible(true);
}
上記の非表示の文字列の長さは、表示されているコンポーネント間のスペースに影響を与えていないようです。ただし、ペインからコピーするとわかるように、まだそこにありますのでご安心ください。