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.


>> REGISTER NOW TO START POSTING


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
    Posts
    11
    Thanks
    3
    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.setTitle("DApplet");
    		this.setBackground(Color.LIGHT_GRAY);
    		this.getContentPane().add(new DApplet(true), BorderLayout.CENTER);
    		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    	}
     
    	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() {
    			this(false);
    		}
     
    		DApplet(boolean app) {
    			this.isDeelVanApplicatie = app;
    			if (app) {
    				this.init();
    			}
    		}
     
    		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
    			playfield.setViewport(mainScrollFrame.getViewport());
    			// Add the scrollpane
    			this.add(mainScrollFrame, BorderLayout.CENTER);
     
     
    			// Extra's to be deleted when it works
    			mainScrollFrame.setHorizontalScrollBarPolicy(mainScrollFrame.HORIZONTAL_SCROLLBAR_ALWAYS);
    		}
     
     
    		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?");
     
    			this.add(label);
    		}
     
    		public void setViewport(JViewport vp) {
    			this.viewport = vp;		
    		}
     
    		@Override
    		public void paintComponent(Graphics g) {
    			super.paintComponent(g);
    			update(g);
    		}
     
    		public void update(Graphics g) {
    			drawGrid(g);
    		}
     
    		private void drawGrid(Graphics g) {
    			g.setColor(Color.BLACK);
    			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
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

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

    This thread has been cross posted here:

    http://www.java-forums.org/awt-swing/65314-jscrollpane-scrollbar-not-working-my-grid.html

    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
    Posts
    11
    Thanks
    3
    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
    Posts
    2
    Thanks
    0
    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(){
     
    		super("Mechachess");
    		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));
    		window.add(scroll,BorderLayout.CENTER);
            }

    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