Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BoxLayout ignores setYAlighment

Here is a function:

/**
 * Creates an instance of a JLabel with the given arguments
 * @param text The text to be displayed on the Label
 * @param font The font of the label
 * @param bold set to true if you want the label's text to be bold
 * @param fontSize The size of the font to be rendered
 * @param alignment An alignment (E.g. CENTER_ALIGNMENT)
 * @param verticleAlignment an optional argument to allow one to choose the Y alignment
 * **/
public JLabel createLabel(String text, String font, boolean bold, int fontSize, float alignment, float...verticleAlignment){
    JLabel label = new JLabel(text);
    label.setFont(new Font(font, bold ? Font.BOLD : Font.PLAIN, fontSize));


    label.setAlignmentX(alignment);
    if(verticleAlignment.length > 0){
        label.setAlignmentY(verticleAlignment[0]);
    }
    return label;
}

For some reason, no matter what I enter in the varArg verticleAlignment, it doesn't actually apply?

add(createLabel("ChatBytes™ - Do not steal.", "Arial", false, 12, CENTER_ALIGNMENT, BOTTOM_ALIGNMENT));

Can anyone see a reason why it would ignore the setYAlignment part of my function?

This is how it renders

like image 307
Shaun Wild Avatar asked Feb 16 '23 07:02

Shaun Wild


1 Answers

As an alternative, consider a nested layout. In the example below, the relevant label is added to the SOUTH area of a BorderLayout, the default for JFrame, and a placeholder for your login panel is added to CENTER. Examine the resize behavior of each approach for suitability.

Addendum: I hope to [learn] why the setAlignmentY() is being ignored.

As noted in How to Use BoxLayout: Box Layout Features, "When a BoxLayout lays out components from top to bottom, … any extra space appears at the bottom of the container." This explains your original observation and correct solution.

In the API, note that setAlignmentX() "Sets the the vertical alignment," and setAlignmentY() "Sets the the horizontal alignment." In this context, vertical means the vertical axis of a top-to-bottom layout, such as BoxLayout.Y_AXIS, while horizontal means the horizontal axis of a left-to-right layout, such as BoxLayout.X_AXIS. In How to Use BoxLayout: Fixing Alignment Problems, BoxAlignmentDemo contrasts the two. In a left-to-right layout, pictured below, setAlignmentY() is used to adjust vertical positioning relative to the horizontal layout axis. In a top-to-bottom layout, such as yours, setAlignmentY() simply has no effect.

setAlignmentY


image

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

/** @see https://stackoverflow.com/a/18805146/230513 */
public class Test {

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(createLogin(), BorderLayout.CENTER);
        JLabel admonition = new JLabel("ChatBytes™—Do not steal.", JLabel.CENTER);
        f.add(admonition, BorderLayout.SOUTH);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private static JPanel createLogin() {
        JPanel p = new JPanel();
        p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
        JLabel label = new JLabel("Existing CHATBYTES login panel.");
        label.setFont(label.getFont().deriveFont(Font.ITALIC, 24f));
        label.setAlignmentX(0.5f);
        label.setBorder(new EmptyBorder(0, 20, 0, 20));
        p.add(Box.createVerticalStrut(36));
        p.add(label);
        p.add(Box.createVerticalStrut(144));
        return p;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Test().display();
            }
        });
    }
}
like image 92
trashgod Avatar answered Mar 07 '23 06:03

trashgod