MemComboBoxDemo.java Source code

Java tutorial

Introduction

Here is the source code for MemComboBoxDemo.java

Source

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.net.URL;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

public class MemComboBoxDemo extends JFrame {

    protected MemComboBox urlComboBox = new MemComboBox();

    public MemComboBoxDemo() {
        super();
        setSize(300, 100);
        getContentPane().setLayout(new BorderLayout());

        JPanel p = new JPanel();
        p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
        p.add(new JLabel("Address"));

        urlComboBox.load("addresses.dat");
        ComboBoxListener lst = new ComboBoxListener();
        urlComboBox.addActionListener(lst);

        MemComboAgent agent = new MemComboAgent(urlComboBox);

        p.add(urlComboBox);
        getContentPane().add(p, BorderLayout.NORTH);

        WindowListener wndCloser = new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                urlComboBox.save("addresses.dat");
                System.exit(0);
            }
        };
        addWindowListener(wndCloser);

        setVisible(true);
        urlComboBox.grabFocus();
    }

    class ComboBoxListener implements ActionListener {
        public void actionPerformed(ActionEvent evt) {
            System.out.println(urlComboBox.getSelectedItem());
        }
    }

    public static void main(String argv[]) {
        new MemComboBoxDemo();
    }
}

class MemComboAgent extends KeyAdapter {
    protected JComboBox comboBox;

    protected JTextField editor;

    public MemComboAgent(JComboBox c) {
        comboBox = c;
        editor = (JTextField) c.getEditor().getEditorComponent();
        editor.addKeyListener(this);
    }

    public void keyReleased(KeyEvent e) {
        char ch = e.getKeyChar();
        if (ch == KeyEvent.CHAR_UNDEFINED || Character.isISOControl(ch))
            return;
        int pos = editor.getCaretPosition();
        String str = editor.getText();
        if (str.length() == 0)
            return;

        for (int k = 0; k < comboBox.getItemCount(); k++) {
            String item = comboBox.getItemAt(k).toString();
            if (item.startsWith(str)) {
                editor.setText(item);
                editor.setCaretPosition(item.length());
                editor.moveCaretPosition(pos);
                break;
            }
        }
    }
}

class MemComboBox extends JComboBox {
    public static final int MAX_MEM_LEN = 30;

    public MemComboBox() {
        super();
        setEditable(true);
    }

    public void add(String item) {
        removeItem(item);
        insertItemAt(item, 0);
        setSelectedItem(item);
        if (getItemCount() > MAX_MEM_LEN)
            removeItemAt(getItemCount() - 1);
    }

    public void load(String fName) {
        try {
            if (getItemCount() > 0)
                removeAllItems();
            File f = new File(fName);
            if (!f.exists())
                return;
            FileInputStream fStream = new FileInputStream(f);
            ObjectInput stream = new ObjectInputStream(fStream);

            Object obj = stream.readObject();
            if (obj instanceof ComboBoxModel)
                setModel((ComboBoxModel) obj);

            stream.close();
            fStream.close();
        } catch (Exception e) {
            System.err.println("Serialization error: " + e.toString());
        }
    }

    public void save(String fName) {
        try {
            FileOutputStream fStream = new FileOutputStream(fName);
            ObjectOutput stream = new ObjectOutputStream(fStream);

            stream.writeObject(getModel());

            stream.flush();
            stream.close();
            fStream.close();
        } catch (Exception e) {
            System.err.println("Serialization error: " + e.toString());
        }
    }
}