How to append html text to JLabel in Java Swing - java

I'm trying to append text with HTML tags to text that is already in a JLabel and also has HTML tags
public class BattleConsoleUI {
private JLabel battleInfo = new JLabel("<html> Hello World <br></html>");
battleInfo.setText(battleInfo.getText() +
"<html> HERO NAME : " +
"<br> HERO CLASS : " +
"<br> HERO LEVEL : " +
"<br> XP : " +
"<br> ATTACK POINTS : " +
"<br> DEFENCE POINTS : " +
"<br> HIT POINTS : " +
"</html>");
}
I'm expecting it to display Hello World plus the appended text but the rest of the text is not displayed because of the first closing HTML tag

Quick solution is to avoid writing </html> at the end of the text. Swing needs only the opening tag <html> in order to show HTML text. Something like:
label.setText("<html>first text");
label.setText(label.getText() + " this is second"); //Still an HTML text
If you insist of closing the HTML tag and using </html> at the end, you will have to replace it before appending the new text:
label.setText(label.getText().replaceAll("</html>","") + "i append a text</html>");
Of course instead of replaceAll you could use substring and other things, but this is what i would use.

You are making the variable itself to be first set and then again inside that get text. You can do simple thing is that make two different string variable and set them to jlabel. As per here you can :
String htmlstr1 = "html hello world tag";
String htmlstr2 = "<html> HERO NAME : "
+
"<br> HERO CLASS : " +
"<br> HERO LEVEL : " +
"<br> XP : " +
"<br> ATTACK POINTS : " +
"<br> DEFENCE POINTS : " +
"<br> HIT POINTS : " + "</html>");
Jlabel.setText(htmlstr1+htmlstr2):

Related

how do i edit HTML inside string JAVA CODE easily

i developed android app and i used HTML code to Print the result in formatted form.
the code will be like this :
String adress = editText1.getText().toString();
String phone = editText2.getText().toString();
String licenseNo = editText3.getText().toString();
"\n" +
" <div class=\"down\">\n" +
" <div class=\"EASY\">\n" +
" <img src=\"" + image + "\" alt=\"QR\" >\n" +
" </div>\n" +
" <table class=\" info_table\">\n" +
" <tbody>\n" +
" <tr>\n" +
" <td class=\"info\">" + phone + "</td>\n" +
" <td class=\"info\"> " + licenseNo + "</td>\n" +
" </tr>\n" +
" <tr>\n" +
" <td class=\"info_A\" colspan=\"2\"> " + adress + "</td>\n" +
" </tr>\n" +
" </tbody>\n" +
" </table>\n" +
" </div>";
as you see it should contain \n and + because its inside a string object contained variable values from user input.
i faced some difficulty when i need to Edit this Piece of code cose i will test it inside the application.
my question is what is the best way to get this code to edit and preview it outside android studio and reinsert it again.
i used find and replace but it make some problems.
hope i explained enough
You can create a XML file in the resources folder, read it and then use format in order to change the values there.

API endpoint how to return a string with new line

In my custom GET endpoint, I would like to return a String but, to "beautify" it, I would like to insert new line for each different information. I've tried with \n\r this way
return "Name of process definition: "+ obj.getString("processDefinitionName") + "\r\n" + "Start time of instance: " + obj.getString("startTime")
+ "||" + "End time of the instance: " + obj.getString("endTime") + "||" + "Total duration time(ms): " + obj.getInt("durationInMillis");
but it only prints a white space and the text is written on the same line. Why is it not working?
You have to use HTML tags to display new line on browser. You can use <br/> tag to add new line. Browser parses your response in HTML form.

How can I add a newline character to a String in Java?

In a Java application, I am creating a String like below (by concatenation):
String notaCorrente = dataOdierna + " - " + testoNotaCorrente;
My problem is that I want to add also something like an HTML newline character at the end of this String (that will be shown into an HTML page).
How can I implement it?
The newline character in Java is "\n" which will look like this:
String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "\n";
However, this will not display as you expect on your HTML page. You can try adding an html break tag, or add the
(Line Feed) and 
 (Carriage Return) HTML entities:
String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "<br>";
or
String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "
&#10";
For a newline that will result in a line break in HTML, use
String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "<br>";
For a newline that will result in a line break in your text editor, use
String notaCorrente = dataOdierna + " - " + testoNotaCorrente + System.lineSeparator();
And for both, use
String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "<br>" + System.lineSeparator();
Why not \n?
\n is specific to certain operating systems, while others use \r\n. System.lineSeparator() will get you the one that is relevant to the system where you are executing your application. See the documentation for more info on this function, and Wikipedia for more info on newlines in general.
Simply, need to add <br/> (break line tag of HTML).
String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "<br/>";
so, while you are going to display this content, <br/> tag will rendered on HTML page in form of new line.

How do i append a new text at JLabel?

I read, some StackOverflow questions that I need to use HTML stuffs.
But what would be the easiest it without any of HTML stuff.
Here's the code
label.setText(label.getText() + (String)boxTimes.getSelectedItem() + input);
This code will produce this
What I want is:
You must know a bit of basic String format:
\n line break
\t tab
So your code will be like:
String myLabel =
// 4
label.getText() + "\n\n" +
// 7:00
(String)boxTimes.getSelectedItem() + "\t" +
// - Going out....
"- " + input;
label.setText(myLabel);
But as long as JLabel does not accept \n as Abishek Manoharan pointed, you must use <br>.
String myLabel =
"<html>" +
label.getText() +
"<br/><br/>" +
(String)boxTimes.getSelectedItem() + " - " + input +
"</html>;
label.setText(myLabel);
I was faced with the same problem too and couldn't find a viable solution.
So I went ahead and used a JTextArea instead of JLabel.
JTextArea label = new JTextArea();
label.setEditable(false);
label.setBackground(null);
It gives the same look and feel of a JLabel
You can use '\n' and '\t' as you like,
and what more, the text is selectable which is not possible in JLabel.

How Html works to create a jLable in java

Since it is an array it repeats till the end of the loop. But Room 1 should not be repeated . it should be place at the top
first[ind] = new JLabel("<html>"
+ "<body>"
+ "<div id=r12style=border: 3px solid orange; margin-bottom: 5px;>"
+ " <h2>"
+ " Room 1"
+ " </h2>"
+ "<img src=" + icon + " width=\"95\" height=\"105\"></img>"
+ "</div>"
+ "</body>"
+ "</html>");
Try this (I haven't tested it):
IMM[ind] = new JLabel("<html><style >#aa {margin-left:25px;}</style>"
+ "<div id=\"aa\"></font><font color=\"rgb(0,0,0)\"size=\"5\">"
+ bed_no + "<font color=\"rgb(255, 204, 204, 150)\"size=\"1\">.</div></font></html>"
, "<html><img src=" + icon + "></html>", JLabel.LEFT_ALIGNMENT);
If you want the text under the icon:
IMM[ind].setHorizontalTextPosition(JLabel.CENTER);
IMM[ind].setVerticalTextPosition(JLabel.BOTTOM);
Edit - It's probably a good idea to break up the JLabel text by using a String variable. Because it looks almost unreadable.

Categories