How to change font of JTextPane and center text.
I tried with textPane.setFont(font); but then i cannot center text and i tried using
SimpleAttributeSet attribs = new SimpleAttributeSet();
StyleConstants.setAlignment(attribs , StyleConstants.ALIGN_CENTER);
, but then i cannot change font...
I solved my problem with this:
StyleContext context = new StyleContext();
StyledDocument document = new DefaultStyledDocument(context);
Style style = context.getStyle(StyleContext.DEFAULT_STYLE);
StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);
JTextPane output = new JTextPane(document);
output.setFont(font);
Related
I want a table with no borders. I've tried to set the border property, individual border properties, set the border manually, set the cells border to no border, etc. None remove the border. What is the proper way to have an iText 7 table with no border?
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outputStream));
Document doc = new Document(pdfDoc);
PdfFont font = PdfFontFactory.createFont(FontConstants.HELVETICA);
Table table = new Table(new float[] { 1, 1 });
table.setProperty(Property.BORDER_BOTTOM, Border.NO_BORDER);
table.setProperty(Property.BORDER_LEFT, Border.NO_BORDER);
table.setProperty(Property.BORDER_RIGHT, Border.NO_BORDER);
table.setProperty(Property.BORDER_TOP, Border.NO_BORDER);
table.setProperty(Property.BORDER, Border.NO_BORDER);
table.setBorder(Border.NO_BORDER);
table.setWidthPercent(100);
// Header
File file = new ClassPathResource("logo.png").getFile();
Image logo = new Image(ImageDataFactory.create(file.getPath()));
Paragraph headerParagraph = new Paragraph();
Text headerTitle = new Text("Title of PDF")
.setFont(font)
.setFontSize(20)
.setFontColor(new DeviceRgb(0, 128, 128));
Text headerDescription = new Text("Description")
.setFont(font)
.setFontSize(11);
headerParagraph.add(headerTitle);
headerParagraph.add(NEW_LINE);
headerParagraph.add(headerDescription);
table.addCell(logo);
table.addCell(headerParagraph).setTextAlignment(TextAlignment.RIGHT);
None of these settings seem to work.Using iText 7.0.2
First of all, run the next snippet and see that iText7 can create tables without borders.
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName));
Document doc = new Document(pdfDoc);
Table table = new Table(new float[] {50, 50 });
Paragraph headerParagraph = new Paragraph();
Text headerTitle = new Text("Title of PDF")
.setFontSize(20)
.setFontColor(new DeviceRgb(0, 128, 128));
Text headerDescription = new Text("Description")
.setFontSize(11);
headerParagraph.add(headerTitle);
headerParagraph.add(headerDescription);
table.addCell(new Cell().add("logo").setBorder(Border.NO_BORDER));
table.addCell(new Cell().add(headerParagraph).setBorder(Border.NO_BORDER).setTextAlignment(TextAlignment.RIGHT));
doc.add(table);
This is the line responsible for such "magic" :
table.addCell(new Cell().add("logo").setBorder(Border.NO_BORDER));
However there is no magic at all.
By default, cells have borders in iText7 (0.5px solid black). So if you want to add a cell without border you should specify it by setting NO_BORDER as a cell border.
On the other hand, tables don't have borders by default (I mean bounding borders). So there is no need in these lines:
table.setProperty(Property.BORDER_BOTTOM, Border.NO_BORDER);
table.setProperty(Property.BORDER_LEFT, Border.NO_BORDER);
table.setProperty(Property.BORDER_RIGHT, Border.NO_BORDER);
table.setProperty(Property.BORDER_TOP, Border.NO_BORDER);
table.setProperty(Property.BORDER, Border.NO_BORDER);
table.setBorder(Border.NO_BORDER);
Also you should understand that table.setBorder(border) stands for table.setProperty(Property.BORDER, border). The same for table.setBorderLeft(border), etc.
Font displayFont = new Font(Font.SANS_SERIF, Font.BOLD, 18);
WindowManager.getInstance().getConsoleWindow().getTextArea().setFont(displayFont);
WindowManager.getInstance().getConsoleWindow().getTextArea().setForeground(Color.BLUE);
The above is my code snippet responsible for changing the properties of the text in my jtextpane when I click a button. The text correctly updates to become larger and bold however it doesn't change the colour and I have no idea why. Thanks in advance.
Instead of setting directly on the foreground property, you may want to do this:
JTextPane textPane = new JTextPane();
StyledDocument doc = textPane.getStyledDocument();
Style style = textPane.addStyle("Blue", null);
StyleConstants.setForeground(style, Color.blue);
I want to mark parts of a jeditorpane content with a color, without using the html type content type. is there a way to accomplish this, maybe playing around with the document?
without using the html type content type
This means you have a plan text file and you should be using a JTextPane. Then you can use attributes to color the text.
JTextPane textPane = new JTextPane();
textPane.setText("Some text for the text pane.");
StyledDocument doc = textPane.getStyledDocument();
SimpleAttributeSet keyWord = new SimpleAttributeSet();
StyleConstants.setForeground(keyWord, Color.RED);
StyleConstants.setBackground(keyWord, Color.YELLOW);
StyleConstants.setBold(keyWord, true);
// Color existing text
doc.setCharacterAttributes(0, 5, keyWord, false);
// Add some text
try
{
doc.insertString(doc.getLength(), "\nEnd of text", keyWord );
}
catch(Exception e) { System.out.println(e); }
When you setContentType("text/html") it is applied only for the text that is set via JTextPane.setText(). All other text, that is put to the JTextPane via styles is "immune" to content type.
Here is what I mean:
private final String[] messages = {"first msg", "second msg <img src=\"file:src/test/2.png\"/> yeah", "<img src=\"file:src/test/2.png\"/> third msg"};
public TestGUI() throws BadLocationException {
JTextPane textPane = new JTextPane();
textPane.setEditable(false);
textPane.setContentType("text/html");
//Read all the messages
StringBuilder text = new StringBuilder();
for (String msg : messages) {
textext.append(msg).append("<br/>");
}
textPane.setText(text.toString());
//Add new message
StyledDocument styleDoc = textPane.getStyledDocument();
styleDoc.insertString(styleDoc.getLength(), messages[1], null);
JScrollPane scrollPane = new JScrollPane(textPane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
//add scrollPane to the main window and launch
//...
}
In general, I have a chat that is represented by JTextPane. I receive messages from server, process them - set text color for specific cases, change smile markers to images path etc. everything is made within the bounds of HTML. But as it can be clearly seen from example above, only the setText is the subject of setContentType("text/html") and the second part, where new message added is represented by "text/plain" (if I'm not mistaken).
Is it possible to apply "text/html" content type to all data that is inserted to JTextPane? Without it, it is almost impossible to process messages without implemention of very complex algorithm.
I don't think you should be using the insertString() method to add text. I think you should be using something like:
JTextPane textPane = new JTextPane();
textPane.setContentType( "text/html" );
textPane.setEditable(false);
HTMLDocument doc = (HTMLDocument)textPane.getDocument();
HTMLEditorKit editorKit = (HTMLEditorKit)textPane.getEditorKit();
String text = "hyperlink";
editorKit.insertHTML(doc, doc.getLength(), text, 0, 0, null);
REEDIT
Sorry, I misunderstood the problem: inserting a string as HTML.
For that one needs to resort to the HTMLEditorKit capabilities:
StyledDocument styleDoc = textPane.getStyledDocument();
HTMLDocument doc = (HTMLDocument)styleDoc;
Element last = doc.getParagraphElement(doc.getLength());
try {
doc.insertBeforeEnd(last, messages[1] + "<br>");
} catch (BadLocationException ex) {
} catch (IOException ex) {
}
Here is a much simpler way to do that.
JTextPane pane = new JTextPane();
pane.setContentType("text/html");
pane.setText("<html><h1>My First Heading</h1><p>My first paragraph.</p></body></html>");
I can make new lines using this code
StringBuilder sb = new StringBuilder();
sb.append("<span style=\"color:black\">--------------</span> <br>");
sb.append("<span style=\"color:red\">Error." + e.toString() + "</span> <br>");
sshoutput.setContentType("text/html");
sshoutput.setText(sb.toString());
but when I do this one more time with another text it only shows second text not after this
Error." + e.toString()
Sorry my English is not great. I hope you understand.
I'm working with JTextPane right now and what I do is:
JTextPane pane = new JTextPane();
StyledDocument doc = pane.getStyledDocument();
And so I can insert strings in any place using:
doc.insertString(STRING POSITION, STRING, null);
I have no exceptions with this method. There is also easy way to style letters using:
SimpleAttributeSet set = new SimpleAttributeSet();
//Here you modify set. Set is collection of
//various style instructions
//(letters color, bolded, italic, background color etc.)
//You modify set using StyleConstants class.
doc.setCharacterAttributes(START, LENGTH, set, true);
EDIT: An example, which creates text pane and writes in it styled 'Hello World':
JTextPane pane = new JTextPane();
StyledDocument doc = pane.getStyledDocument();
doc.insertString(0, "Hello", null);
SimpleAttributeSet set = new SimpleAttributeSet();
StyleConstans.setForeground(set, Color.RED);
doc.setCharacterAttributes(0, 5, set, true);
doc.insertString(5, "World!", null);
SimpleAttributeSet set = new SimpleAttributeSet();
StyleConstans.setForeground(set, Color.BLUE);
doc.setCharacterAttributes(5, 6, set, true);
Add it to a JPanel with GridLayout(1, 1) and you should see text pane with red string "Hello" and blue string "World" in it.