Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set distance between elements ordered vertically?

I have code like that:

    JPanel myPanel = new JPanel();
    myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.Y_AXIS));

    JButton button = new JButton("My Button");
    JLabel label = new JLabel("My label!!!!!!!!!!!");

    myPanel.add(button);
    myPanel.add(label);

In this way I get elements with no distance between them. I mean, the "top" elements always touches the "bottom" element. How can I change it? I would like to have some separation between my elements?

I think about adding some "intermediate" JPanel (with some size) between my elements. But I do not think it is an elegant way to get the desired effect. Can somebody, please, help me with that?

like image 741
Roman Avatar asked Apr 01 '10 14:04

Roman


People also ask

How do I put vertical space between elements in HTML?

So <spacer size="32"></spacer> will create 32px of vertical space. You can use the spacer inside of your element to provide more space or between elements.

How do I make vertical spacing in CSS?

Use the line-height property in CSS to do so. Browsers by default will create a certain amount of space between lines to ensure that the text is easily readable. For example, for 12-point type, a browser will place about 1 point of vertical space between lines.

How do you put a space between two vertical divs?

To give to space between two divs, just add display: flex in the parent element. Then add an justify-content: space-between after adding this you will see a space between two divs.

Which property is used for vertical spacing?

Syntax. The border-spacing property may be specified as either one or two values. When one <length> value is specified, it defines both the horizontal and vertical spacings between cells.


1 Answers

    JPanel myPanel = new JPanel();
    myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.Y_AXIS));

    JButton button = new JButton("My Button");
    JLabel label = new JLabel("My label!!!!!!!!!!!");

    myPanel.add(button);
    myPanel.add(Box.createVerticalStrut(20));
    myPanel.add(label);

will be one way of doing it.

like image 122
Kannan Ekanath Avatar answered Sep 20 '22 22:09

Kannan Ekanath