Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Resize JScrollPane dynamically

843806Nov 6 2007 — edited Nov 6 2007
Hello,

Hello, I'm trilexx, a student of computer science and new to the forums. I'm having some difficulties with JScrollPane. I've searched the forums but could not find a solution or hint to solve my problem. Let me explain my problem to you:

I have a CalendarScrollPane class extending JScrollPane. I've added a JPanel to the ScrollPane and that serves as a root container for other JPanels. What I wanted to do know is to resize the JScrollPane, dependent on how many JPanels are added to Container Panel. So, here is the structure again:

JScrollPane -- contains -- JPanel: ContainerPanel -- contains -- AppointmentPanels (added dynamically during runtime)

When there is just one AppointmentPanel in the ContainerPanel the JScrollPane should have a fixed size, when there are 2-4, the JScrollPane should have a dynamic size dependend on how many AppointmentPanels have been added. And when there are more than 4 AppointmentPanels the ScrollPane should have a fixed Size and allow scrolling over the Container Panel using the ScrollBars.

So I need not only to adjust the ScrollBars, I also need to adjust the size of the ScrollPane itself. I have already created a separate JPanel class, extending JPanel, implementing Scrollable and overwriting Dimension getPreferredScrollableViewportSize(), but this method only seems to called the first time when the ViewPort is set. Well, I'm clueless. Here is some code, to make it easier to follow:
package Calendar;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.LinkedList;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class CalendarScrollPane extends JScrollPane{
	
	private JPanel componentsArea;
	private int number=0;
	private Dimension size;
	
	public CalendarScrollPane(){
		super();
		componentsArea=new IcalComponentsContainer();
		componentsArea.setLayout(new GridLayout(0,1));
		
		this.add(componentsArea);
		this.setViewportView(componentsArea);
		this.addIcalComponent();
		
		this.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		this.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
	}//CalendarScrollPane
	
	private void addIcalComponent(){
		componentsArea.add(new AppointmentPanel());
		componentsArea.revalidate();
	}//addComponent
	
	private void addIcalComponent(IcalComponent c){
		componentsArea.add(new AppointmentPanel(c));
	}//addComponent
	
	/*
	 * TODO: just to remember: using setPreferredSize will not affect the Size of the Scrollpane, but only the behaviour of the Scrollbars
	 */
	
	public void showComponents(LinkedList<IcalComponent> day){
		this.clear();
		
		if(day.isEmpty()) this.addIcalComponent();
		else {
			for(IcalComponent comp:day){
				this.addIcalComponent(comp);
			}//for
			componentsArea.revalidate();
			this.repaint();
		}//else
	}//listComponents
	
	private void clear(){
		componentsArea.removeAll();
	}//clear
package Calendar;

import java.awt.Dimension;
import java.awt.Rectangle;

import javax.swing.JPanel;
import javax.swing.Scrollable;

public class IcalComponentsContainer extends JPanel implements Scrollable{
	
	public Dimension getPreferredScrollableViewportSize(){
		if(this.getComponentCount()==1) return new Dimension(this.getParent().getWidth(),100);
		else if(this.getComponentCount()>1 && this.getComponentCount()<3) return new Dimension(this.getParent().getWidth(),200);
		else return new Dimension(this.getParent().getWidth(),200);
	}

	public int getScrollableBlockIncrement(Rectangle visibleRect,
			int orientation, int direction) {
		// TODO Auto-generated method stub
		return 0;
	}

	public boolean getScrollableTracksViewportHeight() {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean getScrollableTracksViewportWidth() {
		// TODO Auto-generated method stub
		return false;
	}

	public int getScrollableUnitIncrement(Rectangle visibleRect,
			int orientation, int direction) {
		// TODO Auto-generated method stub
		return 0;
	}

}
I thought revalidating the Container Panel would make the ScrollPane update its Viewport settings and rerun Dimension getPreferredScrollableViewportSize(). But it doesn't work. I don't know what I'm missing. I'm thankful for every hint.

greetings
trilexx
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 4 2007
Added on Nov 6 2007
1 comment
291 views