Results 1 to 6 of 6
  1. #1
    Splat is offline Member
    Join Date
    Oct 2009
    Posts
    21
    Rep Power
    0

    Default How to use JTextArea with JScrollPane?

    Hi all,

    I had a play with this but couldn't get it to work so I am assuming its nothing obvious. How is one supposed to use the JScrollPane with JTextArea so that the text area, if displaying only a portion of its contents, is still accessible by the scroll bar. In other words how to make the scroll bar work as expected.

    I am not even sure that I should be trying to do this so if you have a better, easier, suggestion I am open to this as well.

    Thanks in advance.
    Glenn.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I'm not sure that I understand just what isn't working for you. You may wish to post a small runnable program that shows your attempt at this and then describe how you want it to work and why it's not working.

    Much luck.

  3. #3
    Splat is offline Member
    Join Date
    Oct 2009
    Posts
    21
    Rep Power
    0

    Default

    ok, I've managed to produce a standalone .java file called ScrollingTextArea.java which I have included down here. I am expecting a scroll bar to appear once the text area fills up its displayable area. Fill up the displayable area by clicking on the button above it. However the scrollbar doesn't appear so I am thinking I am doing something wrong and probably completely misunderstanding something. JScrollPane doesn't seem to be doing anything at all.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    
    
    
    public class ScrollingTextArea
    {
    
    	static private class UI implements ActionListener
    	{
    		private final static String newline = "\n";
    		int clickCount = 0;	
    		private JTextArea text_area;
    
    		public UI(Container contentPane)
    		{
    			ConstructUI(contentPane);
    		}
    	
    		public void actionPerformed(ActionEvent e)
    		{
    			text_area.append("The button was pressed " + ++clickCount + " times." + newline);
    	    }
    	
    	
    		private void ConstructUI(Container contentPane)
    		{
    			JPanel top = new JPanel();
    
    			contentPane.setLayout (new GridBagLayout());
    			GridBagConstraints c;
    
    			c = new GridBagConstraints();
    			c.fill = GridBagConstraints.BOTH;
    			c.gridx = 0; c.gridy = 0;
    			c.weightx = 0.0; c.weighty = 0.8;
    	
    			top.setBorder(BorderFactory.createLineBorder(Color.black));
    			contentPane.add(top, c);
    			top.setLayout (new GridBagLayout());
    
    
    			JPanel left_pane = new JPanel();
    			left_pane.setBorder(BorderFactory.createLineBorder(Color.black));
    	
    			c = new GridBagConstraints();
    			c.gridx = 0; c.gridy = 0;
    			c.weightx = 0.8; c.weighty = 0.5;
    			c.fill = GridBagConstraints.HORIZONTAL;
    			c.anchor = GridBagConstraints.PAGE_START;
    	
    			top.add(left_pane, c);
    	
    			left_pane.setLayout(new BoxLayout(left_pane, BoxLayout.Y_AXIS));
    	
    	   		JButton btn = new JButton("Button");
    			btn.setPreferredSize(new Dimension(100,100));
    			btn.addActionListener(this);
    
    			left_pane.add(btn);
    
    			String S = "";
    			S += "***** Welcome to the scrolling text area test *****\n";
    			S += "Click the button to add text to the box\n";
    			text_area = new JTextArea(S);
    			text_area.append(S);
    	        text_area.setEditable(false);
    			text_area.setPreferredSize(new Dimension(350, 200));
    	        JScrollPane scroll_pane = new JScrollPane(text_area);
    	        left_pane.add(scroll_pane);
    		}
    
    	}
    
    	public static void main(String[] args)
    	{
    		javax.swing.SwingUtilities.invokeLater(new Runnable()
    		{
                public void run()
    			{
    				JFrame frame = new JFrame("JavaApp1");
    				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			 	Container contentPane = frame.getContentPane();
    				UI ui = new UI(contentPane);
    		        frame.pack();
    		        frame.setSize(new Dimension(500,400));
    		        frame.setVisible(true);
                }
      		});
    	}
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Ah, no problem. You're setting the preferred size on the wrong component. By setting the jtextarea's preferred size, you're limiting its size. You should be setting the scrollpane instead:
    Java Code:
             //text_area.setPreferredSize(new Dimension(350, 200));
             JScrollPane scroll_pane = new JScrollPane(text_area);
             scroll_pane.setPreferredSize(new Dimension(350, 200));
             left_pane.add(scroll_pane);

  5. #5
    Splat is offline Member
    Join Date
    Oct 2009
    Posts
    21
    Rep Power
    0

    Smile

    Yup, that works, thanks very much.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. jscrollpane
    By kaemonsaionji in forum New To Java
    Replies: 3
    Last Post: 02-25-2009, 08:39 AM
  2. JScrollPane And ArrowKeys
    By arpitvavadia in forum AWT / Swing
    Replies: 2
    Last Post: 09-04-2008, 08:22 PM
  3. [SOLVED] JScrollPane - HELP!
    By terox13 in forum AWT / Swing
    Replies: 8
    Last Post: 05-10-2008, 03:58 AM
  4. JScrollPane with HTML
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-14-2008, 11:32 AM
  5. help with JScrollPane
    By tommy in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 07:58 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •