Creating a JTextArea Scroll

Does anyone see a problem with my code that could prevent JTextArea from becoming scrollable when requesting data that extends its boundaries, it also seems that some words are cut in half, but JScrollPane, which supposedly should fix the problem, does not work. Thanks for the help. guiprojj package;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;

import javax.swing.*;

import com.eclipsesource.json.JsonObject;
import com.google.gson.JsonParser;
import com.json.parsers.JSONParser;
import com.json.parsers.JsonParserFactory;


public class gui {
    public static void main(String[] args)
    {
        JFrame maingui = new JFrame("Gui");
        JButton enter = new JButton("Enter");
        final JTextArea movieinfo = new JTextArea(5,20);
        final JTextField movietext = new JTextField(16);
        final JScrollPane scrolll = new JScrollPane(movieinfo);
        JPanel pangui = new JPanel();
        pangui.add(movietext);
        pangui.add(enter);
        scrolll.add(movieinfo);
        pangui.add(movieinfo);
        maingui.setResizable(false);
        maingui.setVisible(true);
        movieinfo.setLineWrap(true);
        movieinfo.setEditable(false);
        maingui.add(pangui);
        scrolll.getPreferredSize();
        //pangui.setPreferredSize(new Dimension(300, 150));
        //pangui.add(scrolll, BorderLayout.CENTER);
        //movieinfo.add(scrolll);
        maingui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        maingui.pack();
        enter.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)  
            {
                System.out.println(Test.getMovieInfo(movietext.getText()));
                 JsonParserFactory factory=JsonParserFactory.getInstance();
                 JSONParser parser=factory.newJsonParser();
                 Map jsonData=parser.parseJson(Test.getMovieInfo(movietext.getText()));
                 String Title = (String)jsonData.get("Title");
                 String Year = (String)jsonData.get("Year");
                 String Plot = (String)jsonData.get("Plot");
                 movieinfo.setText("Title: "+Title+"\nYear: "+ Year +"\nPlot: "+Plot);
            }
            });

        }
}
+4
source share
2 answers
final JTextArea movieinfo = new JTextArea(5,20);
final JScrollPane scrolll = new JScrollPane(movieinfo);

JPanel pangui = new JPanel();
//scrolll.add(movieinfo);
//pangui.add(movieinfo);
pangui.add(scroll);

A component can have only one parent element. You create a scrollpane with a good text area. But then you add the text area to the panel, which is bad because the text area is removed from the scroll).

scrollpane , , .

+9

, ScrollPane.

JTextArea ta = new JTextArea();
JScrollPane sp = new JScrollPane(ta);
0

Source: https://habr.com/ru/post/1502679/


All Articles