Newline in WindowMessage - java

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.

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()

XPATH - getAttribute() and writer.append() to CSV file (Selenium/Selenide)

I'm trying to get "title" attribute value and save it in csv file from element below:
<img src="images/i.png" title="Uwagi: łacina, nieczytelne
Data urodzenia: 25.02.1808 r.">
Whole html here.
I've got this attribute value using xpath below (it works):
SelenideElement uwagi = $(By.xpath("//div[#id='table_b_wrapper']//table[#id='table_b']//tbody//tr[1]//img[contains(#title,'Uwagi')]"));
//tr[1] is just a one example from this table. xpath is ok
Then I've tried to put it into my csv file with:
writer.append(uwagi+";"); //using ; as separator
Problem is that this value "Uwagi: łacina, nieczytelne
Data urodzenia: 25.02.1808 r."
It's divided into 2 parts and they are saved as separate cells, like here
I need all this value in one cell (i.e. J1731 and A1732 values should be as 1 cell).
What's strange when I did System.out.println(uwagi.getAttribute("title"));
only 2nd part of attribute value (Data urodzenia: 25.02.1808 r.) was displayed in console.
How can I save this title attribute value as one cell in csv?
Regards
Tomes
Remove new line character from the title, code below replace \n (new line character) with one space as needed per your shared html.
Also in Selenide you can use $x for xpath selectors:
SelenideElement uwagi = $x("//table[#id='table_b']//tr[#role='row'][1]//img[contains(#title,'Uwagi')]");
//using css selector
uwagi = $("#table_b tr[role='row'] img[title^='Uwagi']");
//or even shorter
uwagi = $("#table_b img[title^='Uwagi']");
String uwagiTitle = uwagi.text().replace("\n", " ");
writer.append(uwagiTitle+";");
I've found solution. I've changed:
FileWriter writer = new FileWriter(pathString, Charset.forName("Cp1250"));
to
CsvWriter writer = new CsvWriter(pathString, ';', Charset.forName("Cp1250"));
using also:
<dependency>
<groupId>net.sourceforge.javacsv</groupId>
<artifactId>javacsv</artifactId>
<version>2.0</version>
</dependency>
Based on the info from: link
Then I've changed writer.apend to writer.write.
Other is the same:
...
SelenideElement xxx = $x("//img[contains(#title,'Uwagi')]");
String str = xxx.getAttribute("title");
writer.write(str);
...
Result: picture
Regards
Tomes

Vertical Align text in java not showing up right

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);

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.

Wicket change label/textfield value

I am trying to learn Wicket. One of the problems I encounter, is changing the values of components like a label.
This is how I declare the label:
Label message = new Label("message", new Model<String>(""));
message .setOutputMarkupId(true);
add(message );
The only solution I can find:
Label newMessage= new Label(message.getId(), "MESSAGE");
newMessage.setOutputMarkupId(true);
message.replaceWith(newMessage);
target.add(newMessage);
Is there a better/easier way to edit the value of a Wicket label and display this new value to the user?
Thanks!
I think you did not understand what Models are. Your example could be rewritten as follows
Model<String> strMdl = Model.of("My old message");
Label msg = new Label("label", strMdl);
msg.setOutputMarkupId(true);
add(msg);
In your ajax event
strMdl.setObject("My new message");
target.add(msg);

Categories