Java window contents resize, but not beyond a minimum size
Asked Answered
U

2

2

I have a Java apps I've written using NetBeans (yes, I've read plenty of complaints here about NetBeans). When I resize the window, the contents resize with the window, until I shrink the window (the height, actually) below a certain size. Then the window resizes, but the contents just get clipped.

I've checked all of the component widgets. They all have min sizes of zero or very small numbers. The only ones with nonzero preferred sizes are those that do not resize with their parents, and they are also small. When shrinking, I'm not bumping into any of the widgets. At the point where I cannot shrink anymore, there is a sizable gap between the bottom of the panel and the widgets inside towards the bottom.

Is there any way I can tell what is limiting the size in this way? Is there another property I should be looking for that might be involved?

Thanks.

Unravel answered 14/9, 2011 at 23:25 Comment(4)
Please provide an sscce that exhibits the problem you describe.Philately
"I've read plenty of complaints here about NetBeans" I have no problem with people using whatever IDE with whatever plug-in they want. But I do object to vague descriptions of code, or having to look at the bloated crap that GUI builders generally churn out. Further, if a question is to the effect "How do I make my IDE..?", I'm out of that question. Got better things to do than provide support for IDEs. Even a questioner needing to mention what there IDE is, adds a certain suspicion that they have no idea what they are doing.Manoeuvre
it's all in the LayoutManager ... they are free to size however they are designed for. Choose a manager which behaves like you want it.Divisionism
Posting a few snapshots would help demonstrating the problem you have (and we could see what kind of components are on your layout). Of course, having a code snippet would also help a lot!Embayment
S
1

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

for example

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);
    }
}
Spinelli answered 14/9, 2011 at 23:31 Comment(3)
How would that help? When I examine the raw source that NetBeans generates for constructing the dialog, only a handful of items have minimum size set to anything, and they are small (85x16). Also, are you saying I should subclass every panel in order to override getMinimumSize()? That would only be necessary, I think, if setMinimumSize doesn't work. Is that broken? Thanks.Unravel
since I'm NetBeans user, I never used built-in Java Desktop Aplication, be sure that there must exist options for generate code or input custom method(s), but question is if are allowed or supported, you have advantage that you create GUI per once hour, but my advantage is full control over the JComponents, here is everything download.oracle.com/javase/tutorial/uiswing/components/… , but required handys coding :-)Spinelli
+1 for demonstrating how to override sizes in a custom component. I don't understand the reason for invoking setMinimumSize() and setPreferredSize() in display().Philately
B
0

Can we see some example code? I believe that its not the window but the content inside the window that's not resizing beyond a certain point.

Bretbretagne answered 14/9, 2011 at 23:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.