0

I have a JScrollPane processesScrollPane which has a JPanel processButtonPanel as component. Now in this component I add JButtons which come from an ArrayList I fill up while the programming is running.

I've set some settings of the JScrollPane, but the problem I'm having is that when there are more buttons in the JPanel then I can display, the JScrollPane isn't enabling its scrollbars.

Here some code snippets:

    processesScrollPane = new JScrollPane();
    processesScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    processesScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    processesScrollPane.setBounds(12, 42, 221, 380);
    processPanel.add(processesScrollPane); //processPanel is the JPanel which holds the JScrollPane
    
    processButtonPanel = new JPanel(); // here are the buttons added whenever they are made and placed in the buttonList
    processesScrollPane.setViewportView(processButtonPanel);
    processButtonPanel.setLayout(null);
    
    processesScrollPane.setViewportView(processButtonPanel);
    
    buttonList = new ArrayList<JButton>();

and the code where I'm making the buttons:

    JButton b = new JButton("Process " + i);
    b.setBounds(12, 5 + (30*i), 174, 25);
    processButtonPanel.add(b);
    processButtonPanel.repaint();
    buttonList.add(b);
                
    processesScrollPane.revalidate();
    processesScrollPane.repaint();

(Yes I'm using no layout, but that's because I'm not going to scale the whole window etc)

Can anyone help me with the enabling of the scrollBars at the right time?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Tomato
  • 336
  • 5
  • 19
  • 3
    1) `processesScrollPane.setBounds(12, 42, 221, 380);` That is likely part of the problem. Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for white space. 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). – Andrew Thompson Apr 12 '15 at 10:10
  • 4
    The scroll bars are shown when the preferred size of the contained panel is large enough. Determining the preferred size is normally the job of the layout manager - just use one, as that's the way swing is designed to be used. `null` layouts cause no end of problems. – kiheru Apr 12 '15 at 10:27
  • 3
    Welcome to yet another reason why it is unrecommended to use null layouts. Layout managers are central to Swing – MadProgrammer Apr 12 '15 at 10:31

0 Answers0