1

I have a problem with line breaks, I have made a code like this

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

public class Welcome extends JFrame {
    JMenu menuAdd, menuShow, menuEdit, menuHelp;
    JMenuItem addCar, addRent, addUser, editCar, editCostumer, editUser, showCar, showRent, helpAbout;
    JLabel lblWelcome;
    JMenuBar mb;

public Welcome() {
    setTitle("Car Tooner");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(400,400);
    setLocationRelativeTo(null);
    setResizable(false);

    menuAdd = new JMenu("Add");
    menuShow = new JMenu("Show");
    menuEdit = new JMenu("Edit");
    menuHelp = new JMenu("Help");

    addCar = new JMenuItem("Car");
    addRent = new JMenuItem("Rent");
    addUser = new JMenuItem("User");
    editCar = new JMenuItem("Car");
    editCostumer = new JMenuItem("Costumer");
    editUser = new JMenuItem("User");
    showCar = new JMenuItem("Car");
    showRent = new JMenuItem("Rent");
    helpAbout = new JMenuItem("About");

    mb = new JMenuBar();

    lblWelcome = new JLabel("Welcome, Admin \nto Car Tooner", SwingConstants.CENTER);

    setJMenuBar(mb);
    mb.add(menuAdd);
    mb.add(menuEdit);
    mb.add(menuShow);
    mb.add(menuHelp);

    menuAdd.add(addCar);
    menuAdd.add(addRent);
    menuAdd.add(addUser);
    menuEdit.add(editCar);
    menuEdit.add(editCostumer);
    menuEdit.add(editUser);
    menuShow.add(showCar);
    menuShow.add(showRent);
    menuHelp.add(helpAbout);


    add(lblWelcome);

}

public static void main(String[] args) {
    Welcome wlc = new Welcome();
    wlc.setVisible(true);
}

}

but when I run the program, line break can not be executed,

lblWelcome = new JLabel("Welcome, Admin \nto Car Tooner", SwingConstants.CENTER);

i want to make text like this "Welcome, admin (\n new line) to Car Tooner" can anyone help me?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Fahmi Jufri
  • 21
  • 1
  • 1
  • 6
  • 1
    guy, you really should have used some search before... [http://stackoverflow.com/questions/685521/multiline-text-in-jlabel][1] [1]: http://stackoverflow.com/questions/685521/multiline-text-in-jlabel – Honza Zidek Dec 20 '13 at 14:53
  • On a related note avoid to use system-dependent newline separators and use `System.getProperty("line.separator");` instead – Toni Toni Chopper Dec 20 '13 at 14:54
  • guy, you really should have made some search before: [http://stackoverflow.com/questions/685521/multiline-text-in-jlabel][1] [1]: http://stackoverflow.com/questions/685521/multiline-text-in-jlabel – Honza Zidek Dec 20 '13 at 14:54

4 Answers4

4

You can embed HTML to do this. I would use

lblWelcome = new JLabel("<html>Welcome, Admin <br/>to Car Tooner", SwingConstants.CENTER);
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
3

i want to make text like this "Welcome, admin (\n new line) to Car Tooner" can anyone help me?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

Surround the string with <html></html> and break the lines with <br>.

JLabel l = new JLabel("<html>Welcome admin <br>to car Toner</html>", SwingConstants.CENTER);

also see here for an extended discussion

Newline in JLabel

Community
  • 1
  • 1
gaurav5430
  • 12,934
  • 6
  • 54
  • 111
0
public static final String NL = System.getProperty("line.separator");  

You need to get line separator so it works cross platform as \n does not always work. \r\n is the correct way to do it in Windows for example. Just write the line.separator into a variable and add it any time you need it. This is why static is useful as it will be persistant across all instances of the class.

To build it in:

lblWelcome = new JLabel("Welcome, Admin" + NL + "to Car Tooner", SwingConstants.CENTER);
Adam Short
  • 498
  • 7
  • 28