Vertical Align text in java not showing up right - java

I am using the answer to this question in order to word-wrap my JLabel's text and the code I am using to do that is this:
label.setText("<html style=\"vertical-align:middle;\">"+label.getText()+"</html>");
But the label displays the text wrong. For example if the label's text was "the big bad wolf" what I see is this:
<html style="vertical-align:middle;">the big bad wolf</html>
Why is the label not accepting my style?
UPDATE: Here is an example of what I am trying to do:

Try
private String warpWithHtmlTags(String...strings) {
StringBuilder sb = new StringBuilder("<html>");
sb.append("<div style = \"text-align: center;\">");
for(String s : strings) {
sb.append("<p>")
.append(s)
.append("</p>");
}
sb.append("</div></html>");
return sb.toString();
}
Test with :
String text = warpWithHtmlTags("line one", "longer line two", "even longer line three");
JLabel label = new JLabel(text);

Related

Label doesn't scale down correctly to 0.07f, but if I use font.setUseIntegerPositions(false), it shows both label and font in libGDX

I try to make a label to display "You Win" and add it to winStage, but I cannot manage to display the label correctly on winStage. Some words don't show on the screen, and if I downscale further than 0.044f, the String disappears. when I add font.setUseIntegerPositions(false) to my code, the font shows correct, but the wrong label also shows. Is there a way to get this work? (hide the wrong label or get the label shows correctly), code uses Kotlin, but very similar to java
YUW N is the wrong label I want to remove, the correct "You Win" only shows when I add font.setUseIntegerPositions(false)
here's the relevant code part
private val winViewport = StretchViewport(mainStage.width, mainStage.height)
val winStage = Stage(winViewport, winBatch)
var font = BitmapFont()
var labelStyle = Label.LabelStyle()
labelStyle.font = font
labelStyle.fontColor = Color.RED
var label2 = Label("YOU WIN", labelStyle)
label2.setBounds(0f, winStage.height*2/3, winStage.width*1, winStage.height/6)
label2.setFontScale(0.07F) // 0.07f
font.setUseIntegerPositions(false)
label2.setWrap(false)
winStage.addActor(label2)
winStage.act(delta)
winStage.draw()

Select single word or string (doubleclick) in order to edit

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.

Newline in WindowMessage

i'm new in java, i try to add new lines in a message string:
String message = "Test User1,\n Test User2,\n Test User1";
WindowMessage win2 = new WindowMessage("The following Names are duplicate : "+messages);
win2.setModal(true);
app.addWindow(win2);
i tried to add \r\n, \n, %r%n, %n, but none of them working
in my WindowMessage(extends WindowPane) class, the Message is set as a Label:
lblMessage.setText(Message);
Any idea why?
Try using JOptionPane.showMessageDialog
It's not clear which API you're using, but try using a JLabel with html:
JLabel l = new JLabel("<html>line1<br>line2</html>");
If you are using a library that does not support multilines within the same label (check the docs!), then you should stack two labels on top of each other:
Label line1 = ...
Label line2 = ...
If you do not have control over how many labels you can add, then what you are trying to do is not possible with your library.

How to make some letters in string BOLD

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.

Showing required indicator on GWT's Label

In my GWT app I want to indicate the fields as required. Please advise how do I do that?
If i just add a * in the label text then it is not very well noticeable as it is of the same color as the label text and also aligned to the label rather like a superscript character.
I don't know if applying style sheet can make it work? Any other web 2.0 ideas which do not involve a lot of work?
Currently, it's displayed as
Label fullName = new Label("Full Name*");
The simplest way is
String strReqLabel = "<span style=\"color: #DC143C;\">*</span>"
Label reqLabel = new Label(strReqLabel + " " + "First Name");
Just separate the * from the label, to another label:
Label label = new Label("Full name");
TextBox tb = new TextBox();
Label reqLabel = new Label("*");
reqLabel.setStyleName("red_label");
then on the war/app.css crete a new item
.red_label {
color: red;
font-size: 8pt;
overflow: hidden;
}
Smething like this should work.
Alternatively,
Suffix the label with * mark - so that who are really cautious of filling the form will observe it and do the needful. For those you are not cautious, after submitting the form,
you can think of making the outline in red color. This way, you need not make the form too flashy at initial look.
So I did it as:
public HorizontalPanel createLabel(String name){
HorizontalPanel hPanel = new HorizontalPanel();
hPanel.add(new Label(name));
Label star = new Label("*");
star.setStyleName("req-label");
hPanel.add(star);
return hPanel;
}
and css as:
.req-label{
color: #FF0000;
font-weight: bold;
}

Categories