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 -

Similar Messages

  • Setting scrollbar in jtextarea

    hi
    Consider a JSplitPanel. in that split panel i have added jtree as the left component, and jScrollpane as the right component. if the user click a node in the jtree, i use the setDividerLocation method to divide the splite pand and add a jscrollpane at right side. in that jscrollpane i added a jtext area with the horizontal scrollbar option which will show always. thing is working well. scrollbar showing when the jscrollpane get visible. the problem is if the text of the text area is go beyond than the jtextarea viewport there is no change in scrollbar. it is showing lik dummy scrollbar without any changes.
    What i have to do to activate the scrollbar if the text in the jtextarea execde it view area?

    I devided JScrollPane Vertically. i added a jtree as left side component of JScrollPane. i did't add anythin in the right side. <br>
    if the user click a node in the JTree which is added in the left side of the jScrollpane, then i add a new jscrollpane as the right side component.<br>
    In that jscrollpane i added a JText area.<br>
    The JTextArea which i created earlier set the vertical scroll bar policy as always. so the vertical scrollbar will be shown always. <br>
    The above things working correctly.<br>
    What is wrong is,<br>
    if the user type any text more than text area, the scrollbar should be activated lik in our forums(text area). But it dos't happen to me. the empty jscrollbar is displayed eventhough the user enter more data that can't be displayed in the jtext area.<br>
    MyAction is,<br>
    if the user click the jtree, i call the setDivider location to devide the JSplitpane. so the left side componet(JTree) uses 200 as width all other width in the screen will be allocated to the JSplitPane(contains the JTextarea).<br>
    My question is,<br>
    why the JScrollbar is not activated are perform correctly eventhough the data which is entered in the JTextArea execedes the row(example 40) which i specified in the constructer when i create the jTextArea?

  • Add scrollbar to JTextArea

    Hello!
    I dont understand why this is not working:
    When I have added the scrollbar I can not print out anything on the JTextArea, if I just add the JTextArea I can print out to it.
    part of my view
              //Display (Do not forget to make the scrollbar after logic is implemented)
              JScrollPane scroll = new JScrollPane(display, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
              JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
              scroll.setLayout(null);
              scroll.setLocation(10, 10);
              scroll.setSize(320, 270);
              //display.setLocation(10, 10);
              //display.setSize(320, 270);
              display.setEditable(false);
              display.setLineWrap(true); //Start a new line instead of making display area bigger
              display.setWrapStyleWord(true); //Words will not split in the middle, the full word will instead show on the next line
              middle_left.add(scroll); //Add to holderThis is how I take inputs that will go into the JTextArea
         // Sets the text displayed on the display area
         public void setDisplayArea(String message){
              display.setText(""+message);
         I must defently be missing something, can you please help me figure out what it is.
    This is part of an assignment, please do not spoonfeed me with code.
    Regards
    Martin

    onslow77 wrote:
    Yes the problem went away when I comment out this line //scroll.setLayout(null);
    Why is it so?
    Can you please explain or guide me to a webpage with an explonation if it is alot to explain.Like abillconsl said, a JScrollPane already has a perfectly good layout manager. So instead it's up to you to explain why you decided to make it have a null layout manager instead.
    If the explanation is "I don't know anything about layout managers so I can't explain that, it just seemed like a good idea at the time" then you should read the [Layout Managers tutorial|http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html].

  • How to move the scrollBar to JTextArea's top?

    I defined a JTextArea inside a JScrollPane, then I read a .txt file through a Reader into that JTextArea (by setText(...)). Now the scroll bar at bottom. I need move the vertical scrollBar to the top.
    Please help me to do this.
    Thanks

    you can use the method setCaretPosition in JTextArea
    and pass the posotion in the text to place the Caret

  • 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

  • Adding Image to JTextArea

    Hello All,
    I need to add an Image to a JTextArea. Any ideas plz.
    best wishes
    Issac

    But I have read that all Swing Components, which extend from JComponent >can have other components added to them, because the JComponent class >extends from java.awt.Container and thus have a add method. I tried using t>he add method, but it didnt work. So I tried it here.Don't believe everything you read!!!!! JTextArea is meant to be a simple editor and is not meant to handle what you are trying to do.

  • Adding actionlistener to JTextArea

    I'm trying to add an actionListener to my JTextArea, so that when the user hits 'enter', the action occurs.
    sort of like this:
    JTextArea.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ??????) {
    doSomethingToTextArea();
    but I'm having problems (e.g. how to make enter the action event, and some problem that says JTextArea can't use actionlisteners???)
    any ideas?
    thanks,
    n00bProgrammer

    Like serveral of the more complex swing gadgets the JTextarea uses model/view architecture. In this case the model behind the textarea is a Document, and the text change listener needs to be added to the Document, not the gadget itself. i.e. area.getModel().addDocumentListener();
    However JTextArea still inherits the methods of java.awt.Component including addKeyListener();

  • 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

  • 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);

  • 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.

  • Scrollbars In JTextArea

    Please see the link below for visual guide.
    http://pollscanada.com/screenshot.html
    I have my TextArea in the middle of the application which displays film titles when the user clicks on a category in the JComboBox further down on the left where it has the entry Science Fiction.
    When the user clicks on another category, the scrollbars stay at the same level, so the the user does not see the top of the next chosen category list.
    Is there a way to make the srollbars go back to the top after clicking on another entry in the JComboBox?

    Thank you for your response. It works.
       public void setTitleList(String file_name) {
         try {
           FileReader fileReader = new FileReader(file_name);
                list.read(fileReader, file_name);
               scroller.getVerticalScrollBar().setValue(0);
               } catch(FileNotFoundException fex) {}
                    catch(IOException ioex ) {}
      }

  • 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.

  • Problem in adding scrollbars

    I am creating an image editor in java and my main class extends jframe. Now the problem arrises when i load an image gretaer than the frame size.
    how should i add jscrollpane....
    plzz help...

    how should i add jscrollpane....Add your component to a JScrollPane and add the scroll pane to the frame. The scrollbar appear automatically when the preffered size of the component is greater than the size of the scroll pane.

  • Adding JButton to JTextAreas

    I'm publishing reports from data onto an uneditable JTextArea (so the user can save it as a text doc if they wish).
    I'd like to have a button next to each report so the user can view the data for that report, but presumably this can't be done on a JTextArea?
    Anyone have any suggestions as to how I can have a 'View data' button, but still be able to save the text of the report?
    Many thanks.

    If you are trying to produce a panel that has a JTextArea and a JButton on it, that is relatively easy to do.
    You cannot put a JButton into a text area but it is easy enough to put it either next to or below the text area and provide the functionality that you are looking for.

Maybe you are looking for