Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: JTextPane add text from bottom upwards

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    22
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default JTextPane add text from bottom upwards

    Is there a way to have the text in a JTextPane start at the bottom of the pane and get shifted up the screen instead of down the screen when new text comes in?

    I'm trying to create a chat room. I am using a JTextField to pass text into the JTextPane. Everything works as desired except that I can't find any documentation or method that starts adding text to the bottom of a JTextPane instead of the top. I want the most recent line to always be the bottom line.

    Is this possible with JTextPane's? I've searched for hours for an answer to this and can't find the solution or figure out how to achieve this.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,778
    My Mood
    Mellow
    Thanks
    175
    Thanked 624 Times in 611 Posts

    Default Re: JTextPane add text from bottom upwards

    JTextArea has an insert() method that allows text to be added at the desired index. You could check the source code for the JTextArea insert() method and write something similar for JTextPane, or you could use a JTextArea.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    22
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: JTextPane add text from bottom upwards

    After spending about 10 hours on this, I think I finally figured it out, and of course it was so simple.

    I had to add a layout manager to the JTextPane. I used BorderLayout.SOUTH on the TextPane and viola, it was adding text to the bottom.

    At first, I tried adding BorderLayout.SOUTH to the JScrollPane, but that threw an error (because it has its own special layout). Besides, I wanted the JScrollPane to have a BorderLayout.CENTER and a JTextField below it to have BorderLayout.SOUTH. So I had to create a Panel with only the TextPane inside it and apply the layout to the TextPane, like so...

    panel.add(textPane, BorderLayout.SOUTH);

    The only other minor issue was that now the background of the TextPane and Panel were different and as the text scrolled up the screen, you could see that they had slightly different color backgrounds (240,240,240) vs (255,255,255), so I had to set the background color of both to the same color.

    In this example, I gave different background colors to the Panel and TextPane to make the background issue I mentioned above easier to see. If you take them out, you will notice the minor issue I was talking about. Of course, I will have them both the same color in my actual code.

    import java.awt.*;  
    import javax.swing.*;  
     
    public class TextPaneBottom extends JFrame  
    {  
        public TextPaneBottom()  
        {  
            JTextPane textPane = new JTextPane();  
            JPanel panel = new JPanel(new BorderLayout());  
     
            panel.setBackground(Color.red);
            textPane.setBackground(Color.yellow);
     
            panel.add(textPane, BorderLayout.SOUTH);  
            add(new JScrollPane(panel));  
        }  
     
        public static void main(String[] args)  
        {  
            TextPaneBottom frame = new TextPaneBottom();  
            frame.setDefaultCloseOperation(EXIT_ON_CLOSE);  
            frame.setSize(400,200);  
            frame.setLocationRelativeTo(null);  
            frame.setVisible(true);  
        }  
    }

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,778
    My Mood
    Mellow
    Thanks
    175
    Thanked 624 Times in 611 Posts

    Default Re: JTextPane add text from bottom upwards

    I don't know why you're making this so hard. Why not use JTextArea and its append() method?

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    22
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: JTextPane add text from bottom upwards

    Because you can't use Stylized text in JTextArea.

Similar Threads

  1. How to add multiple jTables (components) to jTextPane or JEditorPane
    By anchal87mca@gmail.com in forum AWT / Java Swing
    Replies: 1
    Last Post: September 19th, 2013, 09:01 AM
  2. Add shadow for text in JTextArea
    By startas in forum AWT / Java Swing
    Replies: 7
    Last Post: September 17th, 2013, 02:55 PM
  3. JTextPane changing text color issues
    By donnierisk in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 30th, 2012, 11:29 AM
  4. Replies: 5
    Last Post: April 7th, 2011, 11:22 AM
  5. Replies: 3
    Last Post: April 20th, 2009, 08:35 AM