Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

217
LINES

< > BotCompany Repo | #1004040 // Tiny Browser

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (622L/5K/17K).

1  
!759
2  
3  
import java.awt.BorderLayout;
4  
import java.awt.Cursor;
5  
import java.awt.Dimension;
6  
import java.awt.Graphics;
7  
import java.awt.event.ActionEvent;
8  
import java.awt.event.ActionListener;
9  
import java.awt.event.KeyAdapter;
10  
import java.awt.event.KeyEvent;
11  
import java.awt.event.WindowAdapter;
12  
import java.awt.event.WindowEvent;
13  
import java.awt.event.WindowListener;
14  
import java.io.File;
15  
import java.io.FileInputStream;
16  
import java.io.FileOutputStream;
17  
import java.io.ObjectInput;
18  
import java.io.ObjectInputStream;
19  
import java.io.ObjectOutput;
20  
import java.io.ObjectOutputStream;
21  
import java.net.URL;
22  
23  
import javax.swing.Box;
24  
import javax.swing.BoxLayout;
25  
import javax.swing.ComboBoxModel;
26  
import javax.swing.Icon;
27  
import javax.swing.ImageIcon;
28  
import javax.swing.JComboBox;
29  
import javax.swing.JEditorPane;
30  
import javax.swing.JFrame;
31  
import javax.swing.JLabel;
32  
import javax.swing.JOptionPane;
33  
import javax.swing.JPanel;
34  
import javax.swing.JScrollPane;
35  
import javax.swing.JTextField;
36  
import javax.swing.event.HyperlinkEvent;
37  
import javax.swing.event.HyperlinkListener;
38  
39  
p {
40  
  new Browser;
41  
}
42  
43  
sclass Browser extends JFrame {
44  
45  
  protected JEditorPane m_browser;
46  
47  
  protected MemComboBox m_locator = new MemComboBox();
48  
49  
  public Browser() {
50  
    super("HTML Browser");
51  
    setSize(500, 300);
52  
    getContentPane().setLayout(new BorderLayout());
53  
54  
    JPanel p = new JPanel();
55  
    p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
56  
    p.add(new JLabel("Address"));
57  
58  
    m_locator.load("addresses.dat");
59  
    BrowserListener lst = new BrowserListener();
60  
    m_locator.addActionListener(lst);
61  
62  
    MemComboAgent agent = new MemComboAgent(m_locator);
63  
64  
    p.add(m_locator);
65  
66  
    getContentPane().add(p, BorderLayout.NORTH);
67  
68  
    m_browser = new JEditorPane();
69  
    m_browser.setEditable(false);
70  
    // XXX m_browser.addHyperlinkListener(lst);
71  
72  
    JScrollPane sp = new JScrollPane();
73  
    sp.getViewport().add(m_browser);
74  
    getContentPane().add(sp, BorderLayout.CENTER);
75  
76  
    WindowListener wndCloser = new WindowAdapter() {
77  
      public void windowClosing(WindowEvent e) {
78  
        m_locator.save("addresses.dat");
79  
      }
80  
    };
81  
    addWindowListener(wndCloser);
82  
83  
    setVisible(true);
84  
    m_locator.grabFocus();
85  
  }
86  
87  
  class BrowserListener implements ActionListener, HyperlinkListener {
88  
    public void actionPerformed(ActionEvent evt) {
89  
      //printStackTrace();
90  
      String sUrl = (String) m_locator.getSelectedItem();
91  
      if (sUrl == null || sUrl.length() == 0 )
92  
        return;
93  
      sUrl = addHTTP(sUrl);
94  
95  
      BrowserLoader loader = new BrowserLoader(sUrl);
96  
      loader.start();
97  
    }
98  
99  
    public void hyperlinkUpdate(HyperlinkEvent e) {
100  
      URL url = e.getURL();
101  
      if (url == null )
102  
        return;
103  
      BrowserLoader loader = new BrowserLoader(url.toString());
104  
      loader.start();
105  
    }
106  
  }
107  
108  
  class BrowserLoader extends Thread {
109  
    protected String m_sUrl;
110  
111  
    public BrowserLoader(String sUrl) {
112  
      m_sUrl = sUrl;
113  
    }
114  
115  
    public void run() {
116  
      setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
117  
118  
      try {
119  
        URL source = new URL(m_sUrl);
120  
        m_browser.setPage(source);
121  
        m_locator.add(m_sUrl);
122  
      } catch (Exception e) {
123  
        JOptionPane.showMessageDialog(Browser.this, "Error: "
124  
            + e.toString(), "Warning", JOptionPane.WARNING_MESSAGE);
125  
      }
126  
      setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
127  
    }
128  
  }
129  
}
130  
131  
sclass MemComboAgent extends KeyAdapter {
132  
  protected JComboBox m_comboBox;
133  
134  
  protected JTextField m_editor;
135  
136  
  public MemComboAgent(JComboBox comboBox) {
137  
    m_comboBox = comboBox;
138  
    m_editor = (JTextField) comboBox.getEditor().getEditorComponent();
139  
    m_editor.addKeyListener(this);
140  
  }
141  
142  
  public void keyReleased(KeyEvent e) {
143  
    char ch = e.getKeyChar();
144  
    if (ch == KeyEvent.CHAR_UNDEFINED || Character.isISOControl(ch))
145  
      return;
146  
    int pos = m_editor.getCaretPosition();
147  
    String str = m_editor.getText();
148  
    if (str.length() == 0)
149  
      return;
150  
151  
    for (int k = 0; k < m_comboBox.getItemCount(); k++) {
152  
      String item = m_comboBox.getItemAt(k).toString();
153  
      if (item.startsWith(str)) {
154  
        m_editor.setText(item);
155  
        m_editor.setCaretPosition(item.length());
156  
        m_editor.moveCaretPosition(pos);
157  
        break;
158  
      }
159  
    }
160  
  }
161  
}
162  
163  
sclass MemComboBox extends JComboBox {
164  
  public static final int MAX_MEM_LEN = 30;
165  
166  
  public MemComboBox() {
167  
    super();
168  
    setEditable(true);
169  
  }
170  
171  
  public void add(String item) {
172  
    if (eq(getItemAt(0), item)) ret;
173  
    //main.print("MemComboBox.add " + item);
174  
    //printStackTrace();
175  
    removeItem(item);
176  
    insertItemAt(item, 0);
177  
    setSelectedItem(item);
178  
    if (getItemCount() > MAX_MEM_LEN)
179  
      removeItemAt(getItemCount() - 1);
180  
  }
181  
182  
  public void load(String fName) {
183  
    try {
184  
      if (getItemCount() > 0)
185  
        removeAllItems();
186  
      File f = getProgramFile(fName);
187  
      if (!f.exists())
188  
        return;
189  
      FileInputStream fStream = new FileInputStream(f);
190  
      ObjectInput stream = new ObjectInputStream(fStream);
191  
192  
      Object obj = stream.readObject();
193  
      if (obj instanceof ComboBoxModel)
194  
        setModel((ComboBoxModel) obj);
195  
196  
      stream.close();
197  
      fStream.close();
198  
    } catch (Exception e) {
199  
      System.err.println("Serialization error: " + e.toString());
200  
    }
201  
  }
202  
203  
  public void save(String fName) {
204  
    try {
205  
      FileOutputStream fStream = new FileOutputStream(getProgramFile(fName));
206  
      ObjectOutput stream = new ObjectOutputStream(fStream);
207  
208  
      stream.writeObject(getModel());
209  
210  
      stream.flush();
211  
      stream.close();
212  
      fStream.close();
213  
    } catch (Exception e) {
214  
      System.err.println("Serialization error: " + e.toString());
215  
    }
216  
  }
217  
}

Author comment

from http://www.java2s.com/Code/Java/Tiny-Application/BrowerbasedonJEditorPane.htm

download  show line numbers  debug dex  old transpilations   

Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, cysqohhbtkwd, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

Comments [hide]

ID Author/Program Comment Date
1284 stefan Mailed to info@java2s.com about the hopefully fixed endless loop 2016-08-04 16:21:00

add comment

Snippet ID: #1004040
Snippet name: Tiny Browser
Eternal ID of this version: #1004040/1
Text MD5: 97141f4a521a6bf32738eb2814a3fab5
Transpilation MD5: 31d210e835b4bfaa30f8fa97fa1ae75c
Author: stefan
Category: javax / gui
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-08-04 16:16:50
Source code size: 5893 bytes / 217 lines
Pitched / IR pitched: No / No
Views / Downloads: 786 / 883
Referenced in: [show references]