JEditorPane mit unsichtbarem Text
javax.swing.JEditorPane
Möchte man Text in einer JEditorPane verstecken, so kann man sich eines kleines Tricks bedienen.
Man erzeugt einen Style mit eine Fontgröße von 0 Pixeln:
import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.text.BadLocationException; import javax.swing.text.Style; import javax.swing.text.StyleConstants; import javax.swing.text.StyleContext; import javax.swing.text.StyledDocument; import java.awt.BorderLayout; import java.awt.Container; public class UnvisibleText extends JFrame { public UnvisibleText() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 300); setLocationRelativeTo(null); init(); } private void init() { Container cp = getContentPane(); cp.setLayout(new BorderLayout()); JEditorPane editor = new JEditorPane(); cp.add(new JScrollPane(editor), BorderLayout.CENTER); initEditorWithText(editor); } private void initEditorWithText(JEditorPane editor) { editor.setContentType("text/rtf"); StyledDocument doc = (StyledDocument) editor.getDocument(); Style defaultStyle = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE); Style invisibleStyle = doc.addStyle("invisible", defaultStyle); StyleConstants.setFontSize(invisibleStyle, 0); StyleConstants.setForeground(invisibleStyle, editor.getBackground()); try { doc.insertString(doc.getLength(), "Hello ", null); doc.insertString(doc.getLength(), "crazy ", doc.getStyle("invisible")); doc.insertString(doc.getLength(), "world!", null); System.out.println("Gesamter Text: " + doc.getText(0, doc.getLength())); } catch (BadLocationException e) { e.printStackTrace(); } } public static void main(String[] args) { new UnvisibleText().setVisible(true); } }