I have a set of JPanel's within a JFrame. One of the panels contains a JTextArea. At the moment I create this like so:
JTextArea = new JTextArea(5, 40);
And this gives me a text area which is 5 rows by (roughly) 40 columns.
Vertically this works as I'd like it to, the area fills the entire height of the parent container - probably because the parent is the only element positioned in that row.
Horizontally the parent width is determined by elements underneath and it is (usually) wider than the JTextArea is. So I end up with a text area with large margins on either side. What is worse, when I resize the frame smaller to the point where the text area is exactly the width of the parent container, it suddenly 'flicks' and changes into a text area that is 1 row high and is then the width of the parent.
Excuse the crude drawing below which hopefully illustrates the issue.
In short: How to I create a JTextArea that always fills the maximum space available to it? (and if possible with a minimum width after which a scrollbar appears if the user sizes the frame even smaller)
In the parent container of the JTextArea (denoted as Panel 1 in your drawing), call the function:
panel1.setLayout(new BorderLayout());
For reference, see this documentation page:
https://docs.oracle.com/javase/7/docs/api/java/awt/BorderLayout.html
As you only have a single child in panel1, the BorderLayout layout manager of panel1 will by default stretch the text area to use all available space in the parent container.
You may want to take away the constructor parameters specifying the size of your TextArea. The BorderLayout should take care of sizes for you :)
You can request that Swing respects a certain minimum size for the text area by calling:
textArea.setMinimumSize(new Dimension(minimum_width, minimum_height));
You have to use layout manager, for start see oficial Oracle docu about layout managers. For your situation, BorderLayout or GridBagLayout should work fine.
Start with:
panel1.setLayout(new BorderLayout());
or
panel1.setLayout(new GridBagLayout());
With GridBagLayout you can more preciselly do layouting (with BorderLayout you have five areas - no more, no less). With GridBagLayout you can do more complicated layouts.
Related
I've a JFrame with multiple panels. One of those should contain a JTextArea of a given size, that can scroll if the text exceeds the area, and that resizes acording to the frame size.
The thing is that when the panel is an instance of JPanel, it looks as I'd like to both in window or fullscreen size, as long as the content doesn't exceed the current area, when it does, it takes the space given to the other components, making them shrink in the frame (here it should show scrollbars instead).
On the other hand, when I use a JScrollPane, I can't manage to make it have the same behaviour of the JPanel, keeping the proper size. Actually it seem to adjust to it's content, and since it's empty, it's just some pixels wide.
How can I achieve what I'm looking for?
Thanks in advance
Following the code that keeps the right size (JPanel):
add(new JPanel(){
{
input = new JTextArea(20,20);
input.setLineWrap(true);
input.setBackground(Color.WHITE);
input.setBorder(cb);
this.setLayout(new BorderLayout());
//this.setPreferredSize(new Dimension(20,20));
this.setBorder(eb);
this.add(input);
}
});
Don't add the text area to the frame.
this.add(input);
Instead you need to add a JScrollPane containing the text area to the frame:
this.add(new JScrollPane(input));
So I'm making a program as a project for school.
In this program I have a panel inside a scrollpane.
When I click a button a panel with info is added to the panel inside the scrollpane.
I can keep adding as much of these panels as I want.
I set the layout of the panel to FlowLayout.
I disabled the horizontal scrolling and set the width of the panel to the width of the scrollpane so I neatly get two of those 'forms' next to each other before it starts a new row of panels.
Problem is the vertical scrolling doesn't activate so I can see only 1,5 rows of panels. (see picture)
http://i1281.photobucket.com/albums/a518/Bas_Van_den_Steen/Screenshot2014-05-22191813_zps44483b9b.png
I suspect this has something to do with the height of the main panel I had to define if I wanted to set a width.
Ideally there should be an option to set the height to 'automatic', but there isn't.
I know scrolling works because when I enable horizontal scrolling and don't set any dimensions for the panel it just keeps adding forms in a single row which I can scroll through.
I think I might need to use another LayoutManager (but I don't have any experience setting those up) or change some of the settings of the scrollpane or main panel.
Can someone help me with this?
I set the width of the panel to the width of the scrollpane so I neatly get two of those 'forms' next to each other before it starts a new row of panels. Problem is the vertical scrolling doesn't activate
A FlowLayout is designed to display components horizontally and the preferred size is always based on a single row of components.
Use a different layout manager. Maybe a vertical BoxLayout, or GridBagLayout or GridLayout depending on your exact requirement.
Read the section from the Swing tutorial on Using Layout Managers for more information and working examples.
set the width of the panel to the width of the scrollpane
You should not be manually setting the preferred width of you panel. As I mentioned earlier that is the job of the layout manager. Maybe the GridLayout is closes to what you need.
I need to make a fixed sized for a GridLayout with 100 buttons located in the center portion of a BorderLayout. On the east portion of the border layout is another Gridlayout that keeps shrinking the center component whenever the text is longer then the size of the current JTextAreas located in the east. The JFrame is not resizable also.
Is there a way to get a fixed size for the center component while allowing the JTextArea to still expand?
"I need to make a fixed sized for a GridLayout with 100 buttons located in the center portion of a BorderLayout".
Sorry, but that's not going to work. BorderLayout doesn't work like that. You can nest JPanel containers with different Layout managers to get your desired effect.
"Gridlayout that keeps shrinking the center component whenever the text is longer then the size of the current JTextAreas located in the east."
You should wrap your text area in a JScrollPane, and setLineWrap(true) and setWrapStyleWord(true) on you text area. The last two will set it, so that the line typed wraps when it is reaching the right edge of the text area. Also If you are setting the size to the text area, don't. Instead, use the following constructor to set its size
JTextArea jta = new JTextArea(20, 50); <--- rows, and character columns
jta.setLineWrap(true);
jta.setWrapStyleWord(true);
JScrollPane scroll = new JScrollPane(jta);
container.add(scroll); <--- make sure you don add jta anywhere else
Without more context to your querstion, these are really the only valid suggestions I can make.
I have a JTabbedPane with a Border Layout.
Here's the code I'm using to add the components:
add(columnNames, BorderLayout.NORTH);
add(scroll, BorderLayout.CENTER);
add(useCtrl, BorderLayout.SOUTH);
setVisible(true);
Question:
Notice the excess whitespace to the right inside the JScrollPane. I don't want that there. I would like for the JScrollPane not to change size at all when changing the size of the JFrame. I have tried setSize() and setPreferredSize(), but the size of the JScrollPane always changes. I've tried using GridLayout, but I get the same result.
Place the JScrollPane in a JPanel with another layout. (e.g. BoxLayout or GridBagLayout). And add the JPanel to the center.
The size of a graphics object is controlled by the layout manager. The BorderLayout will always expand the CENTER object to take up all available space. GridLayout expands all it's children proportionally. If you try a GridBagLayout and set the weightx to 0, that will prevent expansion horizontally.
There are a lot of layout managers available, browse the API for more choices and experiment until you find the resizing behavior you want. Each has a fairly good explanation of how it works in the javadoc.
I created a JFrame initialized with a BorderLayout and a JScrollPane as its CENTER element.
The scroll pane is set with VERTICAL_SCROLLBAR_ALWAYS and HORIZONTAL_SCROLLBAR_NEVER policies. The intent of my frame is to have a controlled width, while the height should grow/shrink as data is added/removed.
Inside my scroll pane, I added a simple JPanel (lets call it the content panel) which is initialized with a FlowLayout (and LEADING policy).
In order to test this, I simply populate my content panel with 20 JLabel("Item " + n) components where n is the loop counter.
I would expect to see my labels shown on a single row if the frame is large enough and the labels wrap to other lines when I shrink the width. But instead, there is only a single line displayed with no wrapping... ever.
Does anyone know why the flow layout does not wrap when a scroll pane is involved?
If I remove the scroll pane all together and put the content panel directly in the frame, the desired wrapping effect occurs, but if the frame height is shrunk smaller than the content panel height it just disappears.
The idea is that I want my labels to be wrapped when necessary but also always be visible if it means having to scroll up/down.
Any suggestions on how to fix this?
Thanks.
Wrap Layout gives an explanation and a solution.
If you work with the designer, you have to set the prefferedSize property to null (delete what is set) then set the preferred size by clicking the triple dots [...] button next to the prefferedsize property name and put your preferred value.
I encountered the same problem and it works for me.