Hi guys i have a problem with JScrollPane,
Firstly i have 2 custom JPanel's i wanted to add them to a JSplitPane
so i added them to a JScrollPane and then those JSplitPane but the problem is if i drag the divider in splitpane the panel doesnot scroll :'(
Here is how i added the panels(LeftPanel and RightPAnel are my custom JPanels)
and (leftpane and rightpane are JScrollPane objects) and splitpane is JSplitPane object

leftpane=new JScrollPane()
	JViewport viewport = leftpane.getViewport();
        viewport.add(new LeftPanel(us));
        viewport.setPreferredSize(new Dimension(500,350));

rightpanel is also same

splitpane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,leftpane,rightpane);
	splitpane.setOneTouchExpandable(true);
	splitpane.setDividerLocation(700);
	leftpane.setMinimumSize(new Dimension(500,350));
	rightpane.setMinimumSize(new Dimension(250,350));

then i added the splitpane to contentpane and done setBounds for split pane
Here is how the custom panel looks like

class RightPanel extends JPanel
{
	CalendarPanel cp;
	JLabel contentlabel;
	RightPanel()
	{
		super();
		setLayout(null);
		cp=new CalendarPanel();
		contentlabel=new JLabel("Information about clicked date goes here");
		add(cp);
		add(contentlabel);
		cp.setBounds(10,10,350,200);
		contentlabel.setBounds(10,240,350,250);
	}
}

both left and right panel's are alike so help with this scrolling hav i done wrong somewhere :?: if there is wrong tell me i always like to get educated :)
But the interesting thing is if i add my panel like this it is scrollable :-O

leftpanel=new JPanel();
       leftpanel.add(component1);
       .//remaining components
       .//same with rightpanel
       leftpane=new JScrolPane(leftpanel,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
       rightpane=new JScrollPane(rightpanel,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
      splitpane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,leftpane,rightpane);

than you in advance
-Tizon

Can you make a SMALL complete program that compiles and executes and demonstrates your problem?

Can you make a SMALL complete program that compiles and executes and demonstrates your problem?

here u go i wrote a sample prog and it's successfully compiled and executed hope this demonstrates my prob i just want scroll bars when i drag the divider if panel full panel is not in view

Attachments
import java.awt.*;
import javax.swing.*;

class LeftPanel extends JPanel
{
	JLabel contentlabel;
	LeftPanel()
	{
		super();
		setLayout(null);
		//creating and adding component
		contentlabel=new JLabel("Information about this Leftpanel goes here i hav added only one component");
		add(contentlabel);
		contentlabel.setBounds(10,10,350,200);
	}
}
import java.awt.*;
import javax.swing.*;

class RightPanel extends JPanel
{
	JLabel contentlabel;
	RightPanel()
	{
		super();
		setLayout(null);
		//creating and adding component
		contentlabel=new JLabel("Information about this rightpanel goes here i hav added only one component");
		add(contentlabel);
		contentlabel.setBounds(10,10,350,200);
	}
}
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class SampleMain extends JFrame
{
	Container ct;
	Dimension scrsize;
	JScrollPane leftpane,rightpane;
	JSplitPane splitpane;
	SampleMain(String us)
	{
		super("PDA:MainPage");
		System.out.println("im in main page");
		ct=getContentPane();
		Toolkit toolkit=Toolkit.getDefaultToolkit(); //setting size of window and layout
		scrsize=toolkit.getScreenSize();
		setSize(scrsize.width,scrsize.height-25);	
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(null);
		
		//creating and adding custom panels to scrollpanes
		leftpane=new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		JViewport viewport = leftpane.getViewport();
		viewport.add(new LeftPanel());
		viewport.setPreferredSize(new Dimension(500,350));
		rightpane=new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		JViewport viewport1 = rightpane.getViewport();
		viewport1.add(new RightPanel());
		viewport1.setPreferredSize(new Dimension(400, 400));

		//creating splitpane and adding scrollpane's to splitpane
		splitpane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,leftpane,rightpane);
		splitpane.setOneTouchExpandable(true);
		splitpane.setDividerLocation(700);
		leftpane.setMinimumSize(new Dimension(500,350));	//Provide minimum sizers for the two components in the split pane
		rightpane.setMinimumSize(new Dimension(250,350));
		ct.add(splitpane);
		splitpane.setBounds(10,40,1000,650);
		setVisible(true);
	}

	public static void main(String args[])
	{
		new SampleMain("test");
		
	}

}

I was able to get it to add scroll bars by removing the absolute layout stuff:

class RightPanel extends JPanel
{
	JLabel contentlabel;
	RightPanel()
	{
		super();
//		setLayout(null);
		//creating and adding component
		contentlabel=new JLabel("Information about this rightpanel goes here i hav added only one component");
		add(contentlabel);
//		contentlabel.setBounds(10,10,350,200);
	}
}

I was able to get it to add scroll bars by removing the absolute layout stuff:

class RightPanel extends JPanel
{
	JLabel contentlabel;
	RightPanel()
	{
		super();
//		setLayout(null);
		//creating and adding component
		contentlabel=new JLabel("Information about this rightpanel goes here i hav added only one component");
		add(contentlabel);
//		contentlabel.setBounds(10,10,350,200);
	}
}

Thanks for your help removing that setLayout(null) got displayed my scrollbars :) but then my panel doesn't obey my setBounds() in the panel :( have any idea how to fix that.
thanks again
-Tizon

Use a layout manager to control the positioning.

Or if you can find what a layout manager provides that the scroll pane needs/uses and provide it yourself, then you could use a null layout.

Edited 6 Years Ago by NormR1: n/a

Use a layout manager to control the positioning.

Or if you can find what a layout manager provides that the scroll pane needs/uses and provide it yourself, then you could use a null layout.

ya that would solve i hav to use gridbaglayout for my project :yawn:
But i don't really understand why setting layout null would effect scrolling can u explain?
i.e why the scrollpane would use resources from a layout manager :?:

Edited 6 Years Ago by tizon: n/a

why setting layout null would effect scrolling

It could do something that the ScrollPane needs to have done to the component.
I don't know the internals, I only observed that having a layout manager allowed your code to work the way you wanted. To find out what is going on inside, override all of the methods that can be used by a layout manager and see when they are called and with what values. Compare that with what your code does.

Edited 6 Years Ago by NormR1: n/a

It could do something that the ScrollPane needs to have done to the component.
I don't know the internals, I only observed that having a layout manager allowed your code to work the way you wanted. To find out what is going on inside, override all of the methods that can be used by a layout manager and see when they are called and with what values. Compare that with what your code does.

k we shall leave that over ridding methods of layout manager later ;) for now i got it worked thanks for your help :*
i will work with that layout manger with my project and post the results soon thanks again

Edited 6 Years Ago by tizon: n/a

This article has been dead for over six months. Start a new discussion instead.