Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make JLabels start on the next line

JPanel pMeasure = new JPanel();
....
JLabel economy = new JLabel("Economy");
JLabel regularity = new JLabel("Regularity");
pMeasure.add(economy);
pMeasure.add(regularity);
...

When I run the code above I get this output:

Economy Regularity

How can I get this output, where each JLabel starts on a new line? Thanks

Economy  
Regularity
like image 669
Jessy Avatar asked Oct 08 '09 00:10

Jessy


People also ask

How do you go to the next line in a Java Swing?

In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.

How do you start a new line in JLabel?

Surround the string with <html></html> and break the lines with <br/> . just a little correction: use <br /> instead of just <br> ... this is recommended way of doing it (to not miss any closing tags)... happy coding...

How do you wrap text in JLabel?

As per @darren's answer, you simply need to wrap the string with <html> and </html> tags: myLabel. setText("<html>"+ myString +"</html>"); You do not need to hard-code any break tags.

What is box layout in Java?

public class BoxLayout extends Object implements LayoutManager2, Serializable. A layout manager that allows multiple components to be laid out either vertically or horizontally. The components will not wrap so, for example, a vertical arrangement of components will stay vertically arranged when the frame is resized.


2 Answers

You'll want to play around with layout managers to control the positioning and sizing of the controls in your JPanel. Layout managers are responsible for placing controls, determining where they go, how big they are, how much space is between them, what happens when you resize the window, etc.

There are oodles of different layout managers each of which allows you to layout controls in different ways. The default layout manager is FlowLayout, which as you've seen simply places components next to each other left to right. That's the simplest. Some other common layout managers are:

  • GridLayout - arranges components in a rectangular grid with equal-size rows and columns
  • BorderLayout - has one main component in the center and up to four surrounding components above, below, to the left, and to the right.
  • GridBagLayout - the Big Bertha of all the built-in layout managers, it is the most flexible but also the most complicated to use.

You could, for example, use a BoxLayout to layout the labels.

BoxLayout either stacks its components on top of each other or places them in a row — your choice. You might think of it as a version of FlowLayout, but with greater functionality. Here is a picture of an application that demonstrates using BoxLayout to display a centered column of components:

BoxLayout screenshot

An example of code using BoxLayout would be:

JPanel pMeasure = new JPanel();
....
JLabel economy = new JLabel("Economy");
JLabel regularity = new JLabel("Regularity");
pMeasure.setLayout(new BoxLayout(pMeasure, BoxLayout.Y_AXIS));
pMeasure.add(economy);
pMeasure.add(regularity);
...
like image 130
John Kugelman Avatar answered Nov 07 '22 06:11

John Kugelman


Here is what you need to use:

JLabel economy = new JLabel("<html>Economy<br>Regularity</html>");
like image 33
Emir Avatar answered Nov 07 '22 08:11

Emir