I'm using a text area in Netbeans(Java), and I want to highlight certain keywords in the text, something like syntax-highlighting in programming. How could I do that but within a JTextArea in Netbeans?
You can't use a JTextArea to highlight individual pieces of text.
I would suggest a JTextPane so you can use styled attributes.
The basic code would be something like:
JTextPane textPane = new JTextPane();
textPane.setText( "one\ntwo\nthree\nfour\nfive\nsix\nseven\neight" );
StyledDocument doc = textPane.getStyledDocument();
// Define a keyword attribute
SimpleAttributeSet keyWord = new SimpleAttributeSet();
StyleConstants.setForeground(keyWord, Color.RED);
StyleConstants.setBackground(keyWord, Color.YELLOW);
// Change attributes on some text
doc.setCharacterAttributes(0, 5, keyWord, false);
Related
I tired from search the solution for this: i want to align text pane from left to right however but drag and drop java this is last code i written :
` StyledDocument doc = txtio.getStyledDocument();
Style style = txtio.addStyle("right",null);
StyleConstants . setAlignment (style, StyleConstants .ALIGN_RIGHT);
try {
doc.insertString(0,txtio.getSelectedText(), style);
}
catch (BadLocationException ex) {
Logger . getLogger ( mswordframe.class.getName() ).log(Level.SEVERE,null, ex);
}
txtio.setStyledDocument(doc);`
txtio : is the name of text pane;
it doesn't work ,
Sorry i am weak in english
JTextPane supports character and paragraph attributes. Character attributes are for pieces of text and paragraph attributes are for a whole line of text.
Alignment of text is a paragraph attribute because you can't have part of the text center aligned and part right aligned for the same line of text.
Try the following:
SimpleAttributeSet green = new SimpleAttributeSet();
StyleConstants.setForeground(green, Color.GREEN);
SimpleAttributeSet right = new SimpleAttributeSet();
StyleConstants.setAlignment(right, StyleConstants.ALIGN_RIGHT);
try
{
doc.insertString(0, txtio.getSelectedText(), green);
doc.setParagraphAttributes(0, 1, right, false);
}
catch(Exception e) {}
I have a JTextArea in which i want to display messages aligned to right or left, depending on a variable I pass along with the message. Can I do that?
What did I do wrong? Nothing gers added to the text pane when i run my GUI
battleLog = new JTextPane();
StyledDocument bL = battleLog.getStyledDocument();
SimpleAttributeSet r = new SimpleAttributeSet();
StyleConstants.setAlignment(r, StyleConstants.ALIGN_RIGHT);
try {
bL.insertString(bL.getLength(), "test", r);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
Not with a JTextArea.
You need to use a JTextPane which supports different text and paragraph attributes. An example to CENTER the text:
JTextPane textPane = new JTextPane();
textPane.setText("Line1");
StyledDocument doc = textPane.getStyledDocument();
// Define the attribute you want for the line of text
SimpleAttributeSet center = new SimpleAttributeSet();
StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
// Add some text to the end of the Document
try
{
int length = doc.getLength();
doc.insertString(doc.getLength(), "\ntest", null);
doc.setParagraphAttributes(length+1, 1, center, false);
}
catch(Exception e) { System.out.println(e);}
if("left".equals(input)){
setAlignmentX(Component.LEFT_ALIGNMENT);
}
Have a try!
I would like to add HTML in my JEditorPane but the text is not displayed correctly.
Also, when the text's height is greater that the editor's height,
the the cursor goes to the last line of the scroll Pane.
My code is as follows:
JPanel JPInfo = new JPanel(new BorderLayout());
JPInfo.setBorder(BorderFactory.createTitledBorder("Information"));
editorPaneInfo = new JEditorPane();
editorPaneInfo.setEditable(false);
editorPaneInfo.setText("<html><p style=\"color:green\"> Test Test </p></html>");
JScrollPane editorScrollPaneInfo = new JScrollPane(editorPaneInfo);
editorScrollPaneInfo.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JPInfo.add(editorScrollPaneInfo,BorderLayout.CENTER);
SOLVED:
I added the following line before setText
editorPaneInfo.setContentType("text/html");
My problem was solved:
I added the following line before .setText()
editorPaneInfo.setContentType("text/html");
I want to add a global AttributeSet to my JTextPane.
I found this:
SimpleAttributeSet style = new SimpleAttributeSet();
StyleConstants.setLeftIndent(style, 20);
StyleConstants.setFirstLineIndent(style, -20);
From http://java-sl.com/tip_hanging_first_line.html
I'm wondering how I can set the "default stylesheet"? (not using HTML). I then tried this:
StyleContext style = new StyleContext();
Style s = style.addStyle("test", null);
StyleConstants.setForeground(s, Color.BLUE);
StyledDocument d = (StyledDocument) console.getOutputField().getDocument();
From http://www.java2s.com/Code/Java/Swing-JFC/JTextPaneStylesExample1.htm without luck.
I know StyledDocument has specific properties for setting stuff like foreground colour - which is why this may not work - but can anyone point me as to how to use the other style attributes? Such as the left indent and first line indent.
JTextPane textPane = new JTextPane();
StyledDocument doc = textPane.getStyledDocument();
SimpleAttributeSet style = new SimpleAttributeSet();
StyleConstants.setLeftIndent(style, 20);
StyleConstants.setFirstLineIndent(style, -20);
StyleConstants.setForeground(style, Color.BLUE);
doc.setParagraphAttributes(0, doc.getLength(), style, true);
I'm need to modify letter-spacing (font tracking) in a JTextPane, and I can't get it to work.
When I'm using a JTextArea, I can just do:
Font font = new Font("Courier New", Font.PLAIN, 10);
HashMap <TextAttribute, Object> attrs = new HashMap<TextAttribute, Object>();
attrs.put(TextAttribute.TRACKING, -0.1);
font = font.deriveFont(attrs);
textArea.setFont(font);
but as I need to change line spacing, I need to use a JTextPane, and doing:
textPane.setFont(font)
as I did with the JTextArea doesn't work. another thing I tried was:
MutableAttributeSet set = new SimpleAttributeSet();
StyleConstants.setLineSpacing(set, -0.2);
StyleConstants.setFontFamily(set,"Courier New");
StyleConstants.setFontSize(set, 10);
set.addAttribute(TextAttribute.TRACKING, -0.1);
ta.setParagraphAttributes(set, true);
But the tracking attribute doesn't work.
What am I doing wrong?
Do you mean kerning? This one shows how to specify custom kerning and some more text effect
http://java-sl.com/gp_effects.html