jtextpane

Centering Text in a JTextArea or JTextPane - Horizontal Text Alignment

断了今生、忘了曾经 提交于 2019-11-26 22:47:59
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? You need to use a JTextPane and use attributes. The following should center all the text: StyledDocument doc = textPane.getStyledDocument(); SimpleAttributeSet center = new SimpleAttributeSet(); StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER); doc.setParagraphAttributes(0,

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

故事扮演 提交于 2019-11-26 22:11:34
How do I change the color of specific words in a JTextPane just while the user is typing? Should I override JTextPane paintComponent method? 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); // set some style properties StyleConstants.setForeground(style, Color.BLUE); // add some data to the document

Part 2 - How do I get consistent rendering when scaling a JTextPane?

穿精又带淫゛_ 提交于 2019-11-26 22:07:38
问题 I submitted another version of this question and a sample program before: How do I get consistent rendering when scaling a JTextPane? Recapitulating the problem: I would like to allow users to zoom into or out of a non-editable JTextPane. Running the example program submitted in the earlier question, which simply scaled the Graphics object, resulted in inconsistent spacing between runs of bold text and non-bold text. The sample program below attempts to solve the problem by drawing the text

JTextPane appending a new string

狂风中的少年 提交于 2019-11-26 20:08:15
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??? camickr I doubt that is the recommended approach for appending text. This means every time you change some text you need to reparse the entire document. The

JEditorPane set foreground color for different words

放肆的年华 提交于 2019-11-26 18:39:54
问题 I am currently working on a text editor, and I have this code which make the background of the specific word different from the others, but I want the foreground to edit color and not the background. Here's the code I have: import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.JTextPane; import javax.swing.text

JEditorPane with Javascript and CSS support

孤人 提交于 2019-11-26 16:56:36
问题 I am working on Swing using JEditorPane but it's not supporting the Javascript or some advanced tag like <object> etc. and not supporting the color, font style size etc. Is there any solution so that editor pane can display the HTML file as it is being displayed in normal HTML browser? 回答1: current Java6/7 supporting only (upto) Html 3.2 with reduced support for css , for Html5 and quite full css support to use JavaFx Components , there are custom Java libraries with (full???) support of

How can I set each character to a different color/background color in a JTextPane?

喜欢而已 提交于 2019-11-26 16:54:34
问题 I've been searching for this for a while and so far all I've been able to come up with is how to create a style and apply it to a character like so: StyledDocument doc = (StyledDocument) new DefaultStyledDocument(); JTextPane textpane = new JTextPane(doc); textpane.setText("Test"); javax.swing.text.Style style = textpane.addStyle("Red", null); StyleConstants.setForeground(style, Color.RED); doc.setCharacterAttributes(0, 1, textpane.getStyle("Red"), true); This is useful if you have only a few

Keeping the correct style on text retrieval

倖福魔咒の 提交于 2019-11-26 14:47:21
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 (BadLocationException ex) { Logger.getLogger(ChatComponent.class.getName()).log(Level.SEVERE, null, ex); } The problem is that if i

JTextPane highlight text

半世苍凉 提交于 2019-11-26 11:18:40
问题 Can I highlight some text into a JTextPane starting from a value and ending from another value like the following but with the yellow color? \"\" JTextPane highlight text \"\" Thanks. 回答1: As often there are several possibilities, depending on what you really mean by "highlight":-) Highlight by changing any style attributes of arbitrary text parts on the document level, something like SimpleAttributeSet sas = new SimpleAttributeSet(); StyleConstants.setForeground(sas, Color.YELLOW); doc

How to add text different color on JTextPane

情到浓时终转凉″ 提交于 2019-11-26 11:11:32
问题 Can anybody help me with simple log, I have to add at first line on JTextPane log messages with chosen color ( green ok, red failure ). How to achieve this ? 回答1: This will print out "BLAH BLEG" in two different colors. public class Main { public static void main(String[] args) { JTextPane textPane = new JTextPane(); StyledDocument doc = textPane.getStyledDocument(); Style style = textPane.addStyle("I'm a Style", null); StyleConstants.setForeground(style, Color.red); try { doc.insertString