Results 1 to 8 of 8
Thread: text area size problem
- 11-08-2009, 11:52 PM #1
Senior Member
- Join Date
- Nov 2009
- Posts
- 236
- Rep Power
- 12
text area size problem
Hello, I'm making a gui that has a JTextArea inside a JScrollPane inside a JPanel. When I didn't have the JPanel it would always make the JTextArea the size of the window, but when I have them inside the JPanel, the JTextArea will stay the preferred size and the JScrollPane wouldn't work. I want to make it so I can have the JPanel and make it so that the JScrollPane is the same size as the window, and if there is too much text, the scrollbars will appear. Please help, thanks.
-
I think that your problem is with layout managers. Are you using any layout managers here, and if so which ones? JPanel by default uses FlowLayout which might not be the best fit for your purposes. Perhaps a Borderlayout is what you want, and then you can add the JScrollPane to the JPanel in the BorderLayout.CENTER position.
If this doesn't help, then your best bet is to post a small compilable program that demonstrates your problem and lets us play with your code, an SSCCE.
Much luck!
-
Oh, and before I forget, welcome to the forum!!
- 11-09-2009, 12:46 AM #4
Senior Member
- Join Date
- Nov 2009
- Posts
- 236
- Rep Power
- 12
So here is an example,
Java Code:import java.awt.*; import javax.swing.*; public class Resize extends JFrame { JPanel pane = new JPanel(false); JScrollPane scrollPane; JTextArea text = new JTextArea(50, 80); JTabbedPane tabbedPane = new JTabbedPane(); public Resize() { scrollPane = new JScrollPane(text); pane.add(scrollPane, BorderLayout.CENTER); tabbedPane.addTab("Resize", null, pane, ""); add(tabbedPane); } public static void main( String args[] ) { final Resize window = new Resize(); window.setSize( 1680, 1050 ); window.setVisible( true ); } }
-
If you set the JPanel's layout to BorderLayout, does it do what you want? For e.g.,
Java Code:import java.awt.*; import javax.swing.*; public class Resize extends JFrame { JPanel pane = new JPanel(false); JScrollPane scrollPane; JTextArea text = new JTextArea(50, 80); JTabbedPane tabbedPane = new JTabbedPane(); public Resize() { pane.setLayout(new BorderLayout()); // set the JPanel's layout to BorderLayout scrollPane = new JScrollPane(text); pane.add(scrollPane, BorderLayout.CENTER); tabbedPane.addTab("Resize", null, pane, ""); add(tabbedPane); } public static void main(String args[]) { final Resize window = new Resize(); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setSize(1680, 1050); window.setVisible(true); } }
- 11-09-2009, 02:02 AM #6
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,236
- Rep Power
- 13
Well, I'm not sure you can do what you want.
Scrollbars appear automatically when the preferred size of the component added to the scrollpane is greater than the size of the scrollpane.
When you add the scrollpane directly to the frame, then the size of the scrollpane gets changed to reflect the space available. Therefore scrollbars appear as required.
When you add the scrollpane to the tabbed pane, the size of the scrollpane doesn't change, only the size of the tabbed pane changes, so no scrollbars appear.
The only solution I can think of is:
Java Code:// add(tabbedPane); add(new JScrollPane(tabbedPane));
- 11-09-2009, 02:11 AM #7
Senior Member
- Join Date
- Nov 2009
- Posts
- 236
- Rep Power
- 12
Thank you very much Fubarable!! I've been trying to find a solution to my problem for about a week and it was so simple. Thanks, this is exactly what I wanted.
-
You're quite welcome. There's one thing I'm curious about though: why do you turn double buffering off in the JPanel, pane?
Similar Threads
-
eol in text area....
By Nicholas Jordan in forum NetworkingReplies: 0Last Post: 09-14-2008, 10:59 PM -
[SOLVED] Text Area new line problem
By Jay-1.1 in forum New To JavaReplies: 3Last Post: 05-07-2008, 04:18 AM -
Text Area problem
By mcal in forum New To JavaReplies: 0Last Post: 02-11-2008, 09:42 PM -
Displaying data into text area
By abhiN in forum New To JavaReplies: 1Last Post: 01-22-2008, 10:30 AM -
textfiled and text area to UTF-16?
By Mr tuition in forum AWT / SwingReplies: 0Last Post: 12-04-2007, 12:40 PM
Bookmarks