Actually sabzh. In a separate class parsu page, in another I create a GUI. I need to pass the result of the parsing (cycle) to JTextArea. I study the second week for a second, I ask you not to rush with the slippers. Parsing:

import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.util.ArrayList; import java.util.List; public class parseClass { public void go() throws Exception { List<Article> articleList=new ArrayList<>(); Document doc=Jsoup.connect("http://4pda.ru").get(); Elements divelements=doc.getElementsByAttributeValue("itemprop","description"); divelements.forEach(divelement -> { Element pElement=divelement.child(0); // String url=pElement.attr("style"); String title=pElement.text(); articleList.add(new Article(null,title)); }); articleList.forEach(System.out::println); } public ArrayList<String> getTextList(){ ArrayList<String> textList=new ArrayList<String>(); return textList; } } } class Article{ private String url; private String name; public Article(String url, String name) { this.url = url; this.name = name; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Text= "+name + '\n' ; } } 

GUI:

 import javax.swing.*; import javafx.stage.Screen; import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.Label; import java.awt.event.*; public class guiParse { JLabel label; JFrame frame; JTextArea jt; JScrollPane scroll; public void gui() { label=new JLabel("text"); jt=new JTextArea(16,58); jt.setText("kakoy to text"); jt.setEditable(false); frame =new JFrame(); JPanel panel=new JPanel(); scroll= new JScrollPane(jt); panel.setLayout(new FlowLayout()); panel.add(scroll); frame.add(panel); //frame.getContentPane().add(BorderLayout.SOUTH,); frame.setTitle("Future App"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400,400); frame.setVisible(true); } } 

main:

  import javax.swing.*; import java.util.ArrayList; import java.util.List; import javafx.stage.Screen; import java.awt.BorderLayout; import java.awt.Label; import java.awt.event.*; class jSoupTest { public static void main(String[] args) throws Exception { parseClass parse=new parseClass(); parse.go(); guiParse gui=new guiParse(); gui.gui(); } 

}

That is, there is a separate call, I can’t transfer it. I didn’t work with the lists especially, for the first time I apply it, just pass by the String parameter to the gui did not work by itself.

    2 answers 2

    Create a method in parseClass that will return an articleList . Just add:

     return articleList; 

    instead:

     articleList.forEach(System.out::println); 

    In guiParse create a List<Article> articleList and a constructor that takes this list as an input:

     public guiParse(List<Article> articleList){ this.articleList = articleList; } 

    In your main() pass the result of the function articleList to the input of the constructor and read the result from there in the gui() method.

    • Thanks for the farewell, everything works. Tell me, when reading from the constructor, you can send the articleList directly to jt.seText (). When converting to String, everything is OK, I just wonder if there is a direct way - Desu
     class jSoupTest { public static void main(String[] args) throws Exception { new guiParse().gui(new parseClass().go()); } } import java.awt.FlowLayout; import java.util.List; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class guiParse { JLabel label; JFrame frame; JTextArea jt; JScrollPane scroll; public void gui(List <Article> articleList) { label = new JLabel("text"); jt = new JTextArea(16, 58); jt.setText(namesFromArticleList(articleList)); jt.setEditable(false); frame = new JFrame(); JPanel panel = new JPanel(); scroll = new JScrollPane(jt); panel.setLayout(new FlowLayout()); panel.add(scroll); frame.add(panel); //frame.getContentPane().add(BorderLayout.SOUTH,); frame.setTitle("Future App"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 400); frame.setVisible(true); } public String namesFromArticleList(List <Article> articleList) { StringBuilder sb = new StringBuilder(); for (Article article : articleList) sb.append(article.getName()).append("\n"); return sb.toString(); } } class Article { private String url; private String name; public Article(String url, String name) { this.url = url; this.name = name; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Text= " + name + '\n'; } } import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.util.ArrayList; import java.util.List; public class parseClass { public List<Article> go() throws Exception { List<Article> articleList = new ArrayList<>(); Document doc = Jsoup.connect("http://4pda.ru").get(); Elements divelements = doc.getElementsByAttributeValue("itemprop", "description"); divelements.forEach(divelement -> { Element pElement = divelement.child(0); // String url=pElement.attr("style"); String title = pElement.text(); articleList.add(new Article(null, title)); }); return articleList; } } 
    • The size of the text box is already customized. And remember that for the naming of classes with a small letter hit long and hard. and also, if it makes sense to carry a variable as a class variable, then in 99.9 percent of cases it should be private with the modifier. and if access is needed from the outside, then through getters and setters. - Dmitriy