Danstern
Danstern

Reputation: 147

Java Swing Alignment problem with BoxLayout

I have three components inside a top to down boxLayout (JLabel, JTextField, JButton). The problem is that when i set the X alignment for the label it looks as if i would've changed the X alignment of the button and vice versa, only when both have the same alignment it works fine.

When the screen gets wider both components take a weird alignment. enter image description here

when both components have the same alignment everything works fine. enter image description here

here is my code:

public void create(){
    JPanel panel =  new JPanel();
    BoxLayout boxLayout = new BoxLayout(panel, BoxLayout.Y_AXIS);
    panel.setLayout(boxLayout);
    panel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
    
    JLabel etiqueta =  new JLabel("Numero de consultorio: ");
    etiqueta.setBackground(Color.BLUE);
    etiqueta.setOpaque(true);
    etiqueta.setAlignmentX(Component.LEFT_ALIGNMENT);
    panel.add(etiqueta);
    
    JTextField consultorio = new JTextField();
    panel.add(consultorio);
    
    JButton registrar =  new JButton("Registrar");
    registrar.setAlignmentX(Component.LEFT_ALIGNMENT);
    panel.add(registrar);
    
    this.getContentPane().add(panel, BorderLayout.CENTER);
}

Upvotes: 0

Views: 286

Answers (1)

Sergiy Medvynskyy
Sergiy Medvynskyy

Reputation: 11327

Here is the proposed by Andrew Thompson solution:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class TestFrame {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new TestFrame()::create);
    }

    private void create() {
        JPanel panel = new JPanel();
        BoxLayout boxLayout = new BoxLayout(panel, BoxLayout.Y_AXIS);
        panel.setLayout(boxLayout);
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        JLabel etiqueta = new JLabel("Numero de consultorio: ");
        etiqueta.setBackground(Color.BLUE);
        etiqueta.setOpaque(true);
        JPanel layout = new JPanel(new FlowLayout(FlowLayout.LEADING, 0, 0));
        layout.add(etiqueta);
        panel.add(layout);

        JTextField consultorio = new JTextField();
        panel.add(consultorio);

        JButton registrar = new JButton("Registrar");
        layout = new JPanel(new FlowLayout(FlowLayout.TRAILING, 0, 0));
        layout.add(registrar);
        panel.add(layout);

        JFrame frm = new JFrame("Test");
        frm.getContentPane().add(panel, BorderLayout.CENTER);
        frm.pack();
        frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frm.setLocationRelativeTo(null);
        frm.setVisible(true);
    }

}

Upvotes: 2

Related Questions