简体   繁体   中英

Why JTextArea doesn't work with JPanel?

Why when I add JTextArea to JPanel it doesn't work? When I use JButton instead of JTextArea everything works corectly. Why doesn't JTextArea work with JPanel but with JFrame does?

public class Searching extends JPanel {

    private JPanel searchPanel;
    private JTextArea addMedicament;

    public Searching(){
        searchPanel = new JPanel();
        searchPanel.setLayout(new GridLayout(1,1));
        setBackground(Color.BLUE);

        addMedicament = new  JTextArea();
        searchPanel.add(addMedicament);

        this.add(searchPanel);
    }

}

A text area will work fine with a panel.

Try creating the text area as follows:

JTextArea textArea = new JTextArea(5, 20);
JScrollPane = new JScrollPane( textArea );
panel.add( scrollPane );

Now the text area will be created with a preferred size. As the data is changed scrollbars will appear/disappear as required because the problem is with your code and the context of how you use your code, not the panel or text area.

If this doesn't help then post a proper SSCCE that demonstrates the problem.

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