2

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

alex2410
  • 10,904
  • 3
  • 25
  • 41
  • Post link to your image. – alex2410 Jan 09 '14 at 11:54
  • what is the value of panel.getWidth()? – wxyz Jan 09 '14 at 11:59
  • [Here's the screenshot](http://i.imgur.com/cCEx8LI.png) – iamstickfigure Jan 09 '14 at 12:41
  • Thank you @wxyz Lol. I printed that value to the console, and I discovered that it was in fact 0. The problem is that I was calling `panel.getWidth()` before the width was even set of `panel`. The preferred width had already been set, but I think `getWidth()` will continue to be 0 until the layout manager takes care of it. You can post that as your answer if you want since your question brought me to the solution. :-) – iamstickfigure Jan 09 '14 at 12:57

1 Answers1

1

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.

Community
  • 1
  • 1
alex2410
  • 10,904
  • 3
  • 25
  • 41
  • I don't think that really helps. That panel must be a large canvas which displays an image that I can pan around on. If I remove that line, I'm not able to scroll at all. – iamstickfigure Jan 09 '14 at 12:39
  • And there are no components inside that panel, so it's not as simple as just letting the layout manager take care of it. It's just a canvas that I paint to with paintComponent. I posted a screenshot in a comment above – iamstickfigure Jan 09 '14 at 12:46