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);
Related
I have come across various solutions to changing the margins when printing in Java but none seem to work. Here and Here.
What I have so far is,
TextPane textPane = new JTextPane();
StyledDocument doc = textPane.getStyledDocument();
// Define a keyword attribute
SimpleAttributeSet keyWord = new SimpleAttributeSet();
StyleConstants.setBold(keyWord, true);
Style style = doc.addStyle("StyleName", null);
StyleConstants.setIcon(style, new ImageIcon(qrcode));
doc.insertString(0, "Title Here\n", null );
doc.insertString(doc.getLength(), "Ignored", style);
textPane.print();
When using the inbuilt print method the margins are set to default as 25.4mm. I'd like to be able to edit these margins while still being able to have a print dialog.
What I "can" verify is, something like this will effect the page size and margins of the output
JTextPane textPane = new JTextPane();
StyledDocument doc = textPane.getStyledDocument();
// Define a keyword attribute
SimpleAttributeSet keyWord = new SimpleAttributeSet();
StyleConstants.setBold(keyWord, true);
Style style = doc.addStyle("StyleName", null);
//StyleConstants.setIcon(style, new ImageIcon(qrcode));
doc.insertString(0, "Title Here\n", null);
doc.insertString(doc.getLength(), "Ignored", style);
Paper paper = new Paper();
paper.setSize(fromCMToPPI(21.0), fromCMToPPI(29.7)); // A4
paper.setImageableArea(fromCMToPPI(5.0), fromCMToPPI(5.0),
fromCMToPPI(21.0) - fromCMToPPI(10.0), fromCMToPPI(29.7) - fromCMToPPI(10.0));
PageFormat pageFormat = new PageFormat();
pageFormat.setPaper(paper);
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintable(textPane.getPrintable(null, null), pageFormat);
PageFormat pf = pj.pageDialog(pageFormat);
if (pj.printDialog()) {
pj.print();
}
Normal output vs modified output (border added after to highlight change)
And the conversation methods...
protected static double fromCMToPPI(double cm) {
return toPPI(cm * 0.393700787);
}
protected static double toPPI(double inch) {
return inch * 72d;
}
What I can't verify is if those values appear in either page setup or printer dialogs, as MacOS has decided I don't need to see them :/
From previous experience on Windows, I seem to remember it working
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);
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'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