Why do I need to set the width to get the maximum size when it contains box horizontal glue in the panel that has a box layout?

44 Views Asked by At

I need to set this CBox (cricle by blue on the image 1) to not max height and good width. But when I use setMaximumSize, which comes with two parameters, the first is width. I set it around 100 to 200, but it is not working out; it is still small. and when I use bookIDCBox.getMaximumSize().width, it works out. I don't know why I set the maximum width when it contains horizontal glue boxes in the panel.

This is my GUI:

https://i.imgur.com/YmMZh41.png

the panel has 2 horizontal glue boxes and CBox has mixumum size. Code:

private JPanel buttonPanel() {
        JPanel buttonPanel = new JPanel();
        buttonPanel.setPreferredSize(new Dimension(0, 80));
        buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
        buttonPanel.add(Box.createHorizontalGlue());
        buttonPanel.add(insertBtn = new JButton("Thêm"));
        buttonPanel.add(Box.createHorizontalStrut(5));
        buttonPanel.add(resetBtn = new JButton("Xóa trắng"));
        buttonPanel.add(Box.createHorizontalStrut(5));
        buttonPanel.add(removeBtn = new JButton("Xóa"));
        buttonPanel.add(Box.createHorizontalStrut(5));
        buttonPanel.add(updateBtn = new JButton("Sửa"));
        buttonPanel.add(Box.createHorizontalStrut(5));
        buttonPanel.add(saveBtn = new JButton("Lưu"));
        buttonPanel.add(Box.createHorizontalStrut(5));
        buttonPanel.add(new JLabel("Tìm theo mã sách:"));
        buttonPanel.add(Box.createHorizontalStrut(5));
        buttonPanel.add(bookIDCBox = new JComboBox<>());
        bookIDCBox.setMaximumSize(new Dimension(bookIDCBox.getMaximumSize().width, bookIDCBox.getMinimumSize().height));
        buttonPanel.add(Box.createHorizontalGlue());
        insertBtn.addActionListener(this);
        resetBtn.addActionListener(this);
        removeBtn.addActionListener(this);
        updateBtn.addActionListener(this);
        saveBtn.addActionListener(this);
        return buttonPanel;
}

GUI with width of CBox 200

https://i.imgur.com/h2UCoov.png

bookIDCBox.setMaximumSize(new Dimension(200, bookIDCBox.getMinimumSize().height));

I need explain why maximum width size work... Thanks

0

There are 0 best solutions below