简体   繁体   中英

Add JScrollPane to JTextArea

In my program one user should fill a "Object" field with text of an arbitrary length. So I'd like to create a JTextArea with reasonable dimensions and with an associated JSrollPane in order to read all the inserted text, if it's very long. This is what I've done:

    body.add(new JLabel("OGGETTO"), "1,2");

    JTextArea oggetto = new JTextArea(5,20);
    oggetto.setOpaque(true);
    oggetto.setBackground(Color.cyan);

    Border borderOgg = BorderFactory.createLineBorder(Color.BLACK);
    oggetto.setBorder(BorderFactory.createCompoundBorder(borderOgg,
            BorderFactory.createEmptyBorder(1, 1, 1, 1)));

    oggetto.setLineWrap(true);
    oggetto.setWrapStyleWord(true);

    JScrollPane scroll1 = new JScrollPane(oggetto);
    scroll1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    body.add(scroll1,"3,2");

    body.add(oggetto, "2,2");

where body is a JPanel whose layout is TableLayout. But the scroll doesn't work even if it's showed. Why?

remove below line

body.add(oggetto, "2,2");

because JTextArea is already added in JScrollPane hence there is no need to add it again.

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