Newline in Jlabel

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

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.

Java: Linebreaks in JLabels?

Use HTML in setText, e.g.

myLabel.setText("<html><body>with<br>linebreak</body></html>");

New line for JLabel?

Note that this has nothing to do with JLabel having a new line and all to do with smart use of layout managers to place components. The secret here is to simply use more JPanels each with its own layout and thereby combine layouts. For e.g.,

// Center the text in the JLabel using SwingConstants
private JLabel intLabel = new JLabel("Integer Calculation", SwingConstants.CENTER);

// ...

public HW3TabbedPane() {
// GridLayout to hold JPanels in one column and in several rows
intPanel1.setLayout(new GridLayout(0, 1));
intPanel1.add(intLabel); // add the title JLabel to the top

// next row's JPanel
JPanel nextJPanel = new JPanel(); // leave it FlowLayout
nextJPanel.add(n1IntLabel);
nextJPanel.add(n1IntField);
nextJPanel.add(n2IntLabel);
nextJPanel.add(n2IntField);
nextJPanel.add(intResultLabel);
nextJPanel.add(intResultField);

// add to the intPanel1
intPanel1.add(nextJPanel);

How to apply line breaks in JLabels?

You can use HTML code inside your JLabel in this way

JLabel label = new JLabel("<html>Welcome to<br>Stackoverflow</html>");

Java JLabel, break text to next line?

JLabels can't do that by default. But JLabels have some support for html, so a JLabel with the text <html>First Line<br />Second Line</html> would show up on two lines.

If you want a component that can split the lines by itself, take a look at JTextArea.



Related Topics



Leave a reply



Submit