How to set colour of text from a certain point forward - java

I'm building a small conversation agent, where the text looks like follows:
I would like to set the System's text to always be red. The text is all placed in a JTextPane.
How can I accomplish this? I have tried doing the following:
agentTextPane.setForeground(Color.red); after the system's text is added, and then switching back to black, however that changes all the text in the JTextPane.
This is how the system's text is added:
//'output' is a stringBuilder
output.append("\nSystem: ").append(tempOutput).append("\n");
agentTextPane.setText(output.toString());

As shown here, you can define an attribute set representing a desired style. For example,
StyledDocument doc = (StyledDocument) jtp.getDocument();
SimpleAttributeSet system = new SimpleAttributeSet();
StyleConstants.setFontFamily(system, "Serif");
StyleConstants.setForeground(system, Color.red);
doc.insertString(doc.getLength(), "...", system);
The styles can be progressive, as shown here.
See Text Component Features for more examples.

You may want to use HTML tags to format your Strings in terms of colour. The following reference may be useful. setting JTextPane to content type HTML and using string builders
output.append("<font color=\"red\">");
output.append("\nSystem: ").append(tempOutput).append("\n");
output.append("</font>");

Related

JavaFX - edit or parse FX-CSS file programmatically

I am using JavaFX 8u60. I want to give my users the chance to edit a CSS file for a pane in my program, without using an external editor.
For example, the user clicks on a Label, and a dialog to select the color is shown. After the user selects the color, the color is actually written in the CSS file, in the appropriate line...
Are there CSS parsers for JavaFX?
I can't show you any Java code because I'm not sure this can be done.
.table-view .column-header .label{
-fx-font: 18 GatwickSans;
-fx-text-fill: red; //<--- user shall be able to edit this line from my program
-fx-alignment: TOP_LEFT;
}
edit: to clarify, I want to be able to edit a FX-CSS file from Java.
You can use color picker, Try this example
Hbox layout =new HBox(10);
ColorPicker colorPicker = new ColorPicker();
colorPicker.setValue(Color.RED);//Red is the default shown at first
Label label =new Label("Your Text");
layout.getChildren().addAll(label,colorPicker);
//Then
colorPicker.setOnAction(event->{
label.setFill(colorPicker.getValue());
});
Also for css
colorPicker.setOnAction(event->{
label.setStyle("-fx-text-fill: "+colorPicker.getValue()+";");
});
I have used CSSParser:
http://sourceforge.net/projects/cssparser/
It's sufficiently generic and it works a bit like the DOM XML parser. It reads the CSS file and maps it in memory, allowing you to read the single selectors, properties and values, and edit them. Check the discussion on Sourceforge to have some examples, since it lacks documentation.

PDF Annotations using JAVA

I've been trying to add annotations to an existing PDF using the iText API and the older Lowagie version. However, I need alternatives to the API since it does not seem to be able to do what is asked in our requirement.
The requirement is to put an Annotation into an existing PDF with the following details:
Type: plain text
Postion: x=0mm && y=0mm
Font: Arial
Text Colour: White
Text Content: "some text"
Using iText, I can put in an annotation but I need to approximate in pixels where in my A4 size page I should put it. The closest approximation is using
PdfReader reader = new PdfReader(headerFilePath.concat(xmlFileName));
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(headerFilePath.concat(xmlFileNameNew)));
PdfAnnotation annotation = PdfAnnotation.createText(stamper.getWriter(), new Rectangle(0, 842, 5, 842), "some text", "some text", true, null);
annotation.setColor(Color.WHITE);
stamper.addAnnotation(annotation, 1);
reader.close();
stamper.close();
This snippet places it at the top left corner but I'm not sure if it's 0mm,0mm. Also it is black and I cannot specify the font.
Any help on the matter is greatly appreciated. Thanks!
You may try rich text annotions as described here:
annotation.put(PdfName.RC, new PdfString( "<font size=\"whatever\">" +
some text+
"</font>" ) );
myStamper.setGenerateAppearances( false );
However I doubt that this is what you need. If you add an appearance then you'll get an rectangular icon in the upper left corner. If you then hover with you mouse over it you are able to read the annotation in form of a pop up. And even if you get a white font color of the annotation text you can't hide the rectangular icon of the annotation itself...
You want to have a white font color which indicates that you want to "transport" some hidden (white color on white background) information. Maybe in this case you may use the following mechanism ( which is normally used for adding watermarks etc.):
String text;
int pageNumber
PdfContentByte overContent = stamper.getOverContent(pageNumber);
overContent.beginText();
overContent.setFontAndSize(yourFont, yourFontSize);
//overContent.setGrayFill(...);
overContent.showTextAligned(PdfContentByte.ALIGN_CENTER, yourText + " Center", 150, 760, 0);
overContent.endText();
Update: To set an annotation to invisble add to your code:
annotation.setColor(Color.WHITE);
//set visibility to 0 (invisble). All values between 0...1 are possible
//so 0.5 means 50% opacity
annotation.put(PdfName.CA, new PdfNumber(0));

The HTML content inside JEditorPane has blue background in Java

I have this Desktop email client program that I'm currently working on in Java and I want to show the incoming mail content inside a JEditorPane.
I've created an HTMLEditorKit and set it as an editor kit for the JEditorPane control like this:
editorPane.setEditorKit(htmlEditorKit);
And for some messages, it works fine but for some others, it shows its background as a blue-colored one. I tried to add a stylesheet rule to the kit, but it didn't work.
What could have been the problem?
Any comments will be welcome.
EDIT:
JEditorPane txtContent = new JEditorPane();
txtContent.setEditable(false);
txtContent.setBackground(Color.WHITE);
txtContent.setBounds(316, 110, 598, 545);
kit = new HTMLEditorKit();
txtContent.setEditorKit(kit);
HTMLDocument doc = (HTMLDocument)kit.createDefaultDocument();
txtContent.setDocument(doc);
txtContent.setText("HTML content here.....");
When I put "HTML content here....." part inside an html file with Notepad, it shows the right thing.
But when I put the exact same content inside the JEditorPane like the above, it shows different styling(blue background).

Not able to get SWT.UNDERLINE_LINK working in combination with StyledText and TextStyle

I am very new to using StyledText, TextStyle.
I am trying to give a hyperlink look for a text that is displayed. I am using TextStyle object to create a hyper link style text. For TextStyle constructor I am passing Font object which has the style SWT.UNDERLINE_LINK, expecting it to create a text with hyperlink style [as per javadoc].
I am not able get this style working. Other styles SWT.BOLD, SWT.ITALICS works but not SWT.UNDERLINE_LINK. Are there any special conditions to use SWT.UNDERLINE_LINK?
Check this Thread-
Can HTML-Style Links be added to SWT StyledText?
and
http://www.roseindia.net/tutorials/swt/create-link.shtml
You need to set
styleRange.underline = true;
when using the underlineStyle
styleRange.underlineStyle = SWT.UNDERLINE_LINK;

Java Swing JEditorPane: manipulating styled documents

I have model that is a queue of Strings associated with enum types.
I'm trying to display that model in a JEditorPane, with each element in the queue as a separate HTML paragraph that has attributes based based on the associated enum type.
However, my updating methods are not doing what I want. I tried writing the HTML strings directly to the document (e.g., I take the Strings, prepend <p style="color:red"> and append </p> and then insert them at the end of the document), but that gives me the html tags in the output (instead of as formatting) - which of course is inconsistent with the result of putting the tags on the string that I use construct the document with JEditorPane("text/html",String foo). I've also tried inserting with an AttributeSet, but apparently I'm doing that wrong as well.
Any suggestions?
I've never had much luck playing with HTML in a JEditorPane. I just use attributes in a JTextPane. Something like:
SimpleAttributSet keyWord = new SimpleAttributeSet();
StyleConstants.setForeground(keyWord, Color.RED);
StyleConstants.setBackground(keyWord, Color.YELLOW);
StyleConstants.setBold(keyWord, true);
try
{
doc.insertString(doc.getLength(), "\nSome more text", keyWord );
}
catch(Exception e) {}

Categories