JScrollPane viewport size

6.6k views Asked by At

I'm trying to have hist (HistoryPanel extends JPanel) be the viewport of the JScrollPane, histScroll. The problem is that the horizontal scroll bar doesn't show up unless I force it to show up with the scrollbar policy (in the code below), and even if I force it to, there's nowhere for it to scroll. The problem is that the size of the HistoryPanel refuses to increase horizontally. I tried different ways of setting the size of hist, but it continues to stay horizontally fixed in size. Vertically, however, it works perfectly fine. I don't understand what the problem is.

public SuperPanel(GoPanel panel)
{
    super(new BorderLayout());
    setFocusable(false);
    score = new ScorePanel();
    go = panel;
    go.scorePanel = score;
    score.setPreferredSize(new Dimension(panel.getWidth(), 20));
    hist = new HistoryPanel(go);
    hist.setPreferredSize(new Dimension(panel.getWidth()*20, 100*5));
    //hist.setMinimumSize(new Dimension(panel.getWidth()*20, 100*5));
    //hist.setSize(new Dimension(panel.getWidth()*20, 100*5));
    go.histPanel = hist;
    histScroll = new JScrollPane(hist, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    histScroll.setPreferredSize(new Dimension(panel.getWidth(), 100));
    add(score, "North");
    add(panel, "Center");
    add(histScroll, "South");
}

I can scroll down all I want, but I can't scroll to the right. Which is a big problem because it cuts off some of the image, and in this program, horizontal scrolling is actually a lot more important than vertical scrolling.

enter image description here

1

There are 1 answers

2
alex2410 On

You needn't to set prefered size to component which you add to JScrollPane, because it will be not scrollable, just remove next line :

hist.setPreferredSize(new Dimension(panel.getWidth()*20, 100*5));

Also read next post about setting size to component.