-1

I'm trying to make an array of buttons go inside of a JPanel. From there, I want the JPanel to be added to a JScrollPane so if there are more buttons that are needed, I will be able to scroll through all of the buttons as the app scales in size. However, after compiling, the JPanel is fine and will render all 10 buttons fine. Then the trouble begins, once I add the JPanel to the JScrollPane, the entire JScrollPane is completely blank.

    cMain.challengeModePanel = new JPanel(new GridLayout(10,1));
    cMain.challengeModePanel.setBackground(Color.black);
    cMain.challengeModePanel.setForeground(Color.white);
    cMain.challengeModePanel.setVisible(true);
    
    
    //panelConfiguration(cMain.challengeModePanel, 175, 240, 250, 250, Color.black, 10,1, cMain.window);
    
//The button generator method below was created by me to quickly add features to a button such as foreground, background, handlers and the panel it is going to be added to

    for (int i = 0; i < 10; i++) {
    cMain.challengeModeButtons[i] = new JButton();
    buttonGenerator(cMain.challengeModeButtons[i], cMain.font3, Color.black, Color.white, false, cMain.clickerHandler, "StartMenu", cMain.mouseHandler, cMain.challengeModePanel);
    cMain.challengeModeButtons[i].setSize(100, 50);
    cMain.challengeModePanel.add(cMain.challengeModeButtons[i]);
    
    }

    cMain.challengeModeScrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    cMain.challengeModeScrollPane.setColumnHeaderView(new JLabel("Challenges:"));
    cMain.challengeModeScrollPane.setBackground(Color.black);
    cMain.challengeModeScrollPane.setForeground(Color.white);
    cMain.challengeModeScrollPane.add(cMain.challengeModePanel);
    cMain.challengeModeScrollPane.setBounds(130, 180, 200, 300);
    cMain.challengeModeScrollPane.setVisible(true);
    
    
    
    cMain.window.add(cMain.challengeModeScrollPane);

I was expecting the program to render the buttons at the size they were set in within the code. From there, I was hoping that that JPanel would go inside of the JScrollPane and allow me to view it in a smaller area with the ability to scroll through all of the buttons akin to a JTextArea. However, as mentioned above, the JScrollPane is only rendering a blank pane with a vertical scroll and horizontal scroll bar.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • 2
    Don't use setBounds(...) or setSize(...). This implies you are using a null layout which you should NOT be doing. Swing was designed to be used with layout managers. It is the job of the layout manager to set the size/location of components based on the rules of the layout manager. – camickr Oct 25 '22 at 20:59

1 Answers1

1
cMain.challengeModeScrollPane.add(cMain.challengeModePanel);

Don't add components directly to the scroll pane. A scroll panel uses its own layout manager for the scrollbars and "viewport" etc, so the add(...) method doesn't do what you expect.

The component needs to be added to the "viewport" of the scroll pane.

This can be done by using:

scrollPane.setViewportView(challengeModePanel);

Or easier is to just use:

JScrollPane scrollPane = new JScrollPane(challengeModePanel, ...);

when you create the scroll pane.

Read the section from the Swing tutorial on How to Use Scroll Panes for more information.

camickr
  • 321,443
  • 19
  • 166
  • 288