1

I am looking for a way to create a window (with Jpanel or whatever), get a certain amount of text off of a website (this part is working), and display it in a text field. I know this seems fairly simple but i am new at this. Thanks! So far I got this example from the Jsoup website:

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class closings {

public static void main(String[] args) throws IOException {

    Document document = Jsoup.connect("http://www.wjla.com/weather/virginia-school-closings-delays/").get();
    Elements tags = document.select("#closingsList");

    for (Element tag : tags) {
        System.out.println(tag.text());
    }
}}
Somelizard
  • 21
  • 1
  • 3
  • 2
    [Creating a GUI With JFC/Swing](http://docs.oracle.com/javase/tutorial/uiswing/), [How to Use Text Fields](http://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html), [How to Use Text Areas](http://docs.oracle.com/javase/tutorial/uiswing/components/textarea.html), [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/), [Worker Threads and SwingWorker](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html) – MadProgrammer Mar 03 '15 at 23:17
  • You can see: http://stackoverflow.com/questions/25391685/jsoup-get-value-before-executing-form-post – VictorQ_88 Mar 03 '15 at 23:40

2 Answers2

2

Assuming that your jsoup works and that your for each loop prints out the tags that you need.... I have written up some code that prints the contents of an arraylist to a TextArea using Java swing. The only thing that I did different from you is use Strings instead of Elements (because I dont have the jsoup library downloaded.

class stackExchangeHelp

package stackExchangeHelp;

import java.util.ArrayList;

public class stackExchangeHelp
{
    public static void main(String[] args)
    {
        //this should be a list of elements (not strings)
        ArrayList<String> listToSend = new ArrayList<String>();

        //use your for each loop to add elements to the list
        listToSend.add("First element");
        listToSend.add("Second Element");
        listToSend.add("Third Element");

            DisplayGuiHelp gui = new DisplayGuiHelp(listToSend);
    }
}

class DisplayGuiHelp

package stackExchangeHelp;

import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextPane;

public class DisplayGuiHelp
{
    public DisplayGuiHelp(ArrayList<String> list) //constructor of the DisplayGuiHelp object that has the list passed to it on creation
    {
        final JFrame theFrame = new JFrame();
        theFrame.setTitle("Stack exchange help");
        theFrame.setSize(500, 500);
        theFrame.setLocation(550, 400);

        JPanel mainPanel = new JPanel();

        JTextArea theText = new JTextArea(5,25); //create the text area

        for(String text : list)
        {
            theText.append(text + "\n"); //append the contents of the array list to the text area

        }
        mainPanel.add(theText); //add the text area to the panel

        theFrame.getContentPane().add(mainPanel); //add the panel to the frame
        theFrame.pack();
        theFrame.setVisible(true);

    }
}

Let me know if you have any questions or would like me to expand upon it more.

Tango199
  • 52
  • 1
  • 6
1

You can create a JLabel

Step 1: Add this line to your code:

import javax.swing.*;

Step 2: Add this code:

JFrame f = new JFrame();
JPanel p = new JPanel();
JLabel l = new JLabel(tag.text());

p.add(l);
f.add(p);
f.setVisible(true);

You're all set! Enjoy your code!

Lucas Baizer
  • 305
  • 2
  • 5
  • 13
  • 1
    this looks like it will work but i am getting an error at the JLabel l = new JLabel(tag.text()); part. Says "tag cannot be resolved". Also how yould you get the text to output there? seems unclear to me :/ – Somelizard Mar 04 '15 at 22:27