Java window content changes but does not exceed the minimum size

I have Java applications that I wrote using NetBeans (yes, I read a lot of complaints here about NetBeans). When I resize the window, the content resizes using the window until I hide the window (height, in fact) below a certain size. The window then resizes, but the content is just cropped.

I checked all component widgets. All have a minimum size of zero or very small. The only ones that have non-zero sizes are those that do not change with their parents, and they are also small. When I contract, I am not in any of the widgets. At that moment when I can no longer shrink, there is a significant gap between the bottom of the panel and the widgets inside the bottom.

Is there any way to determine what limits the size in this way? Is there any other property that I should look for that could be involved?

Thank.

+2
source share
2 answers

yes and a very simple solution to just override and return getMinimumSize()

eg

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class CustomComponent extends JFrame {

    private static final long serialVersionUID = 1L;

    public CustomComponent() {
        setTitle("Custom Component Test");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void display() {
        add(new CustomComponents(), BorderLayout.NORTH);
        add(new CustomComponents(), BorderLayout.CENTER);
        add(new CustomComponents(), BorderLayout.SOUTH);
        add(new CustomComponents(), BorderLayout.EAST);
        pack();
        // enforces the minimum size of both frame and component
        setMinimumSize(getMinimumSize());
        setPreferredSize(getPreferredSize());
        setVisible(true);
    }

    public static void main(String[] args) {
        CustomComponent main = new CustomComponent();
        main.display();
    }
}

class CustomComponents extends JLabel {

    private static final long serialVersionUID = 1L;

    @Override
    public Dimension getMinimumSize() {
        return new Dimension(200, 100);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(300, 200);
    }

    @Override
    public void paintComponent(Graphics g) {
        int margin = 10;
        Dimension dim = getSize();
        super.paintComponent(g);
        g.setColor(Color.red);
        g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2);
    }
}
+1
source

Can I see a sample code? I believe that this is not a window, but the content inside the window that does not resize beyond a certain point.

0
source

All Articles