Java Forum / GUI / May 2004
AWT ScrollPane
Roedy Green - 29 May 2004 03:11 GMT Is there any way I have missed to use an AWT ScrollPane and have just vertical or just horizontal scrolling?
Going to JScrollPane makes me lose unsigned Applet cut and paste.
 Signature Canadian Mind Products, Roedy Green. Coaching, problem solving, economical contract programming. See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Andrew Thompson - 29 May 2004 03:38 GMT > Is there any way I have missed to use an AWT ScrollPane and have just > vertical or just horizontal scrolling? ...errr. Did you see the constructor.. ScrollPane.html#ScrollPane(int)
I generally use the .. TextArea.html#TextArea(String, int, int, int)
 Signature Andrew Thompson http://www.PhySci.org/ Open-source software suite http://www.PhySci.org/codes/ Web & IT Help http://www.1point1C.org/ Science & Technology
Roedy Green - 29 May 2004 03:58 GMT >...errr. Did you see the constructor.. >ScrollPane.html#ScrollPane(int) So far as I can tell, Scrollpane offers only:
SCROLLBARS_ALWAYS SCROLLBARS_AS_NEEDED SCROLLBARS_NEVER
however TextArea seems to be more capable that ScrollPane, which I never would have expected.
it has: SCROLLBARS_BOTH SCROLLBARS_HORIZONTAL_ONLY SCROLLBARS_NONE SCROLLBARS_VERTICAL_ONLY
Unfortunately, I am doing a custom component, so I need ScrollPane or JScrollPane. JScrollPane has separate horizontal and vertical control but no copy paste in unsigned Applets.
Someone, please lend Sun a copy of the Oxford dictionary with a PostIt flag on the page where they define orthogonal.
 Signature Canadian Mind Products, Roedy Green. Coaching, problem solving, economical contract programming. See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Andrew Thompson - 29 May 2004 04:47 GMT ..
>>ScrollPane.html#ScrollPane(int) > [quoted text clipped - 3 lines] > SCROLLBARS_AS_NEEDED > SCROLLBARS_NEVER ...
> however TextArea seems to be more capable that ScrollPane, which I > never would have expected. [quoted text clipped - 7 lines] > Unfortunately, I am doing a custom component, so I need ScrollPane or > JScrollPane. Yes, I did miss the distinction, and am more used to TextArea scrolling..
> Someone, please lend Sun a copy of the Oxford dictionary with a PostIt > flag on the page where they define orthogonal. good idea..
 Signature Andrew Thompson http://www.PhySci.org/ Open-source software suite http://www.PhySci.org/codes/ Web & IT Help http://www.1point1C.org/ Science & Technology
ak - 29 May 2004 06:39 GMT > Is there any way I have missed to use an AWT ScrollPane and have just > vertical or just horizontal scrolling? no, but you can use ScrollBar.
 Signature http://uio.dev.java.net http://reader.imagero.com
Roedy Green - 29 May 2004 07:31 GMT >no, but you can use ScrollBar. I thought that was just something used internally by ScrollPane. Do you have any sample code?
 Signature Canadian Mind Products, Roedy Green. Coaching, problem solving, economical contract programming. See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
ak - 29 May 2004 11:18 GMT > >no, but you can use ScrollBar. > > I thought that was just something used internally by ScrollPane. Do > you have any sample code? if you use TextArea, then just create TextArea with only 1 ScrollBar: int rows = 100; int columns = 400;
TextArea ta = new TextArea("some text", rows, columns , TextArea.SCROLLBARS_VERTICAL_ONLY);
 Signature http://uio.dev.java.net http://reader.imagero.com
Andrew Thompson - 29 May 2004 12:09 GMT >>>no, but you can use ScrollBar. >> >> I thought that was just something used internally by ScrollPane. Do >> you have any sample code? > > if you use TextArea, .. (Roedy - on another tendril of this thread..) "Unfortunately, I am doing a custom component, so I need ScrollPane or JScrollPane."
 Signature Andrew Thompson http://www.PhySci.org/ Open-source software suite http://www.PhySci.org/codes/ Web & IT Help http://www.1point1C.org/ Science & Technology
Roedy Green - 29 May 2004 20:16 GMT >if you use TextArea, then just create TextArea with only 1 ScrollBar: >int rows = 100; >int columns = 400; > >TextArea ta = new TextArea("some text", rows, columns , >TextArea.SCROLLBARS_VERTICAL_ONLY); The problem is I need to scroll a custom component I draw myself as a bit map.
What I do is swap it with a vanilla TextArea if someone wants to cut and paste, where the scroll bars are under control in AWT. However that still does not get rid of the unwanted control bars on my custom component that is stuck using ScrollPane.
I suppose since TextArea can do it, supposedly I could to, building scrollbars into my custom component the same way TextArea did.
 Signature Canadian Mind Products, Roedy Green. Coaching, problem solving, economical contract programming. See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Roedy Green - 30 May 2004 00:50 GMT >I suppose since TextArea can do it, supposedly I could to, building >scrollbars into my custom component the same way TextArea did. I am puzzled to pieces. I looked at the code TextArea uses to handle scrollbars, and it seems to record the visibility in a private variable and does nothing with it, but expose it in a getScrollbarVisibility method that nobody ever calls.
Perhaps the magic occurs deep in the peers or native gui.
I will try the same trick and see if the magic happens for me too.
 Signature Canadian Mind Products, Roedy Green. Coaching, problem solving, economical contract programming. See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
ak - 30 May 2004 01:05 GMT > >I suppose since TextArea can do it, supposedly I could to, building > >scrollbars into my custom component the same way TextArea did. [quoted text clipped - 7 lines] > > I will try the same trick and see if the magic happens for me too. I don't believe that this magic works, because you have to load some native library and this can work only from some privileged package ;-)
 Signature http://uio.dev.java.net http://reader.imagero.com
Roedy Green - 30 May 2004 01:49 GMT >I am puzzled to pieces. I looked at the code TextArea uses to handle >scrollbars, and it seems to record the visibility in a private [quoted text clipped - 4 lines] > >I will try the same trick and see if the magic happens for me too. That does not seem to be sufficient. No scroll bars magically appear just by having a getScrollbarVisibility method.
 Signature Canadian Mind Products, Roedy Green. Coaching, problem solving, economical contract programming. See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
ak - 30 May 2004 01:07 GMT > The problem is I need to scroll a custom component I draw myself as a > bit map. > > What I do is swap it with a vanilla TextArea if someone wants to cut > and paste, can you explain me the purpose of such component?
 Signature http://uio.dev.java.net http://reader.imagero.com
Roedy Green - 30 May 2004 01:50 GMT >> What I do is swap it with a vanilla TextArea if someone wants to cut >> and paste, >can you explain me the purpose of such component? see http://mindprod.com/projects/javapresenter.html
 Signature Canadian Mind Products, Roedy Green. Coaching, problem solving, economical contract programming. See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
ak - 30 May 2004 10:43 GMT > >> What I do is swap it with a vanilla TextArea if someone wants to cut > >> and paste, > >can you explain me the purpose of such component? > > see http://mindprod.com/projects/javapresenter.html Roedy,
since you know line height in your component, you can easy track (and paint) text selection without vanilla TextArea.
 Signature http://uio.dev.java.net http://reader.imagero.com
Roedy Green - 30 May 2004 12:18 GMT >since you know line height in your component, you can easy track (and paint) >text selection without vanilla TextArea It is a little more complicated since I use variable fonts throughout. There are a variety of families, styles, sizes, and of course colours. I could compute x,y back to charIndex since the whole thing is tokenized, and I compute the x offset for each token.
It would probably take me two days to get it perfect. Anyone interested in trying?
When rendering, the tokens are very simple, a string of text, a font and a colour. There are special tokens for whitespace and nls.
Rendering is very fast, since the fonts are all statically prefabricated.
you can see the essential code at http://mindprod.com/projects/javapresenter.html
Ideas for improvement always welcome.
 Signature Canadian Mind Products, Roedy Green. Coaching, problem solving, economical contract programming. See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
ak - 30 May 2004 10:37 GMT > Is there any way I have missed to use an AWT ScrollPane and have just > vertical or just horizontal scrolling? > > Going to JScrollPane makes me lose unsigned Applet cut and paste. public class CustomScrollPane extends Container {
Scrollbar vscroll; Component child; Container contentPane;
//set to line height int unitSize = 20;
public CustomScrollPane(Component child) { super.setLayout(new BorderLayout()); vscroll = new Scrollbar(Scrollbar.VERTICAL); this.child = child; contentPane = new Container(); add(vscroll, BorderLayout.EAST); add(contentPane, BorderLayout.CENTER); contentPane.add(this.child);
vscroll.addAdjustmentListener(new AdjustmentVHandler()); vscroll.setUnitIncrement(unitSize);
ComponentHandler componentHandler = new ComponentHandler(); contentPane.addComponentListener(componentHandler);
//little hack to set width of child contentPane.addComponentListener(new ComponentAdapter() { public void componentResized(ComponentEvent e) { Component child0 = CustomScrollPane.this.child; Dimension d = contentPane.getSize(); Dimension d0 = child0.getSize(); child0.setSize(d.width, d0.height); } });
child.addComponentListener(componentHandler); }
private class ComponentHandler extends ComponentAdapter { public void componentResized(ComponentEvent e) { int maximum = CustomScrollPane.this.child.getSize().height; int visibleAmount = vscroll.getSize().height; vscroll.setMaximum(maximum); vscroll.setVisibleAmount(visibleAmount); vscroll.setBlockIncrement(visibleAmount); } }
private class AdjustmentVHandler implements AdjustmentListener { public void adjustmentValueChanged(AdjustmentEvent e) { Point location = child.getLocation(); location.y = -vscroll.getValue(); child.setLocation(location); } }
public static void main(String[] args) { Canvas c = new Canvas() { public void paint(Graphics g) { super.paint(g); Dimension d = getSize(); Paint paint = new GradientPaint(0,0, Color.red, d.width, d.height, Color.blue); ((Graphics2D)g).setPaint(paint); // g.setClip(getParent().getBounds()); g.fillRect(0,0,d.width, d.height); } }; c.setSize(200, 600); CustomScrollPane pane = new CustomScrollPane(c); Frame frame = new Frame(); frame.add(pane); frame.pack(); frame.show(); } }
 Signature http://uio.dev.java.net http://reader.imagero.com
Roedy Green - 30 May 2004 12:20 GMT >public class CustomScrollPane extends Container { > [quoted text clipped - 4 lines] > //set to line height > int unitSize = 20; Wow! What gave you the conceptual breakthrough to start thinking of code like that?
 Signature Canadian Mind Products, Roedy Green. Coaching, problem solving, economical contract programming. See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
ak - 30 May 2004 13:26 GMT > >public class CustomScrollPane extends Container { > > [quoted text clipped - 7 lines] > Wow! What gave you the conceptual breakthrough to start thinking of > code like that? is something wrong with my code?
 Signature http://uio.dev.java.net http://reader.imagero.com
Roedy Green - 30 May 2004 21:33 GMT >> Wow! What gave you the conceptual breakthrough to start thinking of >> code like that? >is something wrong with my code? Not at all. Take me literally.
 Signature Canadian Mind Products, Roedy Green. Coaching, problem solving, economical contract programming. See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|