13
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
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Jessy
  • 15,321
  • 31
  • 83
  • 100
  • Possible duplicate of [add controls vertically instead of horizontally using flow layout](http://stackoverflow.com/questions/13510641/add-controls-vertically-instead-of-horizontally-using-flow-layout) – Abhijeet Feb 25 '16 at 06:38

5 Answers5

24

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);
...
Community
  • 1
  • 1
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
5

I read this piece of code:

 pMeasure.setLayout(new BoxLayout(pMeasure, BoxLayout.VERTICAL));

It seems BoxLayout doesn't have VERTICAL. Upon searching, this will work using the following code:

 pMeasure.setLayout(new BoxLayout(pMeasure, BoxLayout.Y_AXIS));
princepiero
  • 1,287
  • 3
  • 15
  • 28
  • @pricepiero Thanks `pMeasure.setLayout(new BoxLayout(pMeasure, BoxLayout.Y_AXIS));` solves it for me. Vertically lays-out all the components in the JPanel. – Abhijeet Feb 25 '16 at 06:09
3

Here is what you need to use:

JLabel economy = new JLabel("<html>Economy<br>Regularity</html>");
Emir
  • 762
  • 2
  • 8
  • 22
  • 2
    This would let you create only one JLabel, if you are waiting for data to come in it requires you to have a lot more code. It's.. inefficient. – Lolmewn Feb 05 '12 at 20:40
0

A quick way is to use html within the JLabel. For instance include the <br/> tag.

Otherwise, implement a BoxLayout.

akf
  • 38,619
  • 8
  • 86
  • 96
waqasahmed
  • 3,555
  • 6
  • 32
  • 52
0

Make a separate JPanel for each line, and set the dimensions to fit each word:

JLabel wordlabel = new JLabel("Word");

JPanel word1 = new JPanel();
word1.setPreferredSize(new Dimension(#,#);

This should work for each word. You can then add each of those JPanels to your main JPanel. This also allows you to add other components next to each word.