Example usage for javax.swing JComboBox addActionListener

List of usage examples for javax.swing JComboBox addActionListener

Introduction

In this page you can find the example usage for javax.swing JComboBox addActionListener.

Prototype

public void addActionListener(ActionListener l) 

Source Link

Document

Adds an ActionListener.

Usage

From source file:Main.java

public static void main(String[] args) {
    JComboBox<String> emptyBox = new JComboBox<String>();
    emptyBox.addActionListener(new ActionListener() {
        @Override/*w  w w. j a va 2s.  co  m*/
        public void actionPerformed(ActionEvent e) {
            Thread.dumpStack();
        }
    });
    emptyBox.addItem("test");
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    String labels[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };
    JComboBox<String> comboBox = new JComboBox<>(labels);
    comboBox.setEditable(true);/* w  ww  .  ja va2 s  .  c om*/

    comboBox.addActionListener(new ActionListener() {
        boolean found = false;

        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            String s = (String) comboBox.getSelectedItem();
            for (int i = 0; i < comboBox.getItemCount(); i++) {
                if (comboBox.getItemAt(i).toString().equals(s)) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                System.out.println("Added: " + s);
                comboBox.addItem(s);
            }
            found = false;
        }
    });

    frame.add(comboBox);

    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();
    ActionListener listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Command: " + e.getActionCommand());
            int modifiers = e.getModifiers();
            System.out.println("\tALT : " + checkMod(modifiers, ActionEvent.ALT_MASK));
            System.out.println("\tCTRL : " + checkMod(modifiers, ActionEvent.CTRL_MASK));
            System.out.println("\tMETA : " + checkMod(modifiers, ActionEvent.META_MASK));
            System.out.println("\tSHIFT: " + checkMod(modifiers, ActionEvent.SHIFT_MASK));
            Object source = e.getSource();
            if (source instanceof JComboBox) {
                JComboBox jb = (JComboBox) source;
                System.out.println("Combo: " + jb.getSelectedItem());
            }/*from w ww . ja v  a  2 s  .c  o m*/
        }

        private boolean checkMod(int modifiers, int mask) {
            return ((modifiers & mask) == mask);
        }
    };

    String flavors[] = { "Item 1", "Item 2", "Item 3" };
    JComboBox jc = new JComboBox(flavors);
    jc.setMaximumRowCount(4);
    jc.setEditable(true);
    jc.addActionListener(listener);
    contentPane.add(jc, BorderLayout.NORTH);

    JButton b = new JButton("Button!");
    b.addActionListener(listener);
    contentPane.add(b, BorderLayout.CENTER);

    JPanel panel = new JPanel();
    JLabel label = new JLabel("Label 1: ");
    JTextField text = new JTextField("Type your text", 15);
    text.addActionListener(listener);
    label.setDisplayedMnemonic(KeyEvent.VK_1);
    label.setLabelFor(text);
    panel.add(label);
    panel.add(text);
    contentPane.add(panel, BorderLayout.SOUTH);

    frame.pack();
    frame.setVisible(true);
}

From source file:ActionTest.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();
    ActionListener listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Command: " + e.getActionCommand());
            System.out.println("Modifiers: ");
            int modifiers = e.getModifiers();
            System.out.println("\tALT : " + checkMod(modifiers, ActionEvent.ALT_MASK));
            System.out.println("\tCTRL : " + checkMod(modifiers, ActionEvent.CTRL_MASK));
            System.out.println("\tMETA : " + checkMod(modifiers, ActionEvent.META_MASK));
            System.out.println("\tSHIFT: " + checkMod(modifiers, ActionEvent.SHIFT_MASK));
            Object source = e.getSource();
            if (source instanceof JComboBox) {
                JComboBox jb = (JComboBox) source;
                System.out.println("Combo: " + jb.getSelectedItem());
            }//w  w w  .ja v a 2s  . c  om
        }

        private boolean checkMod(int modifiers, int mask) {
            return ((modifiers & mask) == mask);
        }
    };

    String flavors[] = { "Item 1", "Item 2", "Item 3" };
    JComboBox jc = new JComboBox(flavors);
    jc.setMaximumRowCount(4);
    jc.setEditable(true);
    jc.addActionListener(listener);
    contentPane.add(jc, BorderLayout.NORTH);

    JButton b = new JButton("Button!");
    b.addActionListener(listener);
    contentPane.add(b, BorderLayout.CENTER);

    JPanel panel = new JPanel();
    JLabel label = new JLabel("Label 1: ");
    JTextField text = new JTextField("Type your text", 15);
    text.addActionListener(listener);
    label.setDisplayedMnemonic(KeyEvent.VK_1);
    label.setLabelFor(text);
    panel.add(label);
    panel.add(text);
    contentPane.add(panel, BorderLayout.SOUTH);

    frame.pack();
    frame.show();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String[] items = { "item1", "item2" };
    JComboBox cb = new JComboBox(items);
    cb.setEditable(true);/* w  w w .  j a  va2 s .  c o m*/

    // Create and register listener
    MyActionListener actionListener = new MyActionListener();
    cb.addActionListener(actionListener);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    int SIZE = 14;
    String FONT = "Dialog";

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextPane tp = new JTextPane();
    tp.setFont(new Font(FONT, Font.PLAIN, SIZE));
    tp.setPreferredSize(new Dimension(400, 300));
    StyledDocument doc = tp.getStyledDocument();
    Style defaultStyle = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
    Style boldStyle = doc.addStyle("bold", defaultStyle);
    StyleConstants.setBold(boldStyle, true);
    String boldText = "this is bold test";
    String plainText = "this is plain.";

    doc.insertString(doc.getLength(), boldText, boldStyle);
    doc.insertString(doc.getLength(), plainText, defaultStyle);

    JPanel panel = new JPanel();
    panel.add(tp);//  www  . ja va2 s.com

    JComboBox<String> zoomCombo = new JComboBox<String>(
            new String[] { "0.75", "1.00", "1.50", "1.75", "2.00" });
    zoomCombo.addActionListener(e -> {
        String s = (String) zoomCombo.getSelectedItem();
        double scale = new Double(s).doubleValue();
        int size = (int) (SIZE * scale);
        tp.setFont(new Font(FONT, Font.PLAIN, size));
    });
    zoomCombo.setSelectedItem("1.00");
    JPanel optionsPanel = new JPanel();
    optionsPanel.add(zoomCombo);
    panel.setBackground(Color.WHITE);
    frame.add(panel, BorderLayout.CENTER);
    frame.add(optionsPanel, BorderLayout.NORTH);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String argv[]) throws Exception {
    JComboBox<Item> comboBox = new JComboBox<Item>(
            new Item[] { new Item("Major", "red"), new Item("Critical", "dark"), new Item("Minor", "green") });
    comboBox.addActionListener(e -> {
        JComboBox<Item> combo = (JComboBox<Item>) e.getSource();
        Item item = (Item) combo.getSelectedItem();
        System.out.println(item.getColor());
    });//from  w  ww.j  a va2 s .co m
    JFrame frame = new JFrame();
    frame.add(comboBox);
    frame.pack();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

From source file:SaveImage.java

public static void main(String s[]) {
    JFrame f = new JFrame("Save Image Sample");
    f.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);//from  w  ww  .j a va 2s  . com
        }
    });
    SaveImage si = new SaveImage();
    f.add("Center", si);
    JComboBox choices = new JComboBox(si.getDescriptions());
    choices.setActionCommand("SetFilter");
    choices.addActionListener(si);
    JComboBox formats = new JComboBox(si.getFormats());
    formats.setActionCommand("Formats");
    formats.addActionListener(si);
    JPanel panel = new JPanel();
    panel.add(choices);
    panel.add(new JLabel("Save As"));
    panel.add(formats);
    f.add("South", panel);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame window = new JFrame("Example");

    JPanel contentPane = new JPanel();

    contentPane.setLayout(new GridBagLayout());

    JComboBox comboBoxfields = new JComboBox(COMBO_BOX_ELEMENTS);

    comboBoxfields.setFont(new Font("sansserif", Font.TRUETYPE_FONT | Font.PLAIN, 15));
    comboBoxfields.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));

    comboBoxfields.setMaximumRowCount(5);

    comboBoxfields.addActionListener(
            e -> System.out.println("'" + comboBoxfields.getSelectedItem().toString() + "'" + " was selected"));

    contentPane.add(comboBoxfields);/* w  w  w .ja  v  a 2  s.  com*/

    window.add(contentPane);
    window.setSize(500, 500);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel gui = new JPanel(new BorderLayout(5, 5));

    JPanel plafComponents = new JPanel(new FlowLayout(FlowLayout.RIGHT, 3, 3));
    plafComponents.setBorder(new TitledBorder("FlowLayout(FlowLayout.RIGHT, 3,3)"));

    UIManager.LookAndFeelInfo[] plafInfos = UIManager.getInstalledLookAndFeels();
    String[] plafNames = new String[plafInfos.length];
    for (int ii = 0; ii < plafInfos.length; ii++) {
        plafNames[ii] = plafInfos[ii].getName();
    }/*from w  w w  .jav  a 2s .  co m*/
    JComboBox plafChooser = new JComboBox(plafNames);
    plafComponents.add(plafChooser);

    JCheckBox pack = new JCheckBox("Pack on PLAF change", true);
    plafComponents.add(pack);

    plafChooser.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            int index = plafChooser.getSelectedIndex();
            try {
                UIManager.setLookAndFeel(plafInfos[index].getClassName());
                SwingUtilities.updateComponentTreeUI(frame);
                if (pack.isSelected()) {
                    frame.pack();
                    frame.setMinimumSize(frame.getSize());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    gui.add(plafComponents, BorderLayout.NORTH);

    JPanel dynamicLabels = new JPanel(new BorderLayout(4, 4));
    dynamicLabels.setBorder(new TitledBorder("BorderLayout(4,4)"));
    gui.add(dynamicLabels, BorderLayout.WEST);

    final JPanel labels = new JPanel(new GridLayout(0, 2, 3, 3));
    labels.setBorder(new TitledBorder("GridLayout(0,2,3,3)"));

    JButton addNew = new JButton("Add Another Label");
    dynamicLabels.add(addNew, BorderLayout.NORTH);
    addNew.addActionListener(new ActionListener() {

        private int labelCount = 0;

        public void actionPerformed(ActionEvent ae) {
            labels.add(new JLabel("Label " + ++labelCount));
            frame.validate();
        }
    });

    dynamicLabels.add(new JScrollPane(labels), BorderLayout.CENTER);

    String[] header = { "Name", "Value" };
    String[] a = new String[0];
    String[] names = System.getProperties().stringPropertyNames().toArray(a);
    String[][] data = new String[names.length][2];
    for (int ii = 0; ii < names.length; ii++) {
        data[ii][0] = names[ii];
        data[ii][1] = System.getProperty(names[ii]);
    }
    DefaultTableModel model = new DefaultTableModel(data, header);
    JTable table = new JTable(model);

    JScrollPane tableScroll = new JScrollPane(table);
    Dimension tablePreferred = tableScroll.getPreferredSize();
    tableScroll.setPreferredSize(new Dimension(tablePreferred.width, tablePreferred.height / 3));

    JPanel imagePanel = new JPanel(new GridBagLayout());
    JLabel imageLabel = new JLabel("test");
    imagePanel.add(imageLabel, null);

    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, tableScroll, new JScrollPane(imagePanel));
    gui.add(splitPane, BorderLayout.CENTER);

    frame.setContentPane(gui);
    frame.pack();
    frame.setVisible(true);
}