Forum Stats

  • 3,767,865 Users
  • 2,252,726 Discussions
  • 7,874,367 Comments

Discussions

JScrollPane with scroll bar on the left

843804
843804 Member Posts: 45,751
edited Nov 9, 2006 2:11AM in Swing
Can anyone point me to information on how to define a JScrollPane with the vertical scroll bar on the left?
«1

Comments

  • 843804
    843804 Member Posts: 45,751
    Don't have time to test this and see if it works properly but it did in a simple cae, but this is worth giving a try.
    		JScrollPane jsp = new JScrollPane(yourComp);
    		jsp.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    		jsp.getViewport().setViewPosition(new Point(0, 0));
    In the scroll pane constructor it has this code:
    	if (!this.getComponentOrientation().isLeftToRight()) {
    	    viewport.setViewPosition(new Point(Integer.MAX_VALUE, 0));
    	}
    So you need the line resetting the view position or you end up scrolled all the way to the right.

    Hope this helps,
    Alex.
  • 843804
    843804 Member Posts: 45,751
    Just been looking at the scrollpane code as far as I can see if you want to do this properly you just need to subclass ScrollPaneLayout and change that slightly.

    Alex.
  • 843804
    843804 Member Posts: 45,751
    Thanks Alex, I will look at your suggestions.
  • 843804
    843804 Member Posts: 45,751
    Alex,

    Your fist suggestion does not seem to have any effect and your second suggestion means I will have to put in more effort than I have time for at the moment.

    I shall add this feature to my 'nice to have' list.

    Thanks again.
  • 843804
    843804 Member Posts: 45,751
    Strange that the orientation thing didn't work but anyway in the scroll pane layout you just need to move a few components bounds and it seems to work perfectly:
    public class LeftyScrollPaneLayout extends ScrollPaneLayout {
    
    	public void layoutContainer(Container parent) {
    		super.layoutContainer(parent);
    		
    		//Get the bounds of the viewport
    		Rectangle availR = viewport.getBounds();
    		
    		//Expected width is the width of availR in the super method before
    		//it is adjusted for the vertical scroll bar
    		Insets insets = parent.getInsets();
    		int expectedWidth = (((JScrollPane)parent).getBounds().width 
    				- insets.left - insets.right);
    		
    		//If availR.width == expected width then no adjustement has been made
    		//for the vertical scroll bar so no vsb is showing so we dont need
    		//to do anything unless availR.width != expectedWidth
    		if (availR.width != expectedWidth) {
    			//Get the bounds of vsb and move its x value to 1 (do not use 0,
    			//1 is the value used in the super method when placing scroll bars
    			//at the edge)
    			Rectangle vsbbounds = vsb.getBounds();
    			vsbbounds.x = 1;
    			vsb.setBounds(vsbbounds);
    			
    			//Test viewport is not null if not move it to the right to make
    			//space for the vsb and to take over the space where the vsb
    			//used to be.
    			if (viewport != null) {
    				availR.x = availR.x + vsbbounds.width;
    				viewport.setBounds(availR);	
    			}
    			
    			//The horizontal scroll bar also needs moving to the right
    			//otherwise there is no empty square between the scroll bars
    			//adjacent ends but there is one where vsb used to be
    			Rectangle hsbbounds = hsb.getBounds();
    			hsbbounds.x = hsbbounds.x + vsbbounds.width;
    			hsb.setBounds(hsbbounds);
    		}		 
    	}
    }
    Hope this helps you,

    Alex.
  • 843804
    843804 Member Posts: 45,751
    Thanks Alex, I was just starting to do what you have posted!

    What you have provided is a good starting point but I think I will have to shift all the components. In effect I have to move the components in the right hand column to the very left and shift all the other components right by the width of the vertical scroll bar.

    I might get time to try this tomorow. I will post any positive result in case others need this feature.
  • camickr
    camickr Member Posts: 24,931
    scrollPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

    Works fine for me. If you are having problems I suggest you create a 5 line demo program to show use exactly what you are doing.
  • 843804
    843804 Member Posts: 45,751
    scrollPane.setComponentOrientation(ComponentOrientatio
    n.RIGHT_TO_LEFT);

    Works fine for me.
    Works find for me now! Thanks' camickr'.
  • 843804
    843804 Member Posts: 45,751
    Works find for me now! Thanks' camickr'.
    Not entirely true! Works OK on JDK1.5 but not on 1.2.2 which is the target minimum platform! I haven't tried the intermediate Java versions.

    That will teach me to be lazy!
  • camickr
    camickr Member Posts: 24,931
    Works OK on JDK1.5 but not on 1.2.2
    Before I found out about the setComponentOrientation(..) method I thought I used the following:

    scrollPane.setRowHeaderView( scrollPane.getVerticalScrollBar() );

    It doesn't work on JDK1.5, but maybe it will work on older versions. If not I guess I have a bad memory

    Actually, I just looked at some of my older posts and I ended up doing code like the following:

    JPanel panel = new JPanel( new BorderLayout() );
    panel.add( scrollPane );
    panel.add( scrollPane.getVerticalScrollBar(), BorderLayout.WEST );
    getContentPane().add( panel );

    Works reasonably well, except the scrollbar never becomes disabled or hidden.
This discussion has been closed.