Search code examples
javaswingspringlayout

Making sure SpringLayout doesn't shrink below a certain size


I'm trying to implement a quite simple UI using SpringLayout (partly because I, as opposed to most tutorial writers I find on the net, quite like the coding interface compared to other layout managers and partly because I want to learn how to use it). The UI basically looks like this:

UI template

This is all well. The UI resizes the way I want (keeping the welcome text centered and expanding the text area to fill all the new available space) if I increase the window size. However, below a certain point (more specifically when the window becomes too narrow for the welcome text):

UI when shrunk

I would like the window to not allow further shrinking, so that if the user tries to shrink the window to a size smaller than enough to house the components, it simply stops. How do I accomplish this, using the SpringLayout layout manager?

I know I could probably do this by handling some resize-event and checking if the minimum size is reach, and then just set the size to the minimum size. But this requires me to a) know, or know how to calculate, the minimum size of the window, even before it renders, b) write a bunch of event-handling code just to get some UI rendering right, and c) write a bunch of code for things that I expect a good layout manager to take care of ;)


Solution

    1. you can override MinimumSize for TopLevelContainer

    2. you have put JTextArea to the JScrollPane

    3. easiest way is mixing LayoutManagers (called as NestedLayout) by spliting GUI to the parts (separated JPanels with same or different LayoutManager), rather than implements some most sofisticated LayoutManager (GridBagLayout or SpringLayout) for whole Container

    4. some LayoutManagers pretty ignore setXxxSize

    5. SpringLayout isn't my cup of Java

    import java.awt.*;
    import javax.swing.*;
    
    public class MinSizeForContainer {
    
        private JFrame frame = new JFrame("some frame title");
    
        public MinSizeForContainer() {
            JTextArea textArea = new JTextArea(15, 30);
            JScrollPane scrollPane = new JScrollPane(textArea);
    
            CustomJPanel fatherPanel = new CustomJPanel();
            fatherPanel.setLayout(new SpringLayout());
    
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(fatherPanel, BorderLayout.CENTER);
            frame.setLocation(20, 20);
            frame.setMinimumSize(fatherPanel.getMinimumSize());
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    MinSizeForContainer Mpgp = new MinSizeForContainer();
                }
            });
        }
    }
    
    class CustomJPanel extends JPanel {
    
        private static final long serialVersionUID = 1L;
    
        @Override
        public Dimension getMinimumSize() {
            return new Dimension(400, 400);
        }
    }