简体   繁体   中英

adding a ScrollPane in a GridLayout doesn't resizes the JTextarea in a JPane when changing size of JFrame

I want to add a grid to the current GridLayout with a filled JTextArea component. It works fine, but as mentioned in the title the JTextArea won't resizes itself after changing the size of frame.

I have already localized the error:

text.setLineWrap(true); //<--- here is the problem!!enter code here

in the method createGui.

Here is my Code:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
class GridLayoutExample {

    private JFrame frame = new JFrame();
    private JPanel panel = new JPanel();
    private JScrollPane pane;
    final JTextArea text = new JTextArea("Click me!\nClick me!\nClick me!\nClick me!\nClick me!");

    GridLayoutExample() { createGui(); } //constrcutor

    private JPanel createGui () {
        panel.setLayout(new GridLayout(0, 2,0,0));          
        panel.add( new JLabel("Hello"));        
        //set style of first row, second column
        text.setLineWrap(true); //<--- here is the problem!!
        text.setEditable(true);
        //enable mouse listening and allow mouse action 
        doMouseActions();
        panel.add(text);
        return panel;
    }

    void addNewRow() {
        panel.add(new JLabel("Test1"));
        panel.add(new JTextField("Test"));
    }//addNewRow

    void showFrame() {
        pane = new JScrollPane(panel);
        frame.add(pane);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }//showframe

    void doMouseActions() {
        text.addMouseListener(new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent arg0) {
                addNewRow();
            }

            @Override
            public void mouseEntered(MouseEvent arg0) {
                text.setBackground(Color.YELLOW);               
            }

            @Override
            public void mouseExited(MouseEvent arg0) {
                text.setBackground(Color.WHITE);                
            }

            @Override
            public void mousePressed(MouseEvent arg0) {}

            @Override
            public void mouseReleased(MouseEvent arg0) {}           
        });
    } // end of mouse action    

    public static void main(String[] args) {
        GridLayoutExample test = new GridLayoutExample();
        test.showFrame();
    }// end of main
}// class GridLayoutExampleenter code here

On the one hand It works fine when I disable line wrapping, on the other hand I need it to get the line breaks.

So any suggestions to do it better?

Many thanks in advance!

You are adding components at run time. In such cases you need to ensure the container gets revalidated:

void addNewRow() {
    panel.add(new JLabel("Test1"));
    panel.add(new JTextField("Test"));
    panel.revalidate();
    panel.repaint();
}//addNewRow

Also, you should create and access swing components only in the event dispatch thread. See here for more.

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