Results 1 to 2 of 2
  1. #1
    Vinvar is offline Member
    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Default JScrollPane (/Scrollbar) not working on my grid

    I have the following code:
    Java 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 < 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
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,693
    Rep Power
    8

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

    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. ScrollBar
    By light-blue in forum AWT / Swing
    Replies: 6
    Last Post: 05-18-2012, 10:13 PM
  2. Open Grid Scheduler/Grid Engine
    By java software in forum Java Software
    Replies: 0
    Last Post: 03-27-2012, 07:00 PM
  3. How to update the scrollbar?
    By Martino in forum Java 2D
    Replies: 3
    Last Post: 06-08-2011, 06:09 PM
  4. ScrollPane Scrollbar's Not Working in Applet
    By rgrant222 in forum New To Java
    Replies: 2
    Last Post: 08-03-2010, 05:03 PM
  5. Replies: 4
    Last Post: 01-21-2010, 05:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •