Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default Unable to set JScrollPane to scroll through the JPanle's contents

    Hi all,
    Ive created around 342 textboxes and i added them to the JPanel in the JFrame. Now i want to scroll through the JPanel to view all the text boxes.
    I had been browsing the net for the past few hours and tried all the possible ways but im unable to resolve this out. I know i am missing something , but unable to find it out
    Please help me with this.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    import java.awt.event.*;
    import java.awt.BorderLayout;
    import java.awt.Graphics;
    import java.awt.GridLayout;
    import java.awt.event.WindowAdapter;
    import javax.swing.JLabel;
    
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.io.IOException;
    import java.net.*;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class hari_ei_main extends JFrame implements MouseListener, MouseMotionListener {
    	
    	//hari_open_excel ho;
    	//Hari_excel he;
    	//hari_string_gen hg;
    	ReadDocFileFromJava rdf;
    	public static JFrame frame;
    	public static JTextField snme,ver,at1,dt1;
    	public static JLabel l1,sname,vers;
    	public static JButton sub1,crds,dtds;
    	public static JPanel panel,panel1;
    	public static JList jl;
    	public static JScrollBar jb1,jb2;
    	public static JScrollPane jp;
    	public static JCheckBox[] jr = new JCheckBox[600];
    	public static JTextField[] at = new JTextField[600];
    	public static String svc_name,svc_vers;
    	public void hari_ei_mainn()
    	{
    		
    		jb1 = new JScrollBar(JScrollBar.HORIZONTAL);
    		jb2 = new JScrollBar(JScrollBar.VERTICAL);
    		frame = new JFrame("EI_DS_Creator");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		GUI();
    		//frame.addMouseListener(this);
    		//panel.scrollRectToVisible(arg0)
    		//frame.setVisible(true);
    		//jp = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    		
    	//	jb = new JScrollBar();
    	//	jb.add(panel);
    		//jb
    		//jp.setPreferredSize(panel.getPreferredSize());
    	//	frame.add(jp);
    		
    		panel.setAutoscrolls(true);
    		panel1 = new JPanel();
    		panel.setBorderLayout(new BorderLayout());
    		jp = new JScrollPane(panel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    		panel1.add(jp,BorderLayout.CENTER);
    		
    		//boolean b =panel.getAutoscrolls();
    		//System.out.println("The bool is "+b);
    		//Container contentPane = frame.getContentPane();
    		//contentPane.add(jb1,BorderLayout.SOUTH);
    		//contentPane.add(jb2,BorderLayout.EAST);
    		//contentPane.add(panel,BorderLayout.CENTER);
    		
    		frame.add(panel1);
    		frame.setSize(20000, 20000);
    		frame.setVisible(true);
    		
    	}
    	
    	
    	public void mouseEntered(MouseEvent e) { }
    	public void mouseExited   ( MouseEvent e ){}
    	public void mousePressed  ( MouseEvent e ) {}
    	public void mouseDragged(MouseEvent e) {}
    	public void mouseMoved(MouseEvent e) { }
    	public void mouseReleased ( MouseEvent e ) {}
    	public void mouseClicked(MouseEvent e) { }
    	MouseMotionListener doScrollRectToVisible = new MouseMotionAdapter()
    	{
    		public void mouseDragged(MouseEvent e){
    			Rectangle r = new Rectangle(e.getX(),e.getY(),1,1);
    			((JPanel)e.getSource()).scrollRectToVisible(r);
    		}
    		
    	};
    	public void GUI()
    	{
    		
    		panel = new JPanel();
    		//panel1 = new JPanel();
    		panel.setLayout(null);
    		panel.setSize(15000,15000);
    		sname = new JLabel("Service name in the app. format:");
    		vers = new JLabel("version no:");
    		sname.setBounds(40,7,250,20);
    		vers.setBounds(500,7,200,20);
    		snme = new JTextField();
    		snme.setBounds(250,7,200,20);
    		ver = new JTextField();
    		ver.setBounds(600,7,50,20);
    		panel.add(sname);
    		panel.add(vers);
    		panel.add(ver);
    		panel.add(snme);
    		
    		//jp.add(panel);
    		//frame.add(jp);
    		
    		//jb = new JScrollBar(JScrollBar.VERTICAL);
    		//jb.add(panel);
    		//panel.add(jb);
    	//	jb.add(frame);
    		//panel.add(jb);
    		//frame.add(jb,BorderLayout.WEST);
    		
    	/*	panel.setPreferredSize(new Dimension(5000,5000));
    		JScrollPane jpp = new JScrollPane(panel);
    		frame.getContentPane().add(jpp);
    		frame.setPreferredSize(new Dimension(1000,1000));
    		frame.setVisible(true);*/
    		//panel.add(jp);
    		
    		//frame.add(jp);
    		//JScrollPane scroller = new JScrollPane(panel);
    		
    		
    		
    		at1 = new JTextField();
    		at1.setBounds(350,40,200,20);
    		panel.add(at1);
    		l1=new JLabel();
    		l1.setBounds(20,20,300,60);
    		l1.setText("Enter the path for the BS doc for fields retrival:");
    		sub1 = new JButton("Retrieve the fields - from txt");
    		sub1.setBounds(600,40,300,20);
    		jl = new JList();
    		dtds = new JButton("Retrieve the fields - from word");
    		dtds.setBounds(600,70,300,20);
    		panel.add(dtds);
    		panel.add(sub1);
    		int j=0,inc=0,jrinc=0,fll=0,inij=100;
    		
    	for(int k = 0;k<6;k++)
    	{
    		for(int i=0;i<3;i++)
    		{
    			for(int jj=0;jj<19;jj++)
    			{
    				jr[inc] = new JCheckBox();
    				jr[inc].setBounds(150+jrinc,inij+j,20,20);
    				
    				at[inc] = new JTextField();
    				at[inc].setBounds(10+jrinc,inij+j,100,20);
    				
    				
    				inc++;
    				j+=25;
    			}
    			fll = j+100;
    		
    			j=0;
    			jrinc+=190;
    		}
    		inij = fll+40;
    		jrinc=0;
    		j=0;
    		
    	}
    	
    
    		for(int i = 0;i<342;i++)
    		{
    			panel.add(jr[i]);
    			panel.add(at[i]);
    		}
    		
    		
    		
    		
    		crds = new JButton("Create DS");
        	crds.setBounds(600,100,150,20);
    		panel.add(l1);
    
    	
    	
    	sub1.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
            	panel.add(crds);
            	//ho = new hari_open_excel();
            	//ho.open_Excel(at1.getText());
            	
            	
            	
    }
    });
    	dtds.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
            	panel.add(crds);
            	//rdf = new ReadDocFileFromJava();
            	System.out.println("The value of at is "+at1.getText());
            	//rdf.readMyDocument(at1.getText());
            	
            	
    }
    });
    	
    	crds.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
            	//he = new Hari_excel();
            	//hg = new hari_string_gen();
            	//hg.stn_calc();
            	svc_name = snme.getText();
            	svc_vers = ver.getText();
            	String docname = snme.getText()+"_V"+ver.getText()+"_Datasource.xls";
            	String loc = "C:\\Documents and Settings\\Administrator\\Desktop\\";
            	String inp = loc+docname;
            	System.out.println("Input name is "+inp);
            	//he.setOutputFile(inp);
            	try{
            //	he.write(inp);
            	}
            	catch(Exception e){
            	System.out.println("the exception is "+e);
            	}
            	//ho.open_Excel(at1.getText());
            	
            	
    }
    });
    	
    	}
    	public static void main(String[] args){
    		System.out.println("Hi");
    		hari_ei_main har = new hari_ei_main();
    		har.hari_ei_mainn();
    	}
    }
    Thanks in advance
    Last edited by mhariharasudan; 09-27-2011 at 08:23 PM.

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

    Default Re: Unable to set JScrollPane to scroll through the JPanle's contents

    Some suggestions:
    • Study up on and learn to use layout managers in your GUI's. Avoid using the null layout manager unless absolutely necessary. It isn't necessary in your code above, and may in fact be getting in your way.
    • Don't use setSize(...) on any component in your GUI. Instead you will want to use preferredSize since this is what most layout managers and the JScrollPane's viewport will use to layout their components. Better still is to let the components and the layout managers set the size themselves.
    • It looks like you could simplify a lot of your code by using a JTable instead of trying to make a "pseudo-table" from JTextFields and checkboxes.
    • When asking a question like this, we would appreciate it if you pare your code down to the smallest possible size but still compiles and shows us your problem. Is there any need for you to make us wallow through MouseListener and ActionListener code when it isn't at all germane to your problem? If your code is smaller, more will take the time to read it, and your problem will likely be more evident. A good description of what you should post is here: SSCCE
    Last edited by Fubarable; 09-28-2011 at 02:48 AM.

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default Re: Unable to set JScrollPane to scroll through the JPanle's contents

    Don't use static variables for all you components.

  4. #4
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default Re: Unable to set JScrollPane to scroll through the JPanle's contents

    hi camickr

    Thanks a lot for your reply. Since i am new to java programming and didnt know anything about these, got messed up a little.
    As you said, i tried using BorderLayout instead of null layout and then everything worked fine for me.
    Regarding, giving only the snippet of the code. Yea ill do that next time .Sorry for this time.
    Thanks a lot

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default Re: Unable to set JScrollPane to scroll through the JPanle's contents

    I think you meant to thank Fubarable.

  6. #6
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default Re: Unable to set JScrollPane to scroll through the JPanle's contents

    Sorry didnt read the post properly and thought the camickr posted both the posts.
    Thanks to both of you for helping me through this :)

Similar Threads

  1. Replies: 1
    Last Post: 04-05-2011, 12:12 PM
  2. Moving Scroll in JScrollPane
    By ayesh85 in forum AWT / Swing
    Replies: 0
    Last Post: 03-02-2011, 08:11 AM
  3. Help with Scroll Bars
    By Abion47 in forum AWT / Swing
    Replies: 5
    Last Post: 03-02-2011, 04:44 AM
  4. Replies: 1
    Last Post: 02-23-2010, 09:48 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
  •