1

I am trying to prevent a JPanle to get so small that it cuts off it subcomponents, is there anyway to enforce this? After reading this I tried (in jython):

frame= JFrame('example', size=(200,200))

pan = JPanel()
pan.add(JLabel('beer'))
pan.add(JButton('get one', actionPerformed=printer))
pan.setMinimumSize(panel.getPreferredSize())

frame.add(pan)
frame.visible = True

however the panel can still be shrunk so far that it cuts off its components (in a JFrame or here), even if I apply the minimum size to the frame. How does one prevent this? ( I assume the preferred size I am getting si not what I want since with the default layout manager it seems to be the size of the largest subcomponent, but if I change it to e.g. GridBagLayout it seems to get smaller 9apply minsize to frame and try)

Community
  • 1
  • 1
ted
  • 4,791
  • 5
  • 38
  • 84
  • What is your desired behavior? Do you want the JFrame to refuse to shrink, do you want scrollbars to appear on the edges, or something else? – Jacob Raihle Jul 10 '12 at 10:31
  • I want the JFrame/panel (think multiple panels in Frame) to refuse shrinking. For scrollbars I am aware of `JScrollPane`. The problem is, that if I use `setMinimumSize` on frame in the same way, it still becomes so small that the button will be cut off (the label is always fully visible if I apply `minimumSize` to `frame` instead of panel – ted Jul 10 '12 at 10:54

1 Answers1

3

You can use this on your JFrame (sorry it's Java, I am not familiar with Jython):

frame.setMinimumSize(frame.getPreferredSize());

Your code could work if the LayoutManager of the content pane of the JFrame enforces minimum sizes. However, by default you get a BorderLayout which does not. If you used frame.setMinimumSize(pan.getPreferredSize());, it will not work because the size of the frame includes its insets (ie, the size of the border of the frame) so the minimum size you are setting is actually too small.

Here is a small demo showing how you can make minimum size work on JFrame:

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestMinimumSizeOnFrame {

    protected void initUI() {
        JFrame frame = new JFrame("example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(200, 200);

        JPanel pan = new JPanel();
        pan.add(new JLabel("Some nice beer"));
        pan.add(new JButton("get one"));
        frame.add(pan);
        frame.setVisible(true);
        frame.setMinimumSize(frame.getPreferredSize());

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TestMinimumSizeOnFrame().initUI();
            }
        });
    }

}
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • thats fine, the transfer is easy, I jsut added it so people don't wonder about the syntax, I have been working a lot with Jython lately so I don't want to post wrong java syntax or translate every minimal example before posting. Thank you for your answer with `Some nice ` example – ted Jul 10 '12 at 14:12