简体   繁体   中英

JTextArea wrapping only once at the creation of the text area

I have a JTextArea with

text.setLineWrap(true);
text.setWrapStyleWord(true);

Now I have the problem, that if I start the GUI containing some of this JTextArea the text is correctly wrapped into 3-4 lines. Now I resize the GUI to the rigth and the text is correctly expanded and just wrapped down to 1-2 lines. Now I start resizing the GUI back to the left, but the JTextArea's don't wrap back to the old state. They just stay wrapped to the 1-2 lines.

What kind of layout you are using? You need to use one that fits on the size of the window.

public static void main(String[] args) {
    StringBuilder sb = new StringBuilder();
    Locale[] locales = Locale.getAvailableLocales();
    for (int i = 0; i < locales.length; i++) {
        sb.append(locales[i].getDisplayCountry()).append(' ');
    }

    JTextArea textArea = new JTextArea(sb.toString());
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setViewportView(textArea);

    JFrame frame = new JFrame("All installed locales");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.add(scrollPane);
    frame.pack();
    frame.setVisible(true);
}

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