I'm looking for an easy way to make some letters in a label bold.
I have a string like this:
String r = "y = "+output0+" "+output1+"sin(x) "+output2+"cos(x)";
and a label:
Label s = new Label(r);
and I need to make the "y" and "sin(x), cos(x)" bold. I tried using HTML, but it didn't work (maybe i was using it wrong). If I set font for that label, then the whole label is bold (including those outputs) which is not what I need.
You can use html style in JLabels and in some other Java components. If you start your text with <html> and end it with </html>, the html code in your JLabel will be rendered.
This should resolve your issue:
JLabel myLabel = new JLabel();
// The following line is required to make this JLabel's text not bold as JLabel's text is bold be default.
myLabel.setFont(myLabel.getFont().deriveFont(Font.PLAIN));
myLabel.setText("<html><strong>y</strong> = " + output0 + " " + output1 + "<strong>sin(x)</strong> " + output2 + "<strong>cos(x)</strong></html>");
Try using HTML (you have to start the string with <html> and end it with </html>):
new Jlabel("<html>Normal text. <strong>This is bold.</strong></html>")
Explanation! <html> tells Java to render HTML (Hyper Text Markup Language), and <strong> tells HTML that the text inside is of strong importance, normally represented in bold.
Related
When using JLabel, is there a way to use HTML and method call together?
For example:
JLabel speedLabel = new JLabel("<html><b>Speed: </b></html>" + plane.getSpeed());
This doesn't work.
It only shows Speed: and ignores the rest.
Try to change your code to this:
JLabel speedLabel = new JLabel("<html><b>Speed: " + plane.getSpeed() + " </b></html>");
The text, or in your case the airplane speed, that is probably a double, must be in between the <html> and </html> tags, to be recognized.
it is discarding the values after html close tag if you something like this it will work JLabel speedLabel = new JLabel("<html><b>Speed: " + plane.getSpeed() + "</b></html>");
I tried to add an HTML styled text to my dynamic jasper report
JRDesignBand band = new JRDesignBand();
band.setHeight(20); // Set band height
JasperDesign jasperDesign = new JasperDesign();
JRDesignTextField textField = new JRDesignTextField();
textField.setX(0); // x position of text field.
textField.setY(0); // y position of text field.
textField.setWidth(860); // set width of text field.
textField.setHeight(20); // set height of text field.
JRDesignExpression jrExpression = new JRDesignExpression(); // new instanse of expression. We need create new instance always when need to set expression.
jrExpression.setText("<p>text space</p>"); // Added String before field in expression.
textField.setExpression(jrExpression);
textField.setMarkup("html");
band.addElement(textField);
((JRDesignSection) jasperDesign.getDetailSection()).addBand(band);
JasperDesignViewer.viewReportDesign(jasperDesign);
But the text still has HTML tags.
As far as I know, the html tags allowed are very limited and for basic text formatting.
<b>bold</b>
<i>italic</i>
<u>underlined</u>
And thats it.
There is a discussion here about "html" and "styled" markup having issues when exporting to PDF http://community.jaspersoft.com/questions/521757/html-markup-pdf
I'm testing the functions of text editor and I would like to be able to select text (either a word or a string between 'p' tags) and then edit it by clicking a button (B for bold or I for Italic). Much like the editor used here at Stackoverflow. This will add an html B tag so that it shows as bold.
I know the following Xpath leads to a string of text but I can't find how to select this text or how to select a word in this text.
This is part of the page source (text editor is in an iframe)
<html>
<head>
<body>
<p>
This is a line of text to test Bold
</p>
<p>
This is a line of text to test Italic
</p>
</body>
Where this
driver.findElement(By.xpath("/html/body/p[1]")
leads to a text:
This is a line of text to test Bold
I would like to select the word 'Bold' (or the entire sentence if this is easier) and then click a button above the text to make it Bold
Try this
Actions a = new Actions(driver);
a.doubleClick(driver.findElement(By.xpath("/html/body/p[1]")).build().perform();
What you could do is to use an <html> Tag within JLabels for instance.
What I have in mind looks like the following:
boldButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String formattedText = "<html><b>" + text + "</b></html>";
label.setText(formattedText);
}
}
italicButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String formattedText = "<html><i>" + text + "</i></html>";
label.setText(formattedText);
}
}
Unforunately, I do not know how your text editor looks like, but this is the way I would approach the problem.
Anyways, in a larger scale you can still use HTML tags to format the text (e.g., in a TextArea). In order to get the highlighted/selected items you might want to have a look at this thread how to get highlighted text in jtextarea.
So you could try something like this:
// retrieve selected text from text area
String selectedText = textArea.getSelectedText();
// get whole text
StringBuffer text = new StringBuffer(textArea.getText());
// remove the selected text
text.remove(textArea.getSelectionStart(), textArea.getSelectionEnd());
// make the selected text, e.g., bold.
selectedText = "<b>" + selectedText + "</b>";
// add the bold text to the whole text
text.insert(textArea.getSelectionStart(), selectedText);
//eventually apply outer <html> tags
String result = "<html>" + text.toString() + "</html>";
// set new text (with bold element) to the text area
textArea.setText(result);
Please note that I have not tried this, so you might tweak some things in order to make it work.
As it says on the tin.. I cannot get a label's text to wrap. Essentially I am building like a comments panel, user enters a comment and its displayed with timestamp etc.
I want the labels to both display with ContentMode.PREFORMATTED but also wrap.
The layouts of which contain the label are fixed width (100%) as is the label obviously by default, from what I have readin the book of vaadin what I am doing should work?
here is my code snippet:
VerticalLayout container = new VerticalLayout();
rootLayout.addComponent(container);
VerticalLayout comment = new VerticalLayout();
container.addComponent(comment);
Label createdByLbl = new Label(entity.getManagedMetadata().getAttrValue("createdBy") + " said:");
createdByLbl.setId("conversation.comment.username." + repositoryUID);
createdByLbl.setStyleName("conversation-comment-username");
Label createdDateLbl = new Label(entity.getManagedMetadata().getAttrValue("createdDate"));
createdDateLbl.setId("conversation.comment.createddate." + repositoryUID);
createdDateLbl.setSizeUndefined();
String text = entity.getDataNode().getAttrValue("text");
Label textLbl = new Label(text, ContentMode.PREFORMATTED);
textLbl.setId("conversation.comment.text." + repositoryUID);
comment.addComponent(createdByLbl);
comment.addComponent(textLbl);
comment.addComponent(createdDateLbl);
comment.setExpandRatio(createdByLbl, 0.2f);
comment.setExpandRatio(textLbl, 0.7f);
comment.setExpandRatio(createdDateLbl, 0.1f);
Note the container is also wrapped by CssLayout (rootLayout), which is full sized.
I want the textLbl as seen above to display as formatted should the user enter text on separate lines themselves and wrap if they have entered a long paragraph comment.
Here is a picturing showing my dilemma.
Any help would be appreciated.
Thanks,
Try with css.
For example:
textLbl.addStyleName("wrapLine");
Css:
.wrapLine{
word-wrap: break-word; /* IE */
white-space: -moz-pre-wrap; /* Firefox */
}
I have a TreeEditor and some text in its first column. I need to append some text to it using a label or string. In addition, I want to change the color of the appended string to green. I tried to do it with this code, but it isn't working:
Label label = new Label(this.tree ,SWT.RIGHT);
label.setFont(new Font(getSite().getShell().getDisplay(), ("Hello"), 18,
SWT.BOLD));
label.setText("hello:));
label.pack();
treeItem.setText(1, hi + " " + label.getText());
How could I accomplish this?
you might want to look at org.eclipse.jface.viewers.DecoratingStyledCellLabelProvider