Results 1 to 6 of 6
  1. #1
    jajdoo is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default making a scroll pane widget

    i'm trying to make a widget to work like JScrollPane.
    my basic idea is this:
    the widget has two scroll bars, and a panel;
    the widget accepts another widget (the content) as the area to be scrolled.
    the content is now added as a component into the panel;
    when the bars are dragged, i change the location of the content.

    i'm rather close, but its not working properly; im having trouble describing the problem because i'm not quite sure i understand what's actually happening incorrectly;

    the code is very short, so i hope my error in logic will be easy to locate :

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.TextArea;
    import java.awt.event.AdjustmentEvent;
    import java.awt.event.AdjustmentListener;
    import java.awt.event.ComponentEvent;
    import java.awt.event.ComponentListener;
    
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollBar;
    
    public class MyScrollPane extends JComponent implements ComponentListener, AdjustmentListener
    {
    	private static final long serialVersionUID = -2808049373354755295L;
    	
    	private JScrollBar 	horizontalScroll 	= new JScrollBar( JScrollBar.HORIZONTAL );
    	private JScrollBar 	verticalScroll		= new JScrollBar( JScrollBar.VERTICAL );
    	private JPanel		panel 				= new JPanel();
    	private Component 	internal;
    
    	public static void main( String[] args )
    	{
    		JFrame frame = new JFrame();
    		
    		frame = new JFrame();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(1000,1000);
    		TextArea text = new TextArea("ssssssssssss");
    		text.setSize(1000, 500);
    		MyScrollPane p = new MyScrollPane(text);
    		frame.add(p);
    		
    		frame.pack();
    		frame.setSize(500,500);
    		frame.setVisible(true);
    	}
    	
    	public MyScrollPane( Component content )
    	{
    		addComponentListener(this);
    		
    		horizontalScroll.addAdjustmentListener(this);
    		verticalScroll.addAdjustmentListener(this);
    		
    		setLayout( new BorderLayout() );
    
    		add( panel, 			BorderLayout.CENTER	);
    		add( horizontalScroll, 	BorderLayout.SOUTH	);
    		add( verticalScroll, 	BorderLayout.WEST  	);	
    		
    		internal = content;
    		
    		panel.setLayout(null);
    		panel.add(internal);
    	}
    
    	@Override
    	public void componentHidden(ComponentEvent e)
    	{	
    	}
    
    	@Override
    	public void componentMoved(ComponentEvent e) 
    	{	
    	}
    
    	@Override
    	public void componentResized(ComponentEvent e) 
    	{
    		horizontalScroll.setValues( 0, panel.getWidth(),  0, internal.getWidth()  );
    		verticalScroll	.setValues( 0, panel.getHeight(), 0, internal.getHeight() );
    	}
    
    	@Override
    	public void componentShown(ComponentEvent e) 
    	{
    	}
    
    	@Override
    	public void adjustmentValueChanged(AdjustmentEvent e) 
    	{
    		boolean vertical = e.getSource().equals(verticalScroll);
    		
    		int x =  vertical ? internal.getX() : -e.getValue();
    		int y = !vertical ? internal.getY() : -e.getValue();
    		
    		internal.setLocation(x,y);
    	}
    }

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

    Default Re: making a scroll pane widget

    Quote Originally Posted by jajdoo View Post
    i'm trying to make a widget to work like JScrollPane.
    I'm confused -- why not just use a JScrollPane?

  3. #3
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: making a scroll pane widget

    its not working properly; im having trouble describing the problem because i'm not quite sure i understand what's actually happening incorrectly
    Frankly, it is necessary for you to describe the problem in order to make progress. Otherwise everybody else is left guessing. Its not that you are expected to say what the cause is, or what lines of code are problematic, but you must be seeing *something* on your screen that you regard as a problem or you wouldn't have posted. Say what that is.

    Does the code compile? If not and you can't understand the compiler's messages, post them.

    Does the code compile but do something unintended or unwanted at runtime (including throwing a runtime exception)? If so describe what the actual behaviour is (including stacktraces and other output and visual appearance) as well as saying what the intended or desired behaviour is.

  4. #4
    jajdoo is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default Re: making a scroll pane widget

    Quote Originally Posted by Fubarable View Post
    I'm confused -- why not just use a JScrollPane?
    because this is self exercise, not practical code.

    Quote Originally Posted by pbrockway2 View Post
    Frankly, it is necessary for you to describe the problem in order to make progress. Otherwise everybody else is left guessing. Its not that you are expected to say what the cause is, or what lines of code are problematic, but you must be seeing *something* on your screen that you regard as a problem or you wouldn't have posted. Say what that is.

    Does the code compile? If not and you can't understand the compiler's messages, post them.

    Does the code compile but do something unintended or unwanted at runtime (including throwing a runtime exception)? If so describe what the actual behaviour is (including stacktraces and other output and visual appearance) as well as saying what the intended or desired behaviour is.
    when i scroll, the content covers the scroll bar, instead of sliding "beneath" it, and instead of seeing the rest of the content, i see the background. i suspect that while re-positioning "internal" beyond the bounds of "panel" it moves both, therefor, not revealing the rest of internal.

  5. #5
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: making a scroll pane widget

    i suspect that while re-positioning "internal" beyond the bounds of "panel" it moves both, therefor, not revealing the rest of internal.
    So test that.

    Java Code:
    @Override
    public void adjustmentValueChanged(AdjustmentEvent e) 
    {
        boolean vertical = e.getSource().equals(verticalScroll);
    
        int x =  vertical ? internal.getX() : -e.getValue();
        int y = !vertical ? internal.getY() : -e.getValue();
    
        internal.setLocation(x,y);
    
        System.out.println("internal: " + internal.getBounds());
        System.out.println("panel: " + panel.getBounds());
    }
    (internal's bounds are relative to panel, and panel's relative to the content pane of the frame.)

    As far as seeing (the panel's) background is concerned it will make a difference whether text is opaque or not.

    Java Code:
    TextArea text = new TextArea("ssssssssssss");
    System.out.println("text will paint itself fully: " + text.isOpaque());
    text.setSize(1000, 500);
    MyScrollPane p = new MyScrollPane(text);
    frame.add(p);
    -----

    My take on it...

    You're mixing Swing and pre-Swing components. See, eg, Mixing heavy and light components where the authors say "When a lightweight component overlaps a heavyweight component, the heavyweight component is always on top, regardless of the relative z-order of the two components". The TextArea is heavy while the JScrollBar and JPanel are both light.

    Even if you are consistent and use Swing components throughout you might expect the background to show through since when the MyScrollPane instance is resized the (J)TextArea instance it contains is not. I think correcting this is part of making the contents of scrollpanes "scroll savvy".
    Last edited by pbrockway2; 01-04-2012 at 02:52 AM.

  6. #6
    jajdoo is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default Re: making a scroll pane widget

    thank you,
    Java's gui is still very foreign to me. coming from Qt background, it takes some getting used to.
    change to constructor to accepts a JComponent, to keep it swing.

Similar Threads

  1. Widget issue
    By soundprizm in forum New To Java
    Replies: 0
    Last Post: 04-21-2011, 07:24 PM
  2. Any widget for file viewing¿
    By lqbweb in forum SWT / JFace
    Replies: 0
    Last Post: 10-21-2010, 01:40 PM
  3. SWT Transition Widget - STW
    By fossilman2005 in forum SWT / JFace
    Replies: 0
    Last Post: 01-03-2010, 12:16 AM
  4. Simple widget advice...
    By billy341 in forum SWT / JFace
    Replies: 0
    Last Post: 04-04-2009, 09:58 PM
  5. Scrolling and Headers in a scroll pane
    By glhansen in forum AWT / Swing
    Replies: 10
    Last Post: 04-03-2009, 03:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •