Load Web Page Content Header And Fill the text field - Java Network

Java examples for Network:URL

Description

Load Web Page Content Header And Fill the text field

Demo Code

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Main extends JFrame implements ActionListener, Runnable {

  Thread runner;/*w  ww.  j  a  v  a  2s  . c  om*/
  String[] headers = { "Content-Length", "Content-Type", "Date", "Public",
      "Expires", "Last-Modified", "Server" };

  URL page;
  JTextField url;
  JLabel[] headerLabel = new JLabel[7];
  JTextField[] header = new JTextField[7];
  JButton readPage, clearPage, quitLoading;
  JLabel status;

  public Main() {
    super("Page Data");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new GridLayout(10, 1));
    JPanel first = new JPanel();
    first.setLayout(new FlowLayout(FlowLayout.RIGHT));
    JLabel urlLabel = new JLabel("URL:");
    url = new JTextField(22);
    url.setText("https://java2s.com");
    urlLabel.setLabelFor(url);
    first.add(urlLabel);
    first.add(url);
    add(first);

    JPanel second = new JPanel();
    second.setLayout(new FlowLayout());
    readPage = new JButton("Read Page");
    clearPage = new JButton("Clear Fields");
    quitLoading = new JButton("Quit Loading");
    readPage.setMnemonic('r');
    clearPage.setMnemonic('c');
    quitLoading.setMnemonic('q');
    readPage.setEnabled(true);
    clearPage.setEnabled(false);
    quitLoading.setEnabled(false);
    readPage.addActionListener(this);
    clearPage.addActionListener(this);
    quitLoading.addActionListener(this);
    second.add(readPage);
    second.add(clearPage);
    second.add(quitLoading);
    add(second);

    JPanel[] row = new JPanel[7];
    for (int i = 0; i < 7; i++) {
      row[i] = new JPanel();
      row[i].setLayout(new FlowLayout(FlowLayout.RIGHT));
      headerLabel[i] = new JLabel(headers[i] + ":");
      header[i] = new JTextField(22);
      headerLabel[i].setLabelFor(header[i]);
      row[i].add(headerLabel[i]);
      row[i].add(header[i]);
      add(row[i]);
    }

    JPanel last = new JPanel();
    last.setLayout(new FlowLayout(FlowLayout.LEFT));
    status = new JLabel("Enter a URL address to check.");
    last.add(status);
    add(last);
    pack();
    setVisible(true);
  }

  public void actionPerformed(ActionEvent evt) {
    Object source = evt.getSource();
    if (source == readPage) {
      try {
        page = new URL(url.getText());
        if (runner == null) {
          runner = new Thread(this);
          runner.start();
        }
        quitLoading.setEnabled(true);
        readPage.setEnabled(false);
      } catch (MalformedURLException e) {
        status.setText("Bad URL: " + page);
      }
    } else if (source == clearPage) {
      for (int i = 0; i < 7; i++)
        header[i].setText("");
      quitLoading.setEnabled(false);
      readPage.setEnabled(true);
      clearPage.setEnabled(false);
    } else if (source == quitLoading) {
      runner = null;
      url.setText("");
      quitLoading.setEnabled(false);
      readPage.setEnabled(true);
      clearPage.setEnabled(false);
    }
  }

  public void run() {
    URLConnection conn;
    try {
      conn = this.page.openConnection();
      conn.connect();
      status.setText("Connection opened ...");
      for (int i = 0; i < 7; i++)
        header[i].setText(conn.getHeaderField(headers[i]));
      quitLoading.setEnabled(false);
      clearPage.setEnabled(true);
      status.setText("Done");
      runner = null;
    } catch (IOException e) {
      status.setText("IO Error:" + e.getMessage());
    }
  }

  public static void main(String[] arguments) {
    Main frame = new Main();
  }
}

Related Tutorials