-
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!!
-
So here is an example,
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 );
}
}
In this example, you will see that the text area is fixed at 50, 80. However, if you change "add(tabbedPane);" to "add(scrollPane);", you will see that the text area is the size of the window, and if you make the window smaller than 50, 80 then you will see scrollbars. This is what I want the first one to look like. Thanks very much for the help so far.
-
If you set the JPanel's layout to BorderLayout, does it do what you want? For e.g.,
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);
}
}
-
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:
Code:
// add(tabbedPane);
add(new JScrollPane(tabbedPane));
Now the tabbed pane is scrollable so you can see the entire text area and the text area will become scrollable if you add more than 50 lines of test to it.
-
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?