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

Similar Messages

  • How to Add Horizontal scrollbar to JTextPane

    when i try to add Horizontal scrollbar to JTextPane it is not apperaing..plase help me..
    nagesh

    I tried in all ways
    1)HORIZONTAL_SCROLLBAR_ALWAYS and VERTICAL_SCROLLBAR_ALWAYS
    2)JTextPane textPane = new JTextPane();
    JScollPane scroller = new JScrollPane(textPane);
    or :
    3)JScollPane scroller = new JScrollPane(textPane, VERTICAL_SCROLLBAR_ALWAYS, HORIZONTAL_SCROLLBAR_ALWAYS);
    4)JScrollPane.setHorizontalScrollBarPolicy(
                   ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
                   JScrollPane.setVerticalScrollBarPolicy(
                   ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    and also we tried to overwrite the setbounds also, but nothing is diplaying..

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

  • 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 text to JTextPane

    I've go a JTextPane placed on a JScrollPane, when I add a lot of text on it, the vertical scrollbar is going to the bottom, I want that TextPane view the top of the text automatically, how can I do this ? I tried to move the vertical scrollbar but it returns to the bottom
    sorry for my english :)

    Questions like this have been asked and answered dozens of times, although I must admit that most can't figure out how to get the text to scroll automatically. But the concept is the same. You get one freebie answer.
    textComponent.setCaretPosition( 0);Next time search the forum before posting.

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

  • Problem adding Icon to JTextPane

    I want to add an image to my JTextPane but it won't work. First i thought it was something with my path of my file but then i tried with a JFileChooser so i could chose the file so i'm sure that the file is good. But still, i don't get the image on my JTextPane. This is the code:
    File file = chooser.getSelectedFile();
    Icon icon2 = new ImageIcon (file.getAbsolutePath());
    textPane.insertIcon (icon2);I don't know if this would help but i'm using a lot of different SimpleAttributeSets
    My textPane is dissabled, can it be that?

    I have found that the problem is with the setEnable.
    When it is disabled i can't add an Icon but when it is enabled, i can.
    When it is disabled, then i do: textPane.setEnabled(true) and then add an icon, it also won't work. Not even when i set the carretPosition.
    How can i add an icon AND still be disabled?

  • 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 Highlight in JTextPane ?

    Hi All,
    I have a interface where i can display a html page.
    Now i want to highlight few text from the displayed page in any color.
    Can anyone tell me how can do that ?
    I am trying to make a demo of search where user enters some key words and i wanna highlight all the words in the page.
    Is it possible ?
    Hopeing for a favourable response.
    Kind Regards
    Chetan Chandarana

    You might be able to use a Highlighter. Here is some code I copied from another thread that gives you an idea how to use a Highlighter.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.text.*;
    import java.util.*;
    public class HighlightTest
         public static void main(String[] arguments)
         JFrame f = new JFrame("3plus4 HLM test");
         JEditorPane e =
         new JEditorPane("text/plain", "hfhk shhdf kdsh kshfdsh fh fkshfhs fh fhf dhf hfhf hfd hfdf ");
         f.getContentPane().add(new JScrollPane(e));
         HLM h = new HLM(e);
         h.addHighlight(3, 5);
         h.addHighlight(9, 12);
         h.addHighlight(16, 22);
         f.pack();
         f.setSize(200, 200);
         f.show();
         static class HLM implements CaretListener
              static class HighlightInterval
                   int p0, p1;
                   Object tag;
                   HighlightInterval(int p0, int p1, Object tag)
                        this.p0 = p0;
                        this.p1 = p1;
                        this.tag = tag;
                   boolean contains(int dot)
                        return dot >= p0 && dot < p1;
              ArrayList highlights = new ArrayList();
              Highlighter h = new DefaultHighlighter();
              Highlighter.HighlightPainter p = new DefaultHighlighter.DefaultHighlightPainter(Color.yellow);
              HLM(JEditorPane editor)
                   editor.setHighlighter(h);
                   editor.addCaretListener(this);
              public void caretUpdate(CaretEvent e)
                   for (Iterator i = highlights.iterator(); i.hasNext();)
                        HighlightInterval hi = (HighlightInterval) i.next();
                        if (hi.contains(e.getDot()))
                             h.removeHighlight(hi.tag);
                             i.remove();
                             break;
              public void addHighlight(int p0, int p1)
                   try
                        Object tag = h.addHighlight(p0, p1, p);
                        highlights.add(new HighlightInterval(p0, p1, tag));
                   catch (BadLocationException e)
                        e.printStackTrace();
    }

  • Removing scrollbars on the JSP page of frames created by included JSP files

    Hi All,
             In the iternet sales B2B application which we are developing, has a blank JSP file. Wherein we are adding other JSP files on it, loading trough JS.
    When we are adding it is making the various jsp files in the form of the frames, and each frame has different JSP page. So it is adding scrollbars on each frame if the size of the page exceeds the size of frame.
    we don't want the scrollbars. Kindly suggest to remove these scrollbars, and all the pages should look as a single JSP page.
    Thanks & Regards
    EKta

    Hi
    set scrolling="no" while creating frames
    Murali.K.N

  • HTMLLoader Scrollbars

    I'm working in Flash and have added an HTMLLoader to a
    movieclip via actionscript. I can't seem to figure out how to add
    scrollbars. The content is clearly bigger than the window, and the
    content scrolls if you drag it.
    What I've ended up doing is adding the HTMLLoader to a
    ScrollPane. It's working, but I'm guessing at the height in order
    to show the vertical scrollbar.
    I feel like I'm missing something here. Is there a better way
    to do this?
    Dave

    You're probably looking for these:
    HTMLLoader.contentWidth
    HTMLLoader.contentHeight
    which will give you the width/height of the content in your
    HTMLLoader, rather than the dimensions of the HTMLLoader object
    itself.
    In my experience, mousewheel vertical scrolling seems to be
    enabled by default for HTMLLoaders. Adding scrollbar functionality
    is just a case of using your content height/width values in
    conjuction with your scrollbar dimensions, which can be updated
    with an
    Event.MOUSE_MOVE event listerner, initiated after an
    Event.MOUSE_DOWN event on your scroll button.

  • Creating a scrollback buffer using a JTextPane

    Hi all, I've run into a problem with an application I'm developing, and I'm hoping to find some help here. This is my first crack at a designing anything worthwhile using Swing, so please bear with me. :)
    My problem: I need a buffer to store a certain number of lines of text, then when the maximum is reached, start removing the oldest lines. eg) max of 10 lines, when line 11 is received, line 1 is removed.
    The JTextPane is contained in a JScrollPane, so the actual scrolling is taken care of.
    Also, somewhat related, how can I force the JScrollPane to stay at the "bottom" of the pane? When new data is received from the server, it's added to the JTextPane, but I can't seem to find a way to force it to stick to the bottom.
    Any help will be greatly appreciated. :)

    Could you use the system line terminator to determine how many lines of text are in the JTextPane? If so, you can simply find the first line, and remove it, then re-set it back into the JTextPane. A StringBuffer would probably be beneficial for the manipulation.
    For the JScrollPane you could try using the getVerticalScrollBar() method to get the vertical scroll bar, then set the it's value to it's maximum value, to try and force it to the bottom of the scrolling region.

Maybe you are looking for

  • How do I copy an App from my wife's phone to mine

    My wife has a free App from a 3rd party authorised provider that I would find very useful. Its a gateway to a government work related suite of password secured websites. She can't remember where it came from and no amount of searching can find it. An

  • Help home button suddenly not working properly after io5 update!!

    having finally got ios 5 after multiple attempts now a few days in the home button has decided to have a mid of its own? in app...take 5-10 presses to exit app?? home screen double tap to show appswitcher sometimes just doesnt happen was all fine til

  • How to include components into mxml file?

    I have an mxml file with multiple states and it is getting quite large. I would like to divide these states into their own components. How can i include these into the mxml file? Thanks!

  • Lots of problems with 10.9.2 upgrade

    How do I fix all of these problems: Upgraded today to 10.9.2 - No sound  no sound device sound internally or externally.  Sound comes at startup before operating system is loaded so I am assuming it's an OSX issue ` No Flash video  All I get are blac

  • FIRE WIRE WHAT IS IT??

    HOW DO I KNOW IF MY PC HAS ONE..??