user3166216
user3166216

Reputation: 45

Position JLabels underneath each other

For my application I am creating a script editor. At the moment I have the line numbers displaying in a JPanel containing represented by JLabels. However, when I add new JLabels to represent new line numbers, the JLabels appear on the center of the panel, even when I set the JLabel's setVerticalAlignment and setVerticalTextPosition. I want it so that the labels would appear underneath each other.

The JPanel class constructor which holds all the JLabels:

public LineNumberPanel() {

    setPreferredSize(new Dimension(width, height));
    setLayout(new GridLayout(0, 1));

    //setup the label
    label = new JLabel(String.valueOf(lineCount));
    label.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 15));

    //setup the label alignment
    label.setVerticalAlignment(JLabel.TOP);
    label.setHorizontalAlignment(JLabel.CENTER);
    label.setVerticalTextPosition(JLabel.TOP);
    setAlignmentY(TOP_ALIGNMENT);

    add(label);
}

The method which is used to add JLabels to the JPanel:

public void increaseLineNumber() {
    lineCount++;

    JLabel tempLabel = new JLabel(String.valueOf(lineCount));
    tempLabel.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 15));

    tempLabel.setVerticalAlignment(JLabel.TOP);
    tempLabel.setHorizontalAlignment(JLabel.CENTER);
    tempLabel.setVerticalTextPosition(JLabel.TOP);

    revalidate();
    this.add(tempLabel);
}

What the it currently looks like when adding new JLabels:

enter image description here

Upvotes: 1

Views: 6002

Answers (2)

Paul Samsotha
Paul Samsotha

Reputation: 208944

Don't use a GridLayout. It will stretch your components. If you use a BoxLayout it won't

A convenience class for BoxLayout is Box. You can do

Box box = Box.createVerticalBox();
add(box);

Then just add the labels to the box

box.add(label);

Example

enter image description here

import java.awt.*;
import javax.swing.*;
import javax.swing.border.BevelBorder;

public class BoxLabels {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                Box box = Box.createVerticalBox();
                Font font = new Font("monospaced", Font.PLAIN, 11);

                JPanel sideBar = new JPanel();
                sideBar.setBackground(Color.BLACK);
                sideBar.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
                sideBar.add(box);

                JTextArea text = new JTextArea(15, 50);
                text.setMargin(new Insets(5, 5, 5, 5));
                text.setBackground(Color.darkGray);
                text.setForeground(Color.white);
                text.setFont(font);

                int count = 1;
                for (int i = 0; i < 10; i++) {
                    JLabel label = new JLabel(String.valueOf(count));
                    label.setFont(font);
                    label.setForeground(Color.GREEN);
                    box.add(label);
                    count++;
                }

                JPanel panel = new JPanel(new BorderLayout());
                panel.add(text);
                panel.add(sideBar, BorderLayout.WEST);

                JOptionPane.showMessageDialog(null, panel);
            }
        });
    }
}

See more How to use BoxLayout and other Layout Managers at Laying out Components Within a Container

Upvotes: 1

ROT13
ROT13

Reputation: 375

You should try to set the PreferedSize of the Labels accordingly. Unfortunately it isn't clear which LayoutManager you use.

Upvotes: 0

Related Questions