Results 1 to 6 of 6
- 10-19-2009, 01:19 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 21
- Rep Power
- 0
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.
-
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.
- 10-19-2009, 02:04 AM #3
Member
- Join Date
- Oct 2009
- Posts
- 21
- Rep Power
- 0
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); } }); } }
-
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);
- 10-19-2009, 04:45 AM #5
Member
- Join Date
- Oct 2009
- Posts
- 21
- Rep Power
- 0
Yup, that works, thanks very much.
-
You're quite welcome. Thanks for reporting back.
Similar Threads
-
jscrollpane
By kaemonsaionji in forum New To JavaReplies: 3Last Post: 02-25-2009, 09:39 AM -
JScrollPane And ArrowKeys
By arpitvavadia in forum AWT / SwingReplies: 2Last Post: 09-04-2008, 09:22 PM -
[SOLVED] JScrollPane - HELP!
By terox13 in forum AWT / SwingReplies: 8Last Post: 05-10-2008, 04:58 AM -
JScrollPane with HTML
By Java Tip in forum Java TipReplies: 0Last Post: 03-14-2008, 12:32 PM -
help with JScrollPane
By tommy in forum AWT / SwingReplies: 1Last Post: 08-06-2007, 08:58 PM
Bookmarks