My lecturer has told us that we must use the pack() method when designing Java Swing GUIs for coursework. I understand this causes the window to be resized to fit the layout of the subcomponents, but when I try to setSize(-,-) then use pack() it obviously resizes it. Why would I have to use pack()? What would happen if I didn't?
            Asked
            
        
        
            Active
            
        
            Viewed 126 times
        
    0
            
            
         
    
    
        Dan O
        
- 6,022
- 2
- 32
- 50
- 
                    "What would happen if I didn't" -- why not have a go? – slim Mar 09 '17 at 13:48
- 
                    Better ask your licturer. Also consider using `setPreferredSize` instead of `setSize` – Zefick Mar 09 '17 at 13:50
- 
                    @Zefick I think you intended to say [override the `getPreferredSize`](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) instead of `setPreferredSize` (please take a look at the link) :) – Frakcool Mar 09 '17 at 14:26
- 
                    @Frakcool I can not imagine why somebody would override `getPreferredSize. But using `setPreferredSize` before 'pack' gives Swing a hitn which dimensions to use for layouting. `setSize` doesn't work this way and it's indeed incompatible with `pack`. I don't talk about is it good or bad to use this methods. – Zefick Mar 09 '17 at 15:50
- 
                    1@Zefick I can imagine lots of reasons to override `getPreferredSize`, but I'd never use either on a `JFrame`, it's not the responsibility of the `JFrame` to be making those decisions, equally, it's not some external logic's responsibility to be deciding the size of the components, that's the responsibility of the component itself, which is why I'd override `getPreferredSize` when I can to control the sizing hints – MadProgrammer Mar 09 '17 at 20:54
