0

When I print out the raw text from a website, it will only put one line of text in the JLabel, but in the console it will do multiple lines each on their own line.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class closinggui extends JFrame {

private JPanel contentPane;
JLabel label;
JButton button;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                closinggui frame = new closinggui();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
public void code() throws IOException
{
    Document document = Jsoup.connect("http://www.nbcwashington.com/weather/school-closings/").get();
    Elements tags = document.select("p");

    for (Element tag : tags) {
        System.out.println(tag.text());
        label.setText(tag.text());
    }
}
public closinggui() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 449, 524);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    label = new JLabel("");
    label.setBounds(10, 45, 414, 440);
    contentPane.add(label);

    button = new JButton("get closings");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                code();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    });
    button.setBounds(164, 11, 89, 23);
    contentPane.add(button);
}
}

As a sample it should print out multiple school closings like this:

Washington Yu Ying Public Charter School Closed
Whitman-Walker Health Open at 10am
Woodyard Road Nursery Open at 10am

But on the label all it shows is 1 line that isn't even a closing it is just in the same HTML tag as the others. So how do I make indents?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
informative
  • 53
  • 2
  • 13
  • Something like [this](http://stackoverflow.com/questions/14737810/jlabel-show-longer-text-as-multiple-lines/14738193#14738193) and/or [this](http://stackoverflow.com/questions/27812936/jlabel-new-line-doesnt-work/27813084#27813084)? You might want to consider if a `JList` or `JTextArea` or `JTable` is more suitable – MadProgrammer Mar 07 '15 at 01:03
  • *"So how do I make indents?"* What do 'indents' have to do with 'line breaks'? – Andrew Thompson Mar 07 '15 at 01:42

2 Answers2

3

Try using html tags:

String txt = "<html>";
for (Element tag : tags) {
  txt += tag.text() + "<br/>";
}
txt += "</html>";
label.setText(txt);
2
JLabel label = new JLabel("<html><span>this is <br> your text</span></html>");

or try

setText()

with html having <br> in it.

The <br> will break the lines properly.

TheCoffeeCup
  • 316
  • 5
  • 16
muasif80
  • 5,586
  • 4
  • 32
  • 45