0

Although I have successfully created windows with scrollbars, I have one particular case where the scrollbars will not appear even though the content within the JScrollPane is too large for the viewing area.

I have a button in a window that opens a new window displaying info from a specific JSONObject. In order to do that, it creates an instance of a class that extends JFrame. As some other needed knowledge, DetailBox is a JPanel that contains some manually drawn shapes based on obj, a JSONObject. The paint method of the JFrame class calls super.paint(), then the following method (summarized for simplicity).

    public void drawAdvancedView(Graphics g) {
        
        JPanel all = new JPanel(); // "all", as the name suggests, will contain all the elements of the window.
        all.setLayout(null);
        
        // Create a DetailBox (a custom JPanel) and place it within "all":

        DetailBox box = new DetailBox(obj);
        box.setBounds(0, 0, 400, ((obj.getInt("linecount") * 2) + 100));
        box.paintComponent(g);
        all.add(box);
        
        // The JPanel "lower" contains some additional text information placed in a JTabbedPane below the drawing.
        
        JPanel lower = new JPanel();
        lower.setBounds(0, ((obj.getInt("linecount") * 2) + 100), 400, 300);
        JTabbedPane tabs = new JTabbedPane();
        
        // Create a JPanel, place it inside of a JScrollPane, then place it within "tabs".
        
        // Create a JPanel, place it inside of a JScrollPane, then place it within "tabs".
        
        // Create a JPanel, place it inside of a JScrollPane, then place it within "tabs".
        
        lower.add(tabs);
        all.add(lower);

        // At this point, "all" contains all the elements wanted in the window.
        
        JScrollPane sizeLocker = new JScrollPane(all); // Next, create the JScrollPane "sizeLocker" and place "all" within it.
        sizeLocker.setMaximumSize(new Dimension(400, 500));
        sizeLocker.getVerticalScrollBar().setUnitIncrement(16);
        
        add(sizeLocker);
        created = true;
    }

So far, this all basically works fine. When the button is clicked, the appropriate window is generated and shown. However, one thing always seems to be missing: The JScrollPane. Resizing the window to be smaller in any way will simply hide elements, rather than allow the user to scroll. Notably, all the content is there, and is added to the JFrame only through the JScrollPane itself. Modifying the JScrollPane with a setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS) statement does show a scrollbar, but it is always greyed out. I've also attempted to remove various elements of the window, asking myself things like, "Do nested JScrollPanes not work? Maybe it's the DetailBox? Try removing lower altogether?" I tried removing and re-adding elements based on these thoughts and more and not one made any difference, other than the individual element itself obviously disappearing from the frame.

Has anyone else ever encountered something like this, and maybe have a fix? I'm out of ideas that don't seriously modify the behavior of the program, like trying a layout instead of using absolute positioning through setBounds() (I tried and failed at making DetailBox work in a layout without having other elements draw on top of it). The only real difference I can see between this JScrollPane and one that works elsewhere in the same program is that this is added to the JFrame from inside of it, rather than added from outside of it. However, it still uses the add() method, so I can't see how it'd have any serious impact. I'd be more than happy to give more details and/or code if they're needed, of course.

  • 2
    `paintComponent(...)` directly. Swing will repaint() components automatically. If you are attempting to dynamically add components to a panel, then you need to revalidate() the panel so the layout manager is invoked. 3. Don't override paint() on a JFrame. 4) Don't pass a Graphics object to your method. Swing will create the Graphics object as needed. – camickr Jul 02 '21 at 00:43
  • 2
    Read the Swing tutorial on [Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) for the proper way to do custom painting. The tutorial also has a link on `Layout Managers`. – camickr Jul 02 '21 at 00:44
  • 2
    `null` layouts aren't going to work. `JScrollPane` makes use of the panels calculated preferred size to make determinations about when scroll bars are needed – MadProgrammer Jul 02 '21 at 02:14

0 Answers0