Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: JScrollPane (/Scrollbar) not working on my grid

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Thanked 1 Time in 1 Post

    Default JScrollPane (/Scrollbar) not working on my grid

    I have the following code:

    import java.awt.*;
    import javax.swing.*;
    /**@name DApplet version 0.9 
     * @made by Vinvar la Lece
    public class TestForum extends JFrame {
    	TestForum() {
    		this.setSize(1024, 768);
    		this.getContentPane().add(new DApplet(true), BorderLayout.CENTER);
    	public static void main(String[] args) { 
    		new DKApplic().setVisible(true);
    	private static final long serialVersionUID = 1;
    	 * Private class called in TestForum()
    	private class DApplet extends JApplet {
    		boolean isDeelVanApplicatie;
    		JScrollPane mainScrollFrame;
    		public DApplet() {
    		DApplet(boolean app) {
    			this.isDeelVanApplicatie = app;
    			if (app) {
    		public void init() {
    			this.setLayout(new BorderLayout());
    			// Creating and adding playfield
    			DPlayfield playfield = new DPlayfield();
    			// The playfield can be larger then the screen.
    			// Therefore, we need to add a  scrollable layer beneath the playfield on which we add the playfield
    			mainScrollFrame = new JScrollPane(playfield);
    			// Make sure that you can scroll within the playfield
    			// Add the scrollpane
    			this.add(mainScrollFrame, BorderLayout.CENTER);
    			// Extra's to be deleted when it works
    		private static final long serialVersionUID = 1L;
    	 * Private class called in DApplet() -> init()
    	private class DPlayfield extends JComponent {
    		JViewport viewport;
    		int boxSize = 50, numberOfRows = 20, numberOfColumns = 10;
    		int width = numberOfRows * boxSize - boxSize;
    		int height = numberOfColumns * boxSize - boxSize;
    		public DPlayfield() {
    			JLabel label = new JLabel();
    			label.setText("Welkom. Nog meer tekst om alles op te vullen en scrollpane te testen. Is dit genoeg?");
    		public void setViewport(JViewport vp) {
    			this.viewport = vp;		
    		public void paintComponent(Graphics g) {
    		public void update(Graphics g) {
    		private void drawGrid(Graphics g) {
    			for (int i = 0; i < numberOfRows; i++) {
    				g.drawLine(i * boxSize, 0, i * boxSize, height);
    			for (int i = 0; i < numberOfColumns; i++) {
    				g.drawLine(0, i * boxSize, width, i * boxSize);
    		private static final long serialVersionUID = 1L;

    I simplified the code so it is just my problem with the grid.

    I want that scrollbars appear if the screensize is smaller then the grid. Unfortunally, I can't get it working (and I was working on it this whole weekend). Is there anybody who has an idea?

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Washington, DC
    My Mood
    Thanked 636 Times in 540 Posts

    Default Re: JScrollPane (/Scrollbar) not working on my grid

    This thread has been cross posted here:


    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Thanked 1 Time in 1 Post

    Default Re: JScrollPane (/Scrollbar) not working on my grid

    I am sorry. Did not know that the forums where releated. I tried to delete the other thread. But I couldn't delete it (or this one). Feel free to delete one of them.

    Nonetheless, do you know the answer to my question?

  4. #4
    Junior Member spacetimeCounselor's Avatar
    Join Date
    Nov 2012
    Thanked 0 Times in 0 Posts

    Default Re: JScrollPane (/Scrollbar) not working on my grid

    I'm having... more or less the same problem- MchessBox is a fairly simple class that extends JComponent. It's larger than the screen, so I tried to put it inside a JScrollPane to give it scroll bars... which just doesn't seem to work at all- there's no scroll bars anywhere on the screen, it's just behaving as if I added the MchessBox directly instead of putting it inside the JScrollPane. I can't seem to get the scroll bars to show at all, here.
            private MchessBox box;
    	private JScrollPane scroll;
    	 * Default constructor that sets up an instance of the game.
    	public MchessFrame(){
    		Container window = getContentPane();
    		setLayout(new BorderLayout());
    		box = new MchessBox("files/maps/test1.txt"); //constructs the MchessBox, which is larger than 500x500
    		scroll = new JScrollPane(box);
                    window.setPreferredSize(new Dimension(500, 500));

    Am I doing something wrong with, like, sizes or something? Or is it a more particular problem with the way I'm setting up the JScrollPane?

Similar Threads

  1. Grid movement randomly not working
    By pajfilms in forum What's Wrong With My Code?
    Replies: 9
    Last Post: August 27th, 2011, 07:35 AM
  2. [SOLVED] Trying to add scrollbar to gui
    By illidari in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 13th, 2011, 10:38 PM
  3. Need help on print , scrollbar for my applet
    By Anandprasad in forum Member Introductions
    Replies: 2
    Last Post: May 12th, 2011, 09:00 AM
  4. How to add scrollbar to a JPanel with Graphics
    By Tanguo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2011, 08:53 PM
  5. problem in getting scrollbar in Jlist
    By sandeepben in forum AWT / Java Swing
    Replies: 1
    Last Post: March 27th, 2010, 03:51 AM