Home > Java > javaTutorial > How can I limit the maximum width of labels in a Swing GroupLayout-managed form to a fraction of the parent frame's width while handling resizing?

How can I limit the maximum width of labels in a Swing GroupLayout-managed form to a fraction of the parent frame's width while handling resizing?

Patricia Arquette
Release: 2024-12-15 02:25:08
Original
119 people have browsed it

How can I limit the maximum width of labels in a Swing GroupLayout-managed form to a fraction of the parent frame's width while handling resizing?

Swing GroupLayout: Resizing and Limiting Component Sizes

Question

In a dynamically generated data input form managed by Swing's GroupLayout, how can one limit the maximum width of labels to a specific fraction of the parent frame's width, while adapting to resizing events?

Answer

To effectively limit label width using GroupLayout:

  • Leverage the preferred size of the label, which naturally accommodates the largest label.
  • Rely on the default behavior of components for resizing.
  • Utilize GroupLayout.Alignment.TRAILING to right-justify the labels if desired.

Example Code

import javax.swing.GroupLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class LabelWidthLimitPanel extends JPanel {

    private JLabel label1 = new JLabel("Primary:");
    private JTextField field1 = new JTextField(16);
    private JLabel label2 = new JLabel("Secondary:");
    private JTextField field2 = new JTextField(16);
    private JLabel label3 = new JLabel("Tertiary:");
    private JTextField field3 = new JTextField(16);

    public LabelWidthLimitPanel() {
        GroupLayout layout = new GroupLayout(this);          
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        layout.setHorizontalGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                .addComponent(label1)
                .addComponent(label2)
                .addComponent(label3))
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(field1)
                .addComponent(field2)
                .addComponent(field3))
        );
        layout.setVerticalGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(label1)
                .addComponent(field1))
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(label2)
                .addComponent(field2))
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(label3)
                .addComponent(field3))
        );
    }
}
Copy after login

In this example:

  • GroupLayout effectively resizes the labels without manual intervention.
  • The text fields are resizable, ensuring optimal use of space.
  • The labels are right-justified using alignment.

The above is the detailed content of How can I limit the maximum width of labels in a Swing GroupLayout-managed form to a fraction of the parent frame's width while handling resizing?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template