//package swing03;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class URL_Demo extends JFrame {
public URL_Demo() {
initComponents();
}
private void initComponents() {
setTitle("URL Connect");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
toolbar = new JPanel();
tabbedpane = new JTabbedPane();
combobox = new JComboBox();
combobox.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
"http://kqe.de/seite_1.html",
"http://news.bbc.co.uk/sport1/hi/football/teams/a/arsenal/default.stm" ,
"http://www.amazon.com/"
}));
combobox.setPreferredSize(new java.awt.Dimension(500, 22));
combobox.setSelectedIndex(0);
combobox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
displayPage();
}
});
toolbar.add(combobox);
getContentPane().add(toolbar, BorderLayout.NORTH);
getContentPane().add(tabbedpane);
setExtendedState(JFrame.MAXIMIZED_BOTH);
setVisible(true);
displayPage();
}
private void displayPage() {
urlString = combobox.getSelectedItem().toString();
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
URL url = null;
InputStream input = null;
try{
url = new URL( urlString );
}catch(MalformedURLException ex){
ex.printStackTrace();
}
StringBuffer strb = null;
try{
try{
input = url.openStream();
}catch(UnknownHostException ex){
}
if( input != null ){
int c = input.read();
strb = new StringBuffer();
while( c != -1 ){
char cc = (char)c;
strb.append(cc);
c = input.read();
}
}
}catch(IOException ex){
ex.printStackTrace();
}
scrollpane = new JScrollPane();
tabbedpane.add(scrollpane);
tabbedpane.setTitleAt(count++, urlString);
tabbedpane.setSelectedComponent(scrollpane);
editorpane = new JEditorPane();
editorpane.setEditable(false);
editorpane.setContentType("text/html");
scrollpane.setViewportView(editorpane);
if( strb != null ){
editorpane.setText(strb.toString());
editorpane.setCaretPosition(0);
}else{
Toolkit.getDefaultToolkit().beep();
String htmlText =
"<html>"
+"<body BGCOLOR=\"#ffffe0\">"
+"
Connection failed:
" +urlString+
"
Make sure you are connected to the Internet"
+"</body>"
+"</html>";
editorpane.setText(htmlText);
}
setCursor(Cursor.getDefaultCursor());
}
public static void main(String args[]) {
new URL_Demo();
}
private String urlString;
private JEditorPane editorpane;
private JPanel toolbar;
private JTabbedPane tabbedpane;
private JComboBox combobox;
private JScrollPane scrollpane;
private int count;
}