Newline in JLabel
Surround the string with <html></html>
and break the lines with <br/>
.
JLabel l = new JLabel("<html>Hello World!<br/>blahblahblah</html>", SwingConstants.CENTER);
How to add a newline to JLabel without using HTML
I am Embedding the font using the method -
createFont()
) and usingJLabel.setFont()
for applying the font.
Instead try setting it in the HTML, as shown here.
How can I apply new line in jLabel?
try{
//display doctor's name by selected classification
String url = "jdbc:mysql://localhost/sched";
Connection conn = DriverManager.getConnection(url,"root","");
Statement stmt = conn.createStatement();
String classification = comboClass.getSelectedItem().toString();
String sqlSelect= "select * from doctorsched where class = '"+classification+"'";
ResultSet rs = stmt.executeQuery(sqlSelect);
String finalText ="";
while(rs.next()){
String docsName= rs.getString("docName");
String room = rs.getString("room");
String days = rs.getString("day");
String from = rs.getString("timefrom");
String to = rs.getString("timeto");
finalText += (docsName+" (room "+room+", "+days+", "+from+"-"+to+")\n") ;
finalText += "<br>";
}
jLabel10.setText("<html>" + finalText + "</html>");
} catch (Exception ex) {
Logger.getLogger(home.class.getName()).log(Level.SEVERE, null, ex);
}
Java: Linebreaks in JLabels?
Use HTML in setText, e.g.
myLabel.setText("<html><body>with<br>linebreak</body></html>");
jLabels automatically goes newline
Instead of messing around with HTML, why not just use a bold font?
Font f = myLabel.getFont();
myLabel.setFont( new Font( f.getName(), Font.BOLD, f.getSize() ) );
JLabel HTML New Line Issue
From the image you pasted, it looks like it is a text INPUT control (under Show Info
button), like JTextField
and not a JLabel
.
You can use HTML
content with JLabel
constructor as well as with its setText
method too. It works fine.
JLabel lbl = new JLabel("<html>Type: Circle<br>Some info<br>More info</html>")
JLabel lbl2 = new JLabel();
lbl2.setText("<html>Type: Circle<br>Some info<br>More info</html>")
But if you want to have an INPUT control (as in your image), you can not use HTML
with JTextField
. You have to use JTextPane
for this.
JTextPane txt = new JTextPane();
txt.setContentType("text/html");
txt.setText("<html>Type: Circle<br>Some info<br>More info</html>");
Related Topics
How to Use a Regex to Search Backwards Effectively
Multiple Queries Executed in Java in Single Statement
Find Maximum Score or the Maximum Average Score of Candidate Scores Given in Two Dim Array
How to Specify the Required Java Version in a Gradle Build
Cannot Find Element Using Selenium Webdriver
Handling the Null Value from a Resultset
How to Properly Re-Run Spring Boot Application from Eclipse
Where Does Gradle Save Dependencies' Jars
Passing Data into HTML from Java Via Spring
How to Return Different Results When Calling the Same Mocked Method
How to Send Parallel Get Requests and Wait for Result Responses
How to Process Lines of a File in Parallel
How to Check Heap Usage of a Running Jvm from the Command Line
Removing a Column in Java from a 2D Array
Broadcast Receiver Is Not Working When Application Is Removed from Background