Newline Character in Jlabel.Settext()

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 using JLabel.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



Leave a reply



Submit