Results 1 to 11 of 11
Like Tree2Likes
  • 1 Post By Fubarable
  • 1 Post By Fubarable

Thread: create scroll bars inside JScrollPane

  1. #1
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Question create scroll bars inside JScrollPane

    my (J)Panel consists of the (J)ScrollPane
    my (J)ScrollPane consists of a (J)Panel

    Java Code:
    public classname extends JPanel
    {
       public void paintComponent(Graphics g)
        {
          //my panel with a graph which really really needs a scroll bar to view the full graph
        }
       public static void main(String[] args)
          {
               JScrollPane scrollpane=new JScrollPane();
               scrollpane.setViewPortView(classname);  //my panel is inside the scrollpane
               JFrame frame = new JFrame("title");
               frame.add(scrollpane);                          //my scrollpane is inside the frame
               frame.setVisible(true);
          }
    }
    the codings ran, and the (J)Panel was inside the (J)ScrollPane which was inside thje (J)Frame,
    and there were no scrollbars. hence it showed only a beginning part of the image, and the rest was hidden to the right side of the monitor.
    How do we add scrollbars?

    thx
    dhilip

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

    Default Re: create scroll bars inside JScrollPane

    The scrollbars will automatically appear if the component held by the view port is larger than the viewport. For more specific help, you'll need to post some code.

  3. #3
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: create scroll bars inside JScrollPane

    Quote Originally Posted by Fubarable View Post
    The scrollbars will automatically appear if the component held by the view port is larger than the viewport. For more specific help, you'll need to post some code.
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.event.MouseWheelListener;
    import java.awt.font.FontRenderContext;
    import java.awt.font.LineMetrics;
    import java.awt.geom.Line2D;
    import java.io.*;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollBar;
    import javax.swing.JScrollPane;
    
    
    
    public class ReadPlat extends JPanel
    {
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 4157203125948260492L;
    	final int PAD = 20;
    	double[] xArray ;
    	double[] yArray ;
    	int Size;
    	
    //////////////////////////////////////////////////////////////////////////////////////////
    	public void paintComponent(Graphics g)
    	{
    	    
    
    		Graphics2D g2 = (Graphics2D)g;
    		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    		
    		int w = getWidth();
            int h = getHeight();
            // Draw ordinate.
            g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD));
            
            // Draw abscissa.
            g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD));
            
            // Draw labels.
            Font font = g2.getFont();
            FontRenderContext frc = g2.getFontRenderContext();
            LineMetrics lm = font.getLineMetrics("0", frc);
            float sh = lm.getAscent() + lm.getDescent();
            // Ordinate label.
            String s = "amplitude";
            float sy = PAD + ((h - 2*PAD) - s.length()*sh)/2 + lm.getAscent();
            for(int i = 0; i < s.length(); i++) {
                String letter = String.valueOf(s.charAt(i));
                float sw = (float)font.getStringBounds(letter, frc).getWidth();
                float sx = (PAD - sw)/2;
                g2.drawString(letter, sx, sy);
                sy += sh;
            }
            s = "time";
            sy = h - PAD + (PAD - sh)/2 + lm.getAscent();
            float sw = (float)font.getStringBounds(s, frc).getWidth();
            float sx = (w - sw)/2;
            g2.drawString(s, sx, sy);
            
            double xInc = (double)(w - 2*PAD)/(this.yArray.length-1);
            double scale = (double)(h - 2*PAD)/getMax();
            
            for(int i=PAD;i<w-PAD;i=i+50)
            {
            	g2.drawString(/*Integer.toString*/Double.toString(/*i-20*/this.xArray[i]), i, h-PAD+10);
            }
                    
            g2.setPaint(Color.green.darker());
            for(int i = 0; i < this.yArray.length-1; i++) {
                double x1 = PAD + i/**xInc*/;
                double y1 = h - PAD - scale*((this.yArray[i]));
                double x2 = PAD + (i+1)/**xInc*/;
                double y2 = h - PAD - scale*this.yArray[i+1];
                g2.draw(new Line2D.Double(x1, 2*y1, x2, 2*y2));
                
            }
            
            // Mark data points.
            g2.setPaint(Color.red);
         
            
    
    	}
    //////////////////////////////////////////////////////////////////////////////////////////
    	
    	private double getMax() {
    	       double max = -Integer.MAX_VALUE;
    	        for(int i = 0; i < this.yArray.length; i++) {
    	            if(this.yArray[i] > max)
    	                max = this.yArray[i];
    	        }
    	        return max;
    	    }
    	
    	public ReadPlat(int size)
    	{
    		this.Size=size;
    		xArray= new double[this.Size];
    		yArray= new double[this.Size];
    	}
    	
    	public void setData(int i,double x,double y)
    	{
    		if(i>this.Size)
    			return;
    		xArray[i]=x;
    		yArray[i]=y;
    	}
    	
    	public static void main(String[] args) throws Exception
    	{   
    		ReadPlat ReadPlatInstance;
    		String filename;
    		FileReader file_in;
    		BufferedReader in;	
    		String input_line;
    		String[]input_split;
    		int TotalLineCount=0;
    		
    		filename = args[0];
    		file_in = new FileReader(filename);
    	    in 		= new BufferedReader(file_in);
    	    
    	    input_line ="Starting";
    	    
    	    
    	    //Reading total lines 
    	    while((input_line=in.readLine())!=null)
    	    {	
    	    	//input_line =in.readLine();
    	    	TotalLineCount++;
    	    }
    	    System.out.println(TotalLineCount);
    	    ReadPlatInstance = new  ReadPlat(TotalLineCount);
    	    
    	    //Closing the File and reopening
    	    TotalLineCount=0;
    	    input_line ="ReStarting";
    	    in.close();
    	    file_in.close();
    	    file_in = new FileReader(filename);
    	    in 		= new BufferedReader(file_in);	    
    	    while((input_line=in.readLine())!=null)
    	    {	
    	    	 System.out.println("---------------");
    	    	 System.out.println("Line No:"+TotalLineCount);
    	    	
    	    	
    	    	
    	    	input_line=input_line.replaceAll("\"","");
    	    	input_split = input_line.split(" ");    	   	
    	    	
    	    	 System.out.println("Input Line:"+input_line);
    	    	 System.out.print(input_split.length);
    	    	 System.out.print("x"+input_split[0]);
    	    	 System.out.print("y"+input_split[2]);
    	    	ReadPlatInstance.setData(TotalLineCount, Double.valueOf(input_split[0].trim()).doubleValue(),Double.valueOf(input_split[2].trim()).doubleValue()   )	;    	
    	    	System.out.println("\n"+"instant x "+ReadPlatInstance.xArray[TotalLineCount] + "\t instant y" + ReadPlatInstance.yArray[TotalLineCount]);
    	    	TotalLineCount++;
    	    	
    	    }	
    	    
    	    
    	  
    	    
    	      
    	    JFrame f=new JFrame("ReadPlatInstance");//////
    	    JScrollPane spp=new JScrollPane();
    	    spp.setViewportView(ReadPlatInstance);
    	    spp.setVisible(true);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(spp);
            f.pack();
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
    	    in.close();
    	    file_in.close();
            
    	    
    	    
    	    
    	    
    	    
    	}
    	
    }
    this is my code,
    this is my input file added to the argument for my class (ReadPlat) input.txt
    the original file has 63375 lines, but i couldn't upload it in this forum, hence, I've deleted so many lines and this file has only 849 lines, and scrollbar doesn't appear yet.

    thx
    dhilip
    Attached Files Attached Files
    Last edited by noobplus; 03-21-2012 at 02:12 PM.

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

    Default Re: create scroll bars inside JScrollPane

    You need to give your ReadPlat class an override for the getPreferredSize method. Then you also need to set the preferredSize of your JScrollPane's viewport (making sure that the two heights match. Do not set the size of the JFrame but rather let pack() take care of its size.

    Also the logic in your paintComponent is wrong as you change how many data points are displayed depending on the size of the JPanel. Don't do that.
    noobplus likes this.

  5. #5
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: create scroll bars inside JScrollPane

    Quote Originally Posted by Fubarable View Post
    You need to give your ReadPlat class an override for the getPreferredSize method.
    thx, I've added the line
    Java Code:
    ReadPlatInstance.setPreferredSize(new Dimension(TotalLineCount,718));//TotalLineCount is the size of the array of y-axis values
    (after initializing the JFrame),
    Quote Originally Posted by Fubarable View Post
    Then you also need to set the preferredSize of your JScrollPane's viewport (making sure that the two heights match. Do not set the size of the JFrame but rather let pack() take care of its size.
    Java Code:
    spp.setPreferredSize(new Dimension(1357,718));
    and yay the scroll bar has appeared!!! and the graph was alright.
    create scroll bars inside JScrollPane-grph1.jpg
    but when I scroll the scrollbar, the incoming graph gets drawn below, and appears as kind of the lines are overlapped,
    create scroll bars inside JScrollPane-grph2.jpg
    Quote Originally Posted by Fubarable View Post
    Also the logic in your paintComponent is wrong as you change how many data points are displayed depending on the size of the JPanel. Don't do that.
    that's what I can't get in mind.. I've removed "xInc" so it got dependency-free(depending on the size of the panel), and got that error while scrolling

    regards
    dhilip
    Last edited by noobplus; 03-22-2012 at 02:41 PM.

  6. #6
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: create scroll bars inside JScrollPane

    I'll go with this question..
    where do we create the actionListener and which listener? because I need the scrollPane get refreshed or repainted every scrolling action

    regards
    dhilip

  7. #7
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: create scroll bars inside JScrollPane

    hi again,

    is it possible to get rid of the error by using repaint() ? I tried that in vain anyway, but
    is there any other way?
    or listener is needed?

    thanks
    dhilip

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

    Default Re: create scroll bars inside JScrollPane

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

  9. #9
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: create scroll bars inside JScrollPane

    Quote Originally Posted by DarrylBurke View Post
    Cross posted

    db
    Hi,
    they asked me to add "super.paintComponent();" at the beginning of the overriding. and it works good , and I thought there are different moderators there, glad to meet you on that forum, anyways

    regards
    dhilip

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

    Default Re: create scroll bars inside JScrollPane

    Quote Originally Posted by noobplus View Post
    ... and I thought there are different moderators there,
    What does this have to do with anything?
    noobplus likes this.

  11. #11
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: create scroll bars inside JScrollPane

    Quote Originally Posted by Fubarable View Post
    What does this have to do with anything?
    i mean, different mods, but this is my fav forums

    regards
    dhilip

Similar Threads

  1. Shell with scroll bars
    By M54 in forum SWT / JFace
    Replies: 0
    Last Post: 10-27-2011, 11:17 AM
  2. Replies: 1
    Last Post: 04-05-2011, 12:12 PM
  3. Moving Scroll in JScrollPane
    By ayesh85 in forum AWT / Swing
    Replies: 0
    Last Post: 03-02-2011, 08:11 AM
  4. Help with Scroll Bars
    By Abion47 in forum AWT / Swing
    Replies: 5
    Last Post: 03-02-2011, 04:44 AM
  5. jscrollpane scroll issues
    By kumar_gemi in forum AWT / Swing
    Replies: 11
    Last Post: 09-30-2009, 08:33 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
  •