Search code examples
javaswinglayout-managerflowlayout

How to check if FlowLayout overflow?


In FlowLayout if there is no space for components, some of them moving to the next line. Is there way to check if layout overflowed programmatically?


Solution

  • You can use ChangeListener for JScrollPane

    final JScrollPane jPane = new JScrollPane(yourPanel);
    jPane.getViewport().addChangeListener(new ChangeListener() {
    
        public void stateChange(ChangeEvent e) {
    
            //if that is true, it means panel overflowed
            if(jPane.getHorizontalScrollBar().isShowing()) {
    
            }
        }
    
    })