2

I have the one big panel on the left with a scrollPanel wrapped around it and it works fine; but in the upper right corner I have a panel which is also supposed to have a scrollPanel, but I cant seem to get it so that the scrollbars can actually move. They just stay full and unmovable. Can someone please help me work out why this is?

This scrollPanel is the one causing issues:

public MapBuilderSidePanel(Dimension size){
    tileSetPanel = new MapBuilderTileSetPanel(new Dimension(tileSetPanelWidth,tileSetPanelHeight),6,17, tileSetPanelHeight, tileSetPanelWidth, this);
    scrollPane = new JScrollPane(tileSetPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    //add(tileSet, BorderLayout.NORTH);
    add(scrollPane, BorderLayout.CENTER);

    setPreferredSize(size);
    setBorder(new BevelBorder(BevelBorder.LOWERED)); 
    setVisible(true);
}

This is the code for my little application, I've tried to strip out as much as possible. It is copy paste compile and runnable.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.BevelBorder;

public class MapBuilderFrame extends JFrame {

/**
 * @param args
 */
private String frameTitle = "Yoonsio Map Editor";
private int frameWidth = 1000;
private int frameHeight = 1000;
private MapBuilderPanel mainPanel;
private MapBuilderSidePanel myLeftSidePanel;
private MapBuilderSidePanel myRightSidePanel;
private JScrollPane scrollPane;



public MapBuilderFrame() {
    myRightSidePanel = new MapBuilderSidePanel(new Dimension(200,2000));
    mainPanel = new MapBuilderPanel(myRightSidePanel);
    setTitle(frameTitle);
    setSize(frameWidth,frameHeight);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
    setPreferredSize(new Dimension(450, 110));
    setResizable(false);
    scrollPane = new JScrollPane(mainPanel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    add(scrollPane, BorderLayout.CENTER);
    add(myRightSidePanel, BorderLayout.EAST);

}




public static void main(String[] args) {
    new MapBuilderFrame();
}



   }

/***************************************************************************/
class MapBuilderPanel extends JPanel {

/**
 * @param args
 */
private final int NUMERIC_TO_ASCII_OFFSET = 96;
private MapBuilderSidePanel mySidePanel;
private Rectangle hoverRect;

public MapBuilderPanel(MapBuilderSidePanel mySidePanel){
    this.mySidePanel = mySidePanel;
    setPreferredSize(new Dimension(2000,2000));
    setBackground(new Color(0x00006595));
}

public void refresh(){
    this.repaint();
}


public void paint(Graphics g){
    g.setColor(new Color(0x00006595));
    g.fillRect(0, 0, 2000, 2000);       
}



}
/***************************************************************************/

class MapBuilderSidePanel extends JPanel {


/* Ypos = the Y position counter used to draw the grid*/
private MapBuilderTileSetPanel tileSetPanel;
/* Width and Heights pertaining to the tileset grid */
private int tileSetPanelHeight = 400;
private int tileSetPanelWidth = 175;

private JScrollPane scrollPane;


/*only constructor needed*/
public MapBuilderSidePanel(Dimension size){
    tileSetPanel = new MapBuilderTileSetPanel(new Dimension(tileSetPanelWidth,tileSetPanelHeight),6,17, tileSetPanelHeight, tileSetPanelWidth, this);
    scrollPane = new JScrollPane(tileSetPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    //add(tileSet, BorderLayout.NORTH);
    add(scrollPane, BorderLayout.CENTER);

    setPreferredSize(size);
    setBorder(new BevelBorder(BevelBorder.LOWERED)); 
    setVisible(true);
}

// Remember override paintComponent(); NOT paint().
public void paintComponent(Graphics g){
}

public void loadTileSet(String filename, int r, int c){
    //tileSet.loadTileSet(filename, r, c);
}

public void refresh(){
    tileSetPanel.repaint(); 
}

}
/***************************************************************************/
class MapBuilderTileSetPanel extends JPanel{
private MapBuilderSidePanel myParent;
MapBuilderTileSetPanel(Dimension size, int columns, int rows, int parentPanelHeight, int parentPanelWidth, MapBuilderSidePanel mp){
    this.myParent = mp;
    setBackground(Color.BLACK);
    setPreferredSize(size);
    setSize(1000,1000);
    setBorder(new BevelBorder(BevelBorder.LOWERED));
}

public void loadTileSet(String filename, int c, int r){
}

public void paintComponent(Graphics g){
    g.setColor(new Color(0x00550055));
    g.drawRect(25, 25, 25, 25);
    g.setColor(new Color(0x0000FF00));
    g.drawRect(30, 30, 200, 500);
}

public MapBuilderTileSetPanel returnThis() {
    return this;
}


}
yoonsi
  • 754
  • 3
  • 10
  • 23

2 Answers2

2

Your scrollPane have no preffered size, so it is scaling to inner component.

Add:

scrollPane.setPreferredSize( new Dimension(200, 200));

Also as mKorbel pointed, call upper class method when you ovveride it.

Piotr Müller
  • 5,323
  • 5
  • 55
  • 82
2

You may be looking for JScrollNavigator, examined here.

image

Notes on your SSCCE:

  • Swing GUI objects should be constructed and manipulated only on the event dispatch thread.

  • "Swing programs should override paintComponent() instead of overriding paint()."—Painting in AWT and Swing: The Paint Methods.

  • Don't use setPreferredSize() when you really mean to override getPreferredSize(), as discussed here.

  • The scrollbar will appear automatically whenever the scrollpane is smaller than the enclosed component, as noted here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thank you for that. I will definitely look into the notes you provided, and I really appreciate the useful links. I'm a little scared of threads, thus why I've avoided the event dispatch thread but I will make a point to learn and do it properly. – yoonsi Oct 09 '13 at 03:03
  • I tend to dwell on theoretical reasons, but here's the practical [reason](http://stackoverflow.com/a/19167194/230513). – trashgod Oct 09 '13 at 08:39