简体   繁体   中英

How to resize JTextArea to fit percentage of JFrame

This is definitely a noob question. How do I resize two JTextArea panels so they look something like this:

aaaaaaaaaaaaaa
a   a        a
a   a        a
a   a        a
aaaaaaaaaaaaaa

With the first area about a tenth of the width of the second. I must also enclose this in a new scroll pane, but I've taken care of that. the resize function doesn't seem to be working.

When you create the text areas you use something like:

JTextArea textArea1 = new JTextArea(10, 10);
JTextArea textArea2 = new JTextArea(10, 80);

The two numbers provide a suggestion for the number of rows and characters in each row.

Then you add them to a scroll pane:

JPanel panel = new JPanel();
panel.add(textArea1);
panel.add(textArea2);

JScrollPane scrollPane = new JScrollPane( panel );
frame.add(scrollPane):

The above code will give you fixed size text areas.

Or, if you really want to do it by percentage and allow the text areas to dynamically grow/shrink you would use:

JTextArea textArea1 = new JTextArea(10, 1);
JTextArea textArea2 = new JTextArea(10, 1);

And then add them to a JPanel using a GridBagLayout with the appropriate constraints. You would need to use: 1. the "fill" constraint which would allow the text areas to grow as the space available grows. 2. the "weightx" contstraint. This will allow you to allocate extra space in the percentage that you desire.

Read the section from the Swing tutorial on How to Use GridBagLayout for more information and working examples.

You can use a JSplitPane to split horizontally (or vertically) the two components (text areas). This approach lets the user to freely move the divider (ie the vertical separator of the two areas) to a location he/she prefers.

As for the preference on the allocation of space of each component, you can use the setResizeWeight method which will distribute the new space allocated for the split pane (each time the user resizes the window) according to the value you specify. For example calling this method with a value of 0.5 will distribute the new size equally to both left and right components. A value of 0 will give all the extra space at the right component. A value of 1 will give it to the left component. A value of 1.0 / 3.0 will split the new space to three and then reserve the first third to the left component and the other two thirds to the right component. And so on...

This should be good user-experience, although if you don't want the user to relocate the divider by himself/herself, then use camickr 's answer.

Here's a working example:

import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class Main {
    private static Component buildTextAreaContainer() {
        final JTextArea txt = new JTextArea();
        final JScrollPane scroll = new JScrollPane(txt);
        return scroll;
    }

    public static void main(final String[] args) {
        SwingUtilities.invokeLater(() -> {
            final JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, buildTextAreaContainer(), buildTextAreaContainer());
            //split.setContinuousLayout(true);
            //split.setOneTouchExpandable(true);
            split.setResizeWeight(1d / 3d); //one third for left component, two thirds for right component.
            split.setPreferredSize(new Dimension(1000, 600));

            final JFrame frame = new JFrame("Splitted text areas test.");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(split);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }
}

To learn more about how to use the JSplitPane you can read the corresponding tutorial or the docs themselves.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM