Question

I am trying to align JPanel vertically. setAlignmentY is not working as expected. Here is what I have done so far.

import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

public class ValignJPanel {

    public static void main(String args[]) {
        JFrame frame = new JFrame("Vertical Alignment");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel1 = wrapPanel(createPanel(), Component.TOP_ALIGNMENT);
        JPanel panel2 = wrapPanel(createPanel(), Component.CENTER_ALIGNMENT);
        JPanel panel3 = wrapPanel(createPanel(), Component.BOTTOM_ALIGNMENT);

        frame.setLayout(new GridLayout(1, 3));
        frame.add(panel1);
        frame.add(panel2);
        frame.add(panel3);

        frame.setSize(400, 150);
        frame.setVisible(true);
    }

    private static JPanel wrapPanel(JPanel oldPanel, float alignment) {
        JPanel panel = new JPanel();
        oldPanel.setAlignmentY(alignment);
        panel.add(oldPanel);
        return panel;
    }

    private static JPanel createPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.add(new JLabel("Hello world"));
        panel.add(new JTextField(10));
        panel.setBorder(new LineBorder(Color.red));
        return panel;
    }
}

I am trying to create a wrapper method which takes JPanel & wraps it as given alignment.

EDIT (from comment, with image) I am trying to get output which looks like this, but in place of button, i want to have JPanel

enter image description here

Was it helpful?

Solution

What you need to do is set the layout to BoxLayout for the new JPanel in the wrapPanel method.

JPanel panel = new JPanel();
BoxLayout layout = new BoxLayout(panel, BoxLayout.LINE_AXIS);
panel.setLayout(layout);

Bug: With your current code It doesn't seem to work because it'll stretch the text field. If you use a JButton in place of the JTextFeild you will see the effect. I'm still trying to figure out why the text field does that.


UPDATE Along with the above code, to fix the problem of the text field stretching, as answered by Camickr in this answer

BoxLayout is one of the few layout managers that respects the minimum and maximum sizes of a component. So if you want to prevent a panel from stretching you can use:

panel.setMaximumSize( panel.getPreferredSize() );

The above code is what you want to put in the createPanel method.

enter image description here

import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

public class ValignJPanel {

    public static void main(String args[]) {
        JFrame frame = new JFrame("Vertical Alignment");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel1 = wrapPanel(createPanel(), Component.TOP_ALIGNMENT);
        JPanel panel2 = wrapPanel(createPanel(), Component.CENTER_ALIGNMENT);
        JPanel panel3 = wrapPanel(createPanel(), Component.BOTTOM_ALIGNMENT);

        frame.setLayout(new GridLayout(1, 3));
        frame.add(panel1);
        frame.add(panel2);
        frame.add(panel3);
        frame.setSize(400, 150);
        frame.setVisible(true);
    }

    private static JPanel wrapPanel(JPanel oldPanel, float alignment) {
        JPanel panel = new JPanel();
        BoxLayout layout = new BoxLayout(panel, BoxLayout.LINE_AXIS);
        panel.setLayout(layout);
        oldPanel.setAlignmentY(alignment);
        panel.add(oldPanel);
        return panel;
    }

    private static JPanel createPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.add(new JLabel("Hello world"));
        panel.add(new JTextField(10));
        panel.setBorder(new LineBorder(Color.red));
        panel.setMaximumSize( panel.getPreferredSize());
        return panel;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top