MainClass.java Source code

Java tutorial

Introduction

Here is the source code for MainClass.java

Source

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.ComboBoxEditor;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;

public class MainClass extends JPanel {

    private IconItem items[] = { new IconItem("A", "A.gif"), new IconItem("B", "B.gif"),
            new IconItem("C", "C.gif") };

    Map itemMap = new HashMap();

    public MainClass() {
        // Build a mapping from book titles to their entries
        for (int i = 0; i < items.length; i++) {
            itemMap.put(items[i].getTitle(), items[i]);
        }

        setLayout(new BorderLayout());

        JComboBox bookCombo = new JComboBox(items);
        bookCombo.setEditable(true);
        bookCombo.setEditor(new MyComboBoxEditor(itemMap, items[0]));
        bookCombo.setMaximumRowCount(4);
        bookCombo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("You chose " + ((JComboBox) e.getSource()).getSelectedItem() + "!");
            }
        });
        bookCombo.setActionCommand("Hello");
        add(bookCombo, BorderLayout.CENTER);
    }

    public static void main(String s[]) {
        JFrame frame = new JFrame("Combo Box Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new MainClass());
        frame.pack();
        frame.setVisible(true);
    }
}

class IconItem {
    private final String title;

    private final String imagePath;

    private ImageIcon image;

    public IconItem(String title, String imagePath) {
        this.title = title;
        this.imagePath = imagePath;
    }

    public String getTitle() {
        return title;
    }

    public ImageIcon getImage() {
        if (image == null) {
            image = new ImageIcon(imagePath);
        }
        return image;
    }

    // Override standard toString method to give a useful result
    public String toString() {
        return title;
    }
}

class MyComboBoxEditor implements ComboBoxEditor {
    Map map;

    ImagePanel panel;

    ImageIcon questionIcon;

    public MyComboBoxEditor(Map m, IconItem defaultChoice) {
        map = m;
        panel = new ImagePanel(defaultChoice);
        questionIcon = new ImageIcon("question.gif");
    }

    public void setItem(Object anObject) {
        if (anObject != null) {
            panel.setText(anObject.toString());
            IconItem entry = (IconItem) map.get(anObject.toString());
            if (entry != null)
                panel.setIcon(entry.getImage());
            else
                panel.setIcon(questionIcon);
        }
    }

    public Component getEditorComponent() {
        return panel;
    }

    public Object getItem() {
        return panel.getText();
    }

    public void selectAll() {
        panel.selectAll();
    }

    public void addActionListener(ActionListener l) {
        panel.addActionListener(l);
    }

    public void removeActionListener(ActionListener l) {
        panel.removeActionListener(l);
    }

    class ImagePanel extends JPanel {

        JLabel imageIconLabel;

        JTextField textField;

        public ImagePanel(IconItem initialEntry) {
            setLayout(new BorderLayout());

            imageIconLabel = new JLabel(initialEntry.getImage());
            imageIconLabel.setBorder(new BevelBorder(BevelBorder.RAISED));

            textField = new JTextField(initialEntry.getTitle());
            textField.setColumns(45);
            textField.setBorder(new BevelBorder(BevelBorder.LOWERED));

            add(imageIconLabel, BorderLayout.WEST);
            add(textField, BorderLayout.EAST);
        }

        public void setText(String s) {
            textField.setText(s);
        }

        public String getText() {
            return (textField.getText());
        }

        public void setIcon(Icon i) {
            imageIconLabel.setIcon(i);
            repaint();
        }

        public void selectAll() {
            textField.selectAll();
        }

        public void addActionListener(ActionListener l) {
            textField.addActionListener(l);
        }

        public void removeActionListener(ActionListener l) {
            textField.removeActionListener(l);
        }
    }
}