0

After reviewing the existing answers to this issue, I am still in the dark here. I am trying to implement scrollable panels inside a tabbed frame. I am using JScrollPane and JTabbedPane. However, scrolling is not working.

This is my code:

public TabbedFrame(List<Panel> panels) {

    super(new GridLayout(1, 1));

    JTabbedPane tabbedPane = new JTabbedPane();

    int layerNum = 0;

    for (Panel p: panels){
        JScrollPane scrollPane = new JScrollPane(
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane.setViewportView(p);
        scrollPane.setPreferredSize(new Dimension(500, 500));
        tabbedPane.addTab("Layer " + layerNum, null, scrollPane, "Layer " + layerNum);
        layerNum++;
    }

    //Add the tabbed pane to this panel.
    add(tabbedPane);

    //The following line enables to use scrolling tabs.
    tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
}

public class Panel extends JPanel{

    private static final long serialVersionUID = 4109034958236244752L;

    private Design design;

    public Panel(Design design){
        this.design = design;
        this.setSize(design.getCanvasWidth(),    design.getCanvasHeight());
    }

    public void paint(Graphics G) { 
        G.setColor(design.getColor());
        design.draw(G); 
    }

    public int getWidth() { return design.getCanvasWidth(); }
    public int getHeight() { return design.getCanvasHeight(); }

}

I will appreciate any help you can offer.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 3
    For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Aug 20 '16 at 08:04
  • 1
    *"However, scrolling is not working."* - I guess because you never actually add any components to the `Panel` instance? Drawing onto it won't calculate the preferred size of it (you should override `paintComponent(Graphics)` instead of `paint(Graphics`) anyway). For that you could just *override* the `getPreferredSize()` method. But `scrollPane.setPreferredSize(new Dimension(500, 500));` doesn't have any effect here, it will still stretch across all available space. But as already mentioned, if this doesn't solve your problem, post a MCVE. – Lukas Rotter Aug 20 '16 at 08:49

1 Answers1

0

Found the problem. I should have set the preferable size of the panel as well:

for (Panel p: panels){
        JScrollPane scrollPane = new JScrollPane(
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        p.setPreferredSize(new Dimension(1000, 1000));
        scrollPane.setViewportView(p);
        scrollPane.setPreferredSize(new Dimension(500, 500));
        tabbedPane.addTab("Layer " + layerNum, null, scrollPane, "Layer " + layerNum);
        layerNum++;
    }
  • See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Aug 21 '16 at 08:47