// import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; /** From: http://java.sun.com/developer/TechTips/txtarchive/2006/Mar06_JohnZ.txt @author John Zukowski */ public class StreamsApplet extends JApplet { private static final String CHARSET = "UTF-8"; JButton add; JButton remove; JList list; JTextField key; JTextField value; public void init() { JLabel keyLabel = new JLabel("Key"); keyLabel.setDisplayedMnemonic('K'); key = new JTextField(); keyLabel.setLabelFor(key); JLabel valueLabel = new JLabel("Value"); valueLabel.setDisplayedMnemonic('V'); value = new JTextField(); valueLabel.setLabelFor(value); JPanel topPanel = new JPanel(new GridLayout(2,2)); topPanel.add(keyLabel); topPanel.add(key); topPanel.add(valueLabel); topPanel.add(value); add(topPanel, BorderLayout.NORTH); list = new JList(); list.setSelectionMode( ListSelectionModel.SINGLE_SELECTION); JScrollPane pane = new JScrollPane(list); add(pane, BorderLayout.CENTER); add = new JButton("Add/Update"); add.setMnemonic('A'); remove = new JButton("Remove"); remove.setMnemonic('R'); JPanel bottomPanel = new JPanel(); bottomPanel.add(add); bottomPanel.add(remove); add(bottomPanel, BorderLayout.SOUTH); ActionListener addListener = new ActionListener() { public void actionPerformed(ActionEvent e) { String keyText = key.getText(); String valueText = value.getText(); try { ByteArrayInputStream bais = new ByteArrayInputStream( valueText.getBytes(CHARSET)); getAppletContext().setStream(keyText, bais); } catch (IOException ioe) { JOptionPane.showMessageDialog(StreamsApplet.this, "Unable to save", "Error", JOptionPane.ERROR_MESSAGE); } updateList(); key.setText(""); value.setText(""); } }; add.addActionListener(addListener); ActionListener removeListener = new ActionListener() { public void actionPerformed(ActionEvent e) { String keyText = key.getText(); try { getAppletContext().setStream(keyText, null); } catch (IOException ioe) { JOptionPane.showMessageDialog(StreamsApplet.this, "Unable to clear", "Error", JOptionPane.ERROR_MESSAGE); } updateList(); key.setText(""); value.setText(""); } }; remove.addActionListener(removeListener); ListSelectionListener selectListener = new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { String selection = (String)list.getSelectedValue(); try { InputStream stream = getAppletContext().getStream(selection); InputStreamReader isr = new InputStreamReader(stream, CHARSET); BufferedReader reader = new BufferedReader(isr); String line = reader.readLine(); key.setText(selection); value.setText(line); } catch (IOException ioe) { JOptionPane.showMessageDialog(StreamsApplet.this, "Unable to read", "Error", JOptionPane.ERROR_MESSAGE); } } }; list.addListSelectionListener(selectListener); updateList(); } private void updateList() { DefaultListModel model = new DefaultListModel(); Iterator iter = getAppletContext().getStreamKeys(); if (iter != null) { while (iter.hasNext()) { model.addElement(iter.next()); } } list.setModel(model); } }