Results 1 to 6 of 6
Thread: making a scroll pane widget
- 01-02-2012, 03:28 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 3
- Rep Power
- 0
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); } }
-
Re: making a scroll pane widget
- 01-02-2012, 11:04 PM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: making a scroll pane widget
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.its not working properly; im having trouble describing the problem because i'm not quite sure i understand what's actually happening incorrectly
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.
- 01-03-2012, 08:17 AM #4
Member
- Join Date
- Jan 2012
- Posts
- 3
- Rep Power
- 0
Re: making a scroll pane widget
because this is self exercise, not practical code.
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.
- 01-04-2012, 01:50 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: making a scroll pane widget
So test that.i suspect that while re-positioning "internal" beyond the bounds of "panel" it moves both, therefor, not revealing the rest of internal.
(internal's bounds are relative to panel, and panel's relative to the content pane of the frame.)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()); }
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 01:52 AM.
- 01-04-2012, 09:22 AM #6
Member
- Join Date
- Jan 2012
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Widget issue
By soundprizm in forum New To JavaReplies: 0Last Post: 04-21-2011, 06:24 PM -
Any widget for file viewing¿
By lqbweb in forum SWT / JFaceReplies: 0Last Post: 10-21-2010, 12:40 PM -
SWT Transition Widget - STW
By fossilman2005 in forum SWT / JFaceReplies: 0Last Post: 01-02-2010, 11:16 PM -
Simple widget advice...
By billy341 in forum SWT / JFaceReplies: 0Last Post: 04-04-2009, 08:58 PM -
Scrolling and Headers in a scroll pane
By glhansen in forum AWT / SwingReplies: 10Last Post: 04-03-2009, 02:06 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks