Adding scrollbars to canvas!!

hi everybody,
a have a simple ??
i'm working on a chat application. my message displaying area is a canvas.i have added scroll-bars to it using scroll-pane
"sp = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);"
now what i want is the scrollbars should adjust automatically to
display the last message and also i should be able to adjust them
manually so that i can view the previous messages.
i have tried setScrollposition() it works well also but then i'm not
able to adjust scrollbars manually.
this interface is in AWT.
please tell me a solution.its a bit urgent.
thankx and regards
asheet

Because your canvas is growing.(I think)
The new size of the canvas causes it to be layd out again. And laying out a component usually causes flickering (depending on the plattform). I guess the workaround would be to use a canvas just as big as the shown part of it. Add a scrollbar by its side (not a scrollpane) and use the scrollposition to choose what part of the text that should be painted. That way you can use doublebuffering, and avoid the problem completly. (No resizing of components means nothing is layd out again)
Ragnvald Barth
Software engineer

Similar Messages

  • Scrollbar on canvas??

    hi guys,
    i hav a prob. in putting scrollbar on canvas..
    actally i m drawing many imgs over canvas..so i need a vertical scrolbar to see all the imgs ..
    can any one of u help me..

    though.. I didnt understand ur problem correctly.. !! u didnt specify in which part u r facing problem??.. if u r drawing images in JPanel..
    just add that JPanel to JScrollPane
    JPanel panel = new JPanel();
    panel.setPreferredSize(new Dimension(600,600));
    JScrollPane scroller = new JScrollPane(panel);
    scroller.setPreferredSize(new Dimension(200,200));
    frame.add(scroller);

  • Adding scrollbars in jtextarea

    can any one tell me the method of adding scrollbars in jtextarea such that they are enabled when required means initially disabled but when the text goes long they are enabled.
    farhan

    Add ur JTextArea in a JScrollPane by doing this :
    JScrollPane jsp = new JScrollPane(JScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    //then add ur Jtext hereMaz -

  • How to make the scroll bar in flex automate to focus on the new ui component added in the canvas?

    Hi all ,
    There is a canvas container where am adding charts in separate windows.
    So whenever a new chart is added the scroll bar needs to set the focus on the present chart window.
    For this i made the functionality for the canvas container to scroll whenever a new chart is added using the below code
    canvasContainer.verticalScrollPosition = canvasContainer.maxVerticalScrollPosition;
    But the calculations for the vertical position are not precise...
    Is there anything else I should do to make the scroll happen automatically as the chart windows get added in the container ??
    Any suggestions pls
    Regards,
    Ajantha

    Many many thanks to Frank,
    In the css,use the follwing style settings to hide the scroll bar
    af|carousel::spin-bar{    
    visibility: hidden;
    af|carousel::spin-h-previous-icon-style{
    visibility: hidden;
    af|carousel::spin-h-next-icon-style{
    visibility: hidden;
    af|carousel::spin-info{
    visibility: hidden;
    }

  • Adding ScrollBars to JTextPane

    I migrated from TextArea to JTextPane in a simple chat program in order to display colors and styled font sizes. The problem is that the border and scroll bars previously given by TextArea are gone when I switched to JTextPane. I managed to place a border. But I cannot figure out how to add at least vertical scroll bars to the TextArea. I tried to use JScrollPane and then added the JTextPane to it, but then I cannot see what I type.
    Can you please help?
    CS

    You are doing the correct thing. To add scrolling to any Swing component you add the component to a JScrollPane and then add the JScrollPane to your container.
    If you cannot see what you are typing that may be because you didn't give your component enough space and the scrollbars of the JScrollPane are taking up all the space. Give your component more space and see if that fixes the problem.
    HTH
    Bryan

  • Scrollbar in canvas & countdown timer

    how do i create a scrollbar in a canvas? it is to display a list of text.
    anyone knows how to create a timer that counts down from 2 minutes to 0 seconds?
    i've searched but to no avail. please help.

    need desperate help....
    this is my code:
    I have a gameCanvas class extends canvas implements Runnable. this is the run method.
    public void run()
              if(gameOver)
                   if(points > targetP)
                        getPuzzleSet(2);
                        repaint();
                        serviceRepaints();
    this is also a method in the class. it is called by the countddown(below) class.
    public void stopTimer()
              timer.cancel();
              gameOver = true;     
              repaint();
    class Countdown extends TimerTask
         GameCanvas gc;
         int min = 0;
         int sec = 10;
         public Countdown(GameCanvas gc)
              this.gc = gc;
         public void run()
              gc.setTime(min,sec);
              sec--;
              if(sec == 0)
                   if(min == 0)
                        gc.stopTimer();
                   else
                   min--;
                   sec = 59;     
    my problem is the "timer.cancel()" seems to stop the entire application. what i want it to do is when the timer reaches zero, the run method will check if player can move to next level and will display the next level. the timer will also be reset back to initial value.

  • Can't move Popup TitleWindow when it gets added to a canvas

    Hi,
    I've created a TitleWindow that gets popped up using
    PopUpManager. When this gets added to the Application.application,
    I can move this popup window around just fine (via mouse
    click-n-dragging the top part of the window). However, when I want
    to add this popup window as a child to a canvas (using addChild()),
    I can see the window on the canvas but I can't move the window
    around (seems to be static). I also can't close the window anymore
    as well. Is there something wrong?

    Maybe because Canvas is absolute layout.

  • Adding Scrollbar and buttons to Dynamic Text

    Hello,
    I am trying to connect dynamic text to scrollbar and buttons. I did tutorial and Lynda.com and practically pasted the code in with my file names and for some reason it does not work. It says I have a "Access of Undefined Property mask_mc" Did I need to create a variable for this? I didn't in the tutorial.
    Right now I have the dynamic text loading successfuly in 2 different places and I wantd to add the scrollbar. I put the variables on frame 1 code and then I put the actually load code on the frame where it is needed.
    Any suggestions?
    Thanks! Sandra
    HERE IS CODE FOR FRAME 1:
    var textLoader:URLLoader = new URLLoader();
    var textReq:URLRequest;
    var scrollPercent:Number = 0;
    var minScroll:Number;
    var maxScroll:Number;
    var targetScroll:Number = philText_mc.y;
    var easing:Number = 5;
    var scrollAmt:Number = 15;
    var scrollDirection:Number = 0;
    HERE IS CODE FOR FRAME WHERE TEXT LOADS:
    textReq = new URLRequest("text_philosophy.txt");
    function philosophyTextLoaded(event:Event):void {
        philText_mc.philosophy_txt.text = textLoader.data;
        minScroll = philText_mc.y;
        maxScroll = minScroll - philText_mc.height + mask_mc.height;
    function dragScroller(event:MouseEvent):void
        var dragX:Number = line_mc.x - scroller_mc.width/2 + 1;
        var dragY:Number = line_mc.y;
        var dragW:Number = 0;
        var dragH:Number = line_mc.height - scroller_mc.height;
        scroller_mc.startDrag(false, new Rectangle(dragX,dragY,dragW,dragH));
        stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
        stage.addEventListener(Event.ENTER_FRAME, setScrollPercent);
        stage.removeEventListener(Event.ENTER_FRAME, scrollText);
    function stopDragging(event:MouseEvent):void
        scroller_mc.stopDrag();
    function setScrollPercent(event:Event):void
        scrollPercent = (scroller_mc.y - line_mc.y) / (line_mc.height - scroller_mc.height);
        if(scrollPercent < 0)
            scrollPercent = 0;
        else if(scrollPercent > 1)
            scrollPercent = 1;
        targetScroll = (scrollPercent * (maxScroll - minScroll)) + minScroll;
        philText_mc.y -= (philText_mc.y - targetScroll) / easing;
    function scrollUp(event:MouseEvent):void
        setDirection(scrollAmt);
    function scrollDown(event:MouseEvent):void
        setDirection(-scrollAmt);
    function setDirection(dir:Number):void
        scrollDirection = dir;
        stage.addEventListener(Event.ENTER_FRAME, scrollText);
        stage.addEventListener(MouseEvent.MOUSE_UP, stopScrolling);
        stage.removeEventListener(Event.ENTER_FRAME, setScrollPercent);
    function scrollText(event:Event):void
        targetScroll += scrollDirection;
        philText_mc.y -= (philText_mc.y - targetScroll) / easing;
        if(philText_mc.y > minScroll)
            philText_mc.y = minScroll;
            targetScroll = minScroll;
        else if(philText_mc.y < maxScroll)
            philText_mc.y = maxScroll;
            targetScroll = maxScroll;
        scrollPercent = (philText_mc.y - minScroll) / (maxScroll - minScroll);
        scroller_mc.y = (scrollPercent * (line_mc.height - scroller_mc.height)) + line_mc.y;
    function stopScrolling(event:MouseEvent):void
        scrollDirection = 0;
    textLoader.load(textReq);
    scroller_mc.buttonMode = true;
    philText_mc.external_txt.autoSize = TextFieldAutoSize.LEFT;
    scroller_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragScroller);
    textLoader.addEventListener(Event.COMPLETE, philosophyTextLoaded);
    up_btn.addEventListener(MouseEvent.MOUSE_DOWN, scrollUp);
    down_btn.addEventListener(MouseEvent.MOUSE_DOWN, scrollDown);

    Hello again,
    Maybe I am asking this question wrong. Instead of making you try and figure out what's going on in my file what I really need to know is how you would take this code I am attaching and make it happen on another frame besides frame 1 and it doesn't appear on frame 1. I think this may help me understand a little better.
    Also another way I was thinking to get around this would be to pull in an external swf into the spot where I want this text to go because I can make the scrollbars and external text work when it is the only thing going on in the movie. Would this be a bad way to set this up? and also SInce it is an external movie would I have maintimeline issues with mouse/scroller control?
    thanks! sandra
    Here is working code:
    var textLoader:URLLoader = new URLLoader();
    var textFile:URLRequest = new URLRequest("text/external.txt");
    var scrollPercent:Number = 0;
    var minScroll:Number;
    var maxScroll:Number;
    var targetScroll:Number = text_mc.y;
    var easing:Number = 5;
    var scrollAmt:Number = 15;
    var scrollDirection:Number = 0;
    function textLoaded(event:Event):void
        text_mc.external_txt.text = textLoader.data;
        minScroll = text_mc.y;
        maxScroll = minScroll - text_mc.height + mask_mc.height;
    function dragScroller(event:MouseEvent):void
        var dragX:Number = line_mc.x - scroller_mc.width/2 + 1;
        var dragY:Number = line_mc.y;
        var dragW:Number = 0;
        var dragH:Number = line_mc.height - scroller_mc.height;
        scroller_mc.startDrag(false, new Rectangle(dragX,dragY,dragW,dragH));
        stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
        stage.addEventListener(Event.ENTER_FRAME, setScrollPercent);
        stage.removeEventListener(Event.ENTER_FRAME, scrollText);
    function stopDragging(event:MouseEvent):void
        scroller_mc.stopDrag();
    function setScrollPercent(event:Event):void
        scrollPercent = (scroller_mc.y - line_mc.y) / (line_mc.height - scroller_mc.height);
        if(scrollPercent < 0)
            scrollPercent = 0;
        else if(scrollPercent > 1)
            scrollPercent = 1;
        targetScroll = (scrollPercent * (maxScroll - minScroll)) + minScroll;
        text_mc.y -= (text_mc.y - targetScroll) / easing;
    function scrollUp(event:MouseEvent):void
        setDirection(scrollAmt);
    function scrollDown(event:MouseEvent):void
        setDirection(-scrollAmt);
    function setDirection(dir:Number):void
        scrollDirection = dir;
        stage.addEventListener(Event.ENTER_FRAME, scrollText);
        stage.addEventListener(MouseEvent.MOUSE_UP, stopScrolling);
        stage.removeEventListener(Event.ENTER_FRAME, setScrollPercent);
    function scrollText(event:Event):void
        targetScroll += scrollDirection;
        text_mc.y -= (text_mc.y - targetScroll) / easing;
        if(text_mc.y > minScroll)
            text_mc.y = minScroll;
            targetScroll = minScroll;
        else if(text_mc.y < maxScroll)
            text_mc.y = maxScroll;
            targetScroll = maxScroll;
        scrollPercent = (text_mc.y - minScroll) / (maxScroll - minScroll);
        scroller_mc.y = (scrollPercent * (line_mc.height - scroller_mc.height)) + line_mc.y;
    function stopScrolling(event:MouseEvent):void
        scrollDirection = 0;
    textLoader.load(textFile);
    scroller_mc.buttonMode = true;
    text_mc.external_txt.autoSize = TextFieldAutoSize.LEFT;
    scroller_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragScroller);
    textLoader.addEventListener(Event.COMPLETE, textLoaded);
    up_btn.addEventListener(MouseEvent.MOUSE_DOWN, scrollUp);
    down_btn.addEventListener(MouseEvent.MOUSE_DOWN, scrollDown);

  • Adding a child canvas immediately

    Hi there,
    I'm trying to add a canvas to my application which fills the screen while a CPU intensive action runs in the background. No matter what I try, I cannot get the loading screen to appear until after the slow action has finished.
    Code (belongs in a subclass of Canvas):
    private function saveBitmap(event:ContextMenuEvent):void
        loadingScreen.visible = true;
        loadingScreen.appLoadingText.text = "Preparing bitmap...";
        addChild(loadingScreen);
        validateNow();
         // Slow code!
        var bmpd:BitmapData = new BitmapData(canv.width, canv.height);
        bmpd.draw(canv);
        var fr:FileReference = new FileReference();
        fr.addEventListener(Event.COMPLETE, removeLoadingScreen);
        fr.addEventListener(Event.CANCEL, removeLoadingScreen);
        var png:PNGEncoder = new PNGEncoder();
        var iba:ByteArray = png.encode(bmpd);
        fr.save(iba, "export.png");   
    Unfortunately sticking the slow code into callLater() doesn't work because then the FileReference() can't be created.
    Any ideas?
    Cheers

    Thanks for the reply. What I've ended up with is quite strange:
    private function saveBitmap(event:ContextMenuEvent):void
        loadingScreen.visible = true;
        loadingScreen.appLoadingText.text = "Preparing bitmap...";
        <point a>
        addChild(loadingScreen);
        validateNow();
        <point b>
        trace("Fired!");
    private function prepareBitmap(event:FlexEvent):void
        trace("Fired beta!");
        removeEventListener(FlexEvent.UPDATE_COMPLETE, prepareBitmap);
        var bmpd:BitmapData = new BitmapData(canv.width, canv.height);
        bmpd.draw(canv);
        var fr:FileReference = new FileReference();
        fr.addEventListener(Event.COMPLETE, removeLoadingScreen);
        fr.addEventListener(Event.CANCEL, removeLoadingScreen);
        var png:PNGEncoder = new PNGEncoder();
        var iba:ByteArray = png.encode(bmpd);
        fr.save(iba, "export.png");   
    If I put addEventListener(FlexEvent.UPDATE_COMPLETE, prepareBitmap); at point a, then I get "Fired beta" then "Fired" (this makes sense I think, since the validateNow() will block until the UPDATE_COMPLETE event is finished) with no loading screen.
    If I put it the listener at point b then "Fired" is first and the loading screen appears, followed by a small (5 sec?) delay before "Fired Beta" is printed and the application dies from "Error: Error #2176: Certain actions, such as those that display a pop-up window, may only be invoked upon user interaction, for example by a mouse click or button press.".

  • WebDynpro ABAP - issue adding scrollbar to table UI element.

    Hi WebDynPro experts,
        i have an requirement where in table UI element in one of my VIEW requires SCROLLBAR to appear.  i did try setting in 'Webdynpro Application' under the parameters tab, new parameter 'WDTABLENAVIGATION' from the F4. However the problem is that  i cannot find WDTABLENAVIGATION' via F4 nor can i enter this param manually ????
    System info: SAP ECC 6.0 Level 9
    whats the best way to achieve this effect.

    have a look at note 998273.
    Seems to available as of SAP_BASIS 700 Support Package 11.

  • Adding Image to Canvas Won't Start at 0 (but img.y says 0)

    I am going nuts! I have a canvas which I am trying to add a background image to. I am not setting the Y position, but after I add it to the canvas it always starts about 40 px down. If I trace the Y position it still says 0, even after a timer. I have tried to move it to the parent canvas and got the same results.
    What is going on?

    If the image y is indicating 0, then chances are whatever object contains the image is offset somehow.

  • Creating shapes and adding them to canvas

    hi all
    How can we create shapes in flex..add them into a canvas i found code that used sprite but was unable to add .as it not being a uicomponent?

    You need to wrap your sprite in a UIComponent. Accessing rawChildren can be dangerous.
    var mySprite:Sprite = new Sprite();
    mySprite.graphics.beginFill(0xFFCC00);
    mySprite.graphics.drawCircle(30, 30, 30);
    var uic:UIComponent = new UIComponent();
    uic.addChild(mySprite);
    this.addChild(uic);
    If this post answers your question or helps, please mark it as such.
    Greg Lafrance
    www.ChikaraDev.com
    Flex Development and Support Services

  • Adding scrollbars to a applet?

    I am trying to write a small java applet for my personal interest, but I can't seem to figure out how to add scroll bars to my applet. What I have so far is this
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.*;
    import java.io.IOException;
    public class SimpleApplet extends JApplet {
      public SimpleApplet() {
        JPanel p = new JPanel();
        p.setLayout(new GridLayout(20, 3, 3, 3));
        p.add(new JLabel("Username"));
        p.add(new JTextField());
        p.add(new JLabel("Password"));
        p.add(new JPasswordField());
        p.add(new JLabel("Username"));
        p.add(new JTextField());
        p.add(new JLabel("Password"));
        p.add(new JPasswordField());
        p.add(new JLabel("Username"));
        p.add(new JTextField());
        p.add(new JLabel("Password"));
        p.add(new JPasswordField());
        p.add(new JLabel("Username"));
        p.add(new JTextField());
        p.add(new JLabel("Password"));
        p.add(new JPasswordField());
        Container content = getContentPane();
        JScrollPane scroller = new JScrollPane();
        scroller.setLayout(new BorderLayout());
        content.setLayout(new GridBagLayout()); // Used to center the panel
        content.add(scroller);
        //content.add(p);
    public static void main(String[] args) throws IOException, ClassNotFoundException {
         SimpleApplet J = new SimpleApplet();
    }when I try to run this the applet fails, but if I delete all the lines with scroller the applet runs fine expect that I can't see some the textfield on the applet unless I enlarge it. I want to add a scrollbar in so that I can scroll down to the unseen parts without enlarging the applet screen. Can any1 help me figure out what i'm doing wrong?

    Encephalopathic wrote:
    bigauto wrote:
    I'm still running the applet in eclipse and I haven't tested it on a webpage yet. It shouldn't matter whether it on eclipse or a webpage should it? The applet itself didn't become small, but all the components like the textfield became micoscope.Does it matter if the application is ran on eclipse or a webpage?1) did you look at my example? Did you get rid of your setting the contentpane's layout to gridbaglayout? the contentpane by default uses borderlayout and this should let the jscrollpane fill your applet. If you don't do this, you may need to set a preferred size for your jscrollpane.
    2) I think that you can size the applet in Eclipse's Run Dialog, but I'm not sure. You size it in "real life" in the HTML code (I think).Thanks a lot. That fixed it.

  • Adding scrollbars to a panel containing a buffered image

    Frame Class:
    package HeatMap;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.GridBagLayout;
    import java.awt.image.BufferedImage;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.ScrollPaneConstants;
    import javax.swing.SwingUtilities;
    class HeatMapFrame extends JFrame
         BufferedImage img;
         HeatMapFrame(BufferedImage img)
              super("Gene Expression Profile Heatmap");
              this.img = img;
              HeatMapPane mapPanel = new HeatMapPane(img);
              JScrollPane scrollPane = new JScrollPane(mapPanel, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,   
                                              ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS );   
              scrollPane.setPreferredSize(new Dimension(640,480));
              add(scrollPane,BorderLayout.CENTER);
              setSize(640,480);
              setLocation(getWidth()/4,getHeight()/4);
              setDefaultCloseOperation(EXIT_ON_CLOSE);
              setVisible(true);
    Panel Class:
    package HeatMap;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.geom.AffineTransform;
    import java.awt.image.BufferedImage;
    import javax.swing.JPanel;
    public class HeatMapPane extends JPanel
         BufferedImage img;
         HeatMapPane(BufferedImage img)
              this.img = img;
              //setSize(640,480);
         protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            // Draw image centered.
            //int x = (getWidth() - img.getWidth())/2;
            //int y = (getHeight() - img.getHeight())/2;
            g.drawImage(img, 0, 0, this);
    }I`m drawing the BufferedImage to a JPanel and addig the JPanel to a JScrollPane. The problem is that the scrollbars are visible but not active. Scrolling is not working. Can somebody tell me why? Thanks for any advice.

    Since your HeatMapPane does not declare a preferred size, I'm guessing(1) it is assigned a size of 0x0.
    1) I would do more than guess, if you had posted an SSCCE. For an SSCCE to do with images, you might hot-link to some of the images available at my [media page|http://pscode.org/media/#image], or generate an image within the source.

  • Adding buttons to Canvas

    Hi
    i have a class that extends Canvas.
    can i add buttons to the canvas?
    i mean, i want to make the images in the canvas clickable.
    is this possible in Java?
    Thank for help.

    This is the complete code:
    import java.awt.*;
    import java.applet.*;
    import java.applet.Applet;
    import java.awt.image.*;
    import javax.swing.ImageIcon;
    class IconCanvas extends Canvas{
    Applet app;
    Image h;
    Image s;
    ImageIcon h_icon ;
    ImageIcon s_icon ;
    public void init(Applet app)
         this.app=app;
         h = app.getImage(app.getCodeBase(), "hidey.bmp");
         s = app.getImage(app.getCodeBase(), "salley.gif");
        h_icon = new ImageIcon( h) ;
        s_icon = new ImageIcon( s) ;
    public void paint(Graphics g)
             add(h_icon);  // Error Here because add applied only to awt component not swing component(ImageIcon)
    public class CanvasTest extends Applet {
         IconCanvas canvas;
         Panel button_panel;
         public void init()
                canvas = new IconCanvas();
              canvas.init(this);
               button_panel = new Panel();
              button_panel.setLayout(new BorderLayout());
              this.setLayout(new BorderLayout());     
              add("Center", canvas);  
    }

Maybe you are looking for