-1

I am trying to design a class of endless coordinate board with a grid. I have extended a JViewport and it draws at initial position ok (inside JScrollPane). How to tell scroll pane that there are some space to scroll in any direction?

The following does not help

            JCoordinateViewport coordinate = new JCoordinateViewport();
            coordinate.setBackground(Color.WHITE);
            //coordinate.setPreferredSize(new Dimension(10000, 10000));

            JScrollPane scroll = new JScrollPane();
            //scroll.setViewportView(coordinate);
            scroll.setViewport(coordinate);
            scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
            scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
            scroll.getVerticalScrollBar().setMinimum(-10000);
            scroll.getVerticalScrollBar().setMaximum(+10000);
            scroll.getHorizontalScrollBar().setMinimum(-10000);
            scroll.getHorizontalScrollBar().setMaximum(+10000);

UPDATE

Does anybody knows how JScrollPane determines scroll ranges from it's viewport?

UPDATE2

I found, that scrollbars appear t work if maximums and minimums are set after setVisible called.

But unfortunately, paintConponent does not called upon scroll.

Why?

UPDATE3

Although scroll bars work, they don't change viewport position.

Why?

Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385

2 Answers2

2
CoordinateViewport coordinate = new JCoordinateViewport(); coordinate.setBackground(Color.WHITE); //coordinate.setPreferredSize(new Dimension(10000, 10000));

JScrollPane scroll = new JScrollPane(); //scroll.setViewportView(coordinate);
scroll.setViewport(coordinate); 
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); 
scroll.getVerticalScrollBar().setMinimum(-10000); 
scroll.getVerticalScrollBar().setMaximum(+10000); 
scroll.getHorizontalScrollBar().setMinimum(-10000); 
scroll.getHorizontalScrollBar().setMaximum(+10000);

.

The following does not help

  • then issue should be only in the class with name CoordinateViewport

  • for better help sooner post an SSCCE, short, runnable, compilable, just about JFrame, JScrollPane and JViewport

  • for reducing of flickering in the JViewport is required to set

    1. own RepaintManager

    2. and by using/with built_in methods in JViewport

    JViewport.setScrollMode(JViewport.BLIT_SCROLL_MODE); JViewport.setScrollMode(JViewport.BACKINGSTORE_SCROLL_MODE); JViewport.setScrollMode(JViewport.SIMPLE_SCROLL_MODE);

  • please see Passing current Date and JTable how to change BackGround Color as potential source for SSCCE

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
2

As shown here, painting on a JViewport appears to "stick" to the viewport, while painting on the underlying scrollable component slides beneath. Sizes are integral multiples of TILE: for demonstration purposes, the preferred size of the viewport is made smaller than the underlying panel; in practice, it's better to override getPreferredSize(). See also ScrollAction, which auto-scrolls as the mouse hovers near any border.

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045