jtextpane

How to change the color of specific words in a JTextPane?

 ̄綄美尐妖づ 提交于 2019-11-26 08:13:21
问题 How do I change the color of specific words in a JTextPane just while the user is typing? Should I override JTextPane paintComponent method? 回答1: Overwriting paintComponent will not help you. This is not an easy one, but not impossible either. Something like this will help you: DefaultStyledDocument document = new DefaultStyledDocument(); JTextPane textpane = new JTextPane(document); StyleContext context = new StyleContext(); // build a style Style style = context.addStyle("test", null); //

Centering Text in a JTextArea or JTextPane - Horizontal Text Alignment

主宰稳场 提交于 2019-11-26 07:46:22
问题 Is there a way to create horizontally centered text for a JTextArea like with a JTextField? setHorizontalAlignment(JTextField.CENTER); Is there a way I can accomplish the same thing with a multi-line text area? I can\'t find a method for it with JTextArea, so is there another option? JTextPane? If so, how? 回答1: You need to use a JTextPane and use attributes. The following should center all the text: StyledDocument doc = textPane.getStyledDocument(); SimpleAttributeSet center = new

JTextPane appending a new string

点点圈 提交于 2019-11-26 07:32:22
问题 In an every article the answer to a question \"How to append a string to a JEditorPane?\" is something like jep.setText(jep.getText + \"new string\"); I have tried this: jep.setText(\"<b>Termination time : </b>\" + CriterionFunction.estimateIndividual_top(individual) + \" </br>\"); jep.setText(jep.getText() + \"Processes\' distribution: </br>\"); And as a result I got \"Termination time : 1000\" without \"Processes\' distribution:\" Why did this happen??? 回答1: I doubt that is the recommended

JTextPane formatting [closed]

偶尔善良 提交于 2019-11-26 06:49:14
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . I have a JTextPane where I want to add lines and depending on their content have them have a different formatting. Currently I have this StyleContext context = new StyleContext(); StyledDocument document = new

Keeping the correct style on text retrieval

允我心安 提交于 2019-11-26 04:01:24
问题 I am making an application similar to a chat. For that purpose I have two JTextPanes, one where I\'m writing and one that displays messages. This is the code that handles the transferring of text from input to display : String input = textPane.getText(); if(!input.endsWith(\"\\n\")){ input+=\"\\n\"; } StyledDocument doc = displayPane.getStyledDocument(); int offset = displayPane.getCaretPosition(); textPane.setText(\"\"); try { doc.insertString(offset, input, set); } catch