Coders Packet

How Linebreaks in JLabels using Java Swing

By Kirti Sandip Phegade

In this program, we are going to discuss about How to Display Multiple Lines in JLabel. Swing provides platform-independent and lightweight components Java.

Labels in swing classes are instances of the JLabel class, which extends JComponent. JLabel is used for displaying unselectable short strings and images. 

Syntax:- JLabel label_name=new JLabel("Diplay on screen");

JLabel only shows a single line of text. This means if we want to write a passage of multiple lines. The output of the program shows paragraph in a single line only.

Program to display paragraph using JLabel

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Long_Paragraph extends JFrame implements ActionListener
{
JButton button;
Long_Paragraph()
{
setBounds(400,200,1020,600);
getContentPane().setBackground(Color.lightGray);

JLabel label = new JLabel("Long Paragraph");
label.setBounds(400,-185,1000,400);
label.setFont(new Font("Verdana", Font.PLAIN, 30));
add(label);

JLabel label1 = new JLabel("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
label1.setBounds(11,-100,1000,400);
label1.setFont(new Font("Verdana", Font.PLAIN, 20));
add(label1);

JLabel label2 = new JLabel("List Of Sentence");
label2.setBounds(400,-30,1000,400);
label2.setFont(new Font("Verdana", Font.PLAIN, 30));
add(label2);

JLabel label3 = new JLabel("1) Labels are the easiest control create and use. 2) It is and object of type Label. 3) Label also contains a string which is displayed when they are displayed in the window First ");
label3.setBounds(10,40,1000,400);
label3.setFont(new Font("Verdana", Font.PLAIN, 20));
add(label3);

button = new JButton("click to Solve Problem");
button.setBounds(350,390,350,60);
button.setFont(new Font("Verdana", Font.PLAIN, 20));
button.setFocusable(false);
button.setForeground(Color.BLACK);
button.addActionListener(this);


setDefaultCloseOperation(EXIT_ON_CLOSE);
add(button);
setLayout(null);
setVisible(true);

}

public static void main(String[] args)
{
new Long_Paragraph();
}

@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button)
{
setVisible(false);
new Solve() ;
}
}
}

Output

output


In this output paragraph displayed in a single line and go's out of frame. we have \n escape sequence means to insert a new line at this specific point in the text. but JLabel ignores the escape sequence. \n does not insert new line in Jlabel

Solution of this problem:- By default, Swing does not wrap text we can use HTML to enable line-wrapping. If we want to write a paragraph then we use paragraph tag and to give linebreak we use tag break tag 

Code to display paragraph using html tag:-

paragraph

Code to display sentences in separate line by using html tag:-

List Using JLabel

Output of program

output

Download project

Reviews Report

Submitted by Kirti Sandip Phegade (KirtiPhegade)

Download packets of source code on Coders Packet