Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By Fubarable

Thread: ScrollBar

  1. #1
    light-blue is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default ScrollBar

    hi
    I really need your help :(

    I have a JFrame : fr
    and a JPanle : this

    fr size is 600*800 and is empty. no background , no object , ...
    the Jpanel size is for example 1400*1400 ( bigger than jframe)
    so I want to add scroll !

    I use this code but it dosent help me :(
    Java Code:
    this.setSize(1400, 1400);
    		this.setLocation(50, 50);
    		this.setVisible(true);
    
    		JScrollPane jsp = new JScrollPane(this);
    		jsp.setPreferredSize(new Dimension(1300, 1300));
    
    		 
    		jsp.setAutoscrolls(true);
    		jsp.setVisible(true);
    		jsp.setLayout(null);
    		jsp.setLocation(40, 40);
    		jsp.setSize(1400, 1400);
    		 
    		  
    		jsp.add(this);
    
    		fr.add(jsp);
    		fr.repaint();
    I really need scroll
    please help me
    Thanks in advance :)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: ScrollBar

    Don't set the JScrollPane's layout, and for gosh's sake don't set it to null!

    Also, don't add the JPanel to the JScrollPane itself (which replaces the JScrollPane's viewport -- and you need that) but rather to its viewport via the JScrollPane method setViewPortView(myJPanel). Either that or pass the JPanel into the JScrollPane in its constructor which does the same thing.

    Also, don't set sizes of anything. If you must, set the JScrollPane's preferredSize and the JPanel that is added to it. Be sure to call pack() on your JFrame.
    light-blue likes this.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: ScrollBar

    Moved from Java 2D

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    light-blue is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default Re: ScrollBar

    sorry for posting in a wrong place db .

    Thank you fubarable
    I use some of your advices but they don't help me
    could you please run this code and check it

    Java Code:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    
    public class Tst {
    
    	public static void main(String[] args) {
    		MyFrame2 mainFrame = new MyFrame2();
    
    	}
    
    }
    
    class MyFrame2 extends JFrame {
    
    	private static final long serialVersionUID = 1L;
    
    	public int WIDTH = 600;
    	public int HEIGHT = 800;
    	public MyLines2 ml;
    
    	MyFrame2() {
    
    		this.setVisible(true);
    		this.setSize(this.HEIGHT, this.WIDTH);
    		this.setBackground(Color.gray);
    		this.setTitle("Graph");
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setLayout(null);
    		this.setResizable(false);
    		ml = new MyLines2(this);
    	}
    
    }
    
    class MyLines2 extends JPanel {
    
    	private static final long serialVersionUID = 1L;
    	private MyFrame2 fr;
    	private int WIDTH, HEIGHT;
    
    	MyLines2(MyFrame2 fr) {
    
    		this.fr = fr;
    		this.setLayout(null);
    		WIDTH = 1800;
    		HEIGHT = fr.HEIGHT - 200;
    		this.setSize(HEIGHT, WIDTH);
    		this.setLocation(50, 50);
    		this.setVisible(true);
    
    		////////////// START JSP //////////////////// 
    		JScrollPane jsp = new JScrollPane(this,
    				JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
    				JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    
    		jsp.setPreferredSize(new Dimension(600, 400));
    		jsp.setViewportView(this);
    		fr.pack();
    		fr.add(jsp);
    
    		//////////END JSP ///////////////////////
    		fr.repaint();
    	}
    
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		if (Graph.x == 0)
    			g.drawLine(0, 0, 0, WIDTH);
    		for (int i = 0; i < 3; i++)
    			g.drawLine(30 + i, 0, 30 + i, WIDTH);
    
    		for (int i = 0; i < 50; i++)
    			g.drawLine(30 + 30 * i, 0, 33 + 30 * i, WIDTH);
    
    		for (int i = 0; i < 32; i++)
    			g.drawString("R" + i, 5, 30 * i + 15);
    	}
    
    }

    I marked the Scroll part with comment. when I don't use scroll and write just " fr.add(this) " everything id OK but my panel is larger than usual size so i wanted to use scroll but I can't:(
    I'm really sorry for putting my code here and asking you to check it, but I really need it. whole my program depends on this graphical part

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: ScrollBar

    1. you only need to add your component to the JScrollPane once, not twice. So add it in the constructor or via setViewportView, but not both.
    2. Add all components to the JFrame before calling pack().
    3. You may need to set your JScrollPane's preferredSize
    4. Consider overriding MyLines2 getPreferredSize() method to return what size you want it to be.

  6. #6
    light-blue is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default Re: ScrollBar

    Solved
    Thank you

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Problem with the Scrollbar
    By mike28 in forum Java Applets
    Replies: 0
    Last Post: 10-30-2011, 09:59 PM
  2. How to update the scrollbar?
    By Martino in forum Java 2D
    Replies: 3
    Last Post: 06-08-2011, 06:09 PM
  3. Custom scrollbar possible?
    By FatWhale in forum AWT / Swing
    Replies: 2
    Last Post: 03-19-2011, 02:37 PM
  4. ScrollBar in a JList
    By lupo in forum AWT / Swing
    Replies: 2
    Last Post: 05-20-2010, 10:42 PM
  5. Using java.awt.Scrollbar
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-03-2008, 09:19 AM

Posting Permissions

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