Example usage for java.awt.event ItemEvent getItem

List of usage examples for java.awt.event ItemEvent getItem

Introduction

In this page you can find the example usage for java.awt.event ItemEvent getItem.

Prototype

public Object getItem() 

Source Link

Document

Returns the item affected by the event.

Usage

From source file:Main.java

public static void main(String[] args) {
    JComboBox<String> box = new JComboBox<>();
    box.addItem("One");
    box.addItem("Two");
    box.addItem("Three");

    box.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                System.out.println(e.getItem());
            }//from w w w .  j  a v  a 2  s  .com
        }
    });
    JFrame frame = new JFrame();
    frame.getContentPane().add(box);
    frame.pack();
    frame.setVisible(true);
}

From source file:ItemTest.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();

    ItemListener listener = new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            System.out.println("Source: " + name(e.getSource()));
            System.out.println("Item: " + name(e.getItem()));
            int state = e.getStateChange();
            System.out.println("State: " + ((state == ItemEvent.SELECTED) ? "Selected" : "Deselected"));
        }//from   ww w. ja  va2s . c  o m

        private String name(Object o) {
            if (o instanceof JComponent) {
                JComponent comp = (JComponent) o;
                return comp.getName();
            } else {
                return o.toString();
            }
        }
    };

    JPanel panel = new JPanel(new GridLayout(0, 1));
    ButtonGroup group = new ButtonGroup();
    JRadioButton option = new JRadioButton("French Fries", true);
    option.setName(option.getText());
    option.addItemListener(listener);
    group.add(option);
    panel.add(option);
    option = new JRadioButton("Onion Rings", false);
    option.setName(option.getText());
    option.addItemListener(listener);
    group.add(option);
    panel.add(option);
    option = new JRadioButton("Ice Cream", false);
    option.setName(option.getText());
    option.addItemListener(listener);
    group.add(option);
    panel.add(option);
    contentPane.add(panel, BorderLayout.NORTH);

    String flavors[] = { "Item 1", "Item 2", "Item 3" };
    JComboBox jc = new JComboBox(flavors);
    jc.setName("Combo");
    jc.addItemListener(listener);
    jc.setMaximumRowCount(4);
    contentPane.add(jc, BorderLayout.SOUTH);

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

From source file:Main.java

public static void main(final String args[]) {
    final String labels[] = { "A", "B", "C", "D", "E" };
    JFrame frame = new JFrame("Selecting JComboBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JComboBox comboBox = new JComboBox(labels);
    frame.add(comboBox, BorderLayout.SOUTH);

    ItemListener itemListener = new ItemListener() {
        public void itemStateChanged(ItemEvent itemEvent) {
            int state = itemEvent.getStateChange();
            String stateString = ((state == ItemEvent.SELECTED) ? "Selected" : "Deselected");
            System.out.println("Item: " + itemEvent.getItem());
            System.out.println(", State: " + stateString);
            ItemSelectable is = itemEvent.getItemSelectable();
            System.out.println(", Selected: " + selectedString(is));
        }/*from  w  w  w .j av  a2 s  .c o  m*/
    };
    comboBox.addItemListener(itemListener);
    frame.setSize(400, 200);
    frame.setVisible(true);

}

From source file:SelectingComboSample.java

public static void main(String args[]) {
    String labels[] = { "A", "B", "C", "D", "E", "F", "G", "H", "J", "I" };
    JFrame frame = new JFrame("Selecting JComboBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    JComboBox comboBox = new JComboBox(labels);
    contentPane.add(comboBox, BorderLayout.SOUTH);

    final JTextArea textArea = new JTextArea();
    textArea.setEditable(false);/*from ww  w .  jav  a  2s. co  m*/
    JScrollPane sp = new JScrollPane(textArea);
    contentPane.add(sp, BorderLayout.CENTER);

    ItemListener itemListener = new ItemListener() {
        public void itemStateChanged(ItemEvent itemEvent) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            int state = itemEvent.getStateChange();
            String stateString = ((state == ItemEvent.SELECTED) ? "Selected" : "Deselected");
            pw.print("Item: " + itemEvent.getItem());
            pw.print(", State: " + stateString);
            ItemSelectable is = itemEvent.getItemSelectable();
            pw.print(", Selected: " + selectedString(is));
            pw.println();
            textArea.append(sw.toString());
        }
    };
    comboBox.addItemListener(itemListener);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            pw.print("Command: " + actionEvent.getActionCommand());
            ItemSelectable is = (ItemSelectable) actionEvent.getSource();
            pw.print(", Selected: " + selectedString(is));
            pw.println();
            textArea.append(sw.toString());
        }
    };
    comboBox.addActionListener(actionListener);

    frame.setSize(400, 200);
    frame.setVisible(true);
}

From source file:levelBuilder.DialogMaker.java

/**
 * Various routines necessary for displaying graph.
 *///from   ww  w .  j av  a2s . c om
private static void displayGraph() {
    JFrame frame = new JFrame("Dialog Maker");
    layout = new KKLayout<DialogNode, Double>(g);
    VisualizationViewer<DialogNode, Double> vv = new VisualizationViewer<DialogNode, Double>(layout);
    layout.setSize(new Dimension(windowWidth, windowHeight));
    vv.setPreferredSize(new Dimension(windowWidth, windowHeight));

    //Changes fields in node properties window when a node is selected
    pickedState = vv.getPickedVertexState();
    pickedState.addItemListener(new ItemListener() {
        @Override
        public void itemStateChanged(ItemEvent e) {
            Object subject = e.getItem();
            if (subject instanceof DialogNode) {
                selectedNode = (DialogNode) subject;
                textField.setText(selectedNode.getText());
                npcBox.setSelected(selectedNode.getIsNPC());
                String working = "";
                for (int c = 0; c < selectedNode.getChildren().length; c++) {
                    if (c == 0)
                        working += selectedNode.getChildren()[c].getText();
                    else
                        working += "," + selectedNode.getChildren()[c].getText();
                }
                if (working.equals("null"))
                    working = "";
                childrenField.setText(working.replace("]", "").replace("[", ""));
                working = Arrays.toString(selectedNode.getProbSets().get(strategy));
                if (working.equals("null"))
                    working = "";
                probSetField.setText(working.replace("]", "").replace("[", "").replace(" ", ""));
            }
        }
    });

    //Colors vertices according to 'initial', 'end', or 'middle' status.
    vv.getRenderContext().setVertexFillPaintTransformer(new Transformer<DialogNode, Paint>() {
        @Override
        public Paint transform(DialogNode n) {
            if (n.getText().equals("initial"))
                return new Color(100, 255, 100);
            if (n.getChildren().length == 0)
                return new Color(255, 100, 100);
            return new Color(100, 100, 255);
        }
    });

    //Labels vertices with node text.
    vv.getRenderContext().setVertexLabelTransformer(new Transformer<DialogNode, String>() {
        @Override
        public String transform(DialogNode n) {
            return n.getText().split(" ")[0];
        }

    });

    //Draws shape of vertices according to player or non-player status.
    vv.getRenderContext().setVertexShapeTransformer(new Transformer<DialogNode, Shape>() {
        @Override
        public Shape transform(DialogNode n) {
            if (n.getIsNPC())
                return new Rectangle(-15, -15, 30, 30);
            else
                return new Ellipse2D.Double(-15.0, -15.0, 30.0, 30.0);
        }

    });

    //Labels edges with probability of child being selected under the current strategy.
    vv.getRenderContext().setEdgeLabelTransformer(new Transformer<Double, String>() {
        @Override
        public String transform(Double e) {
            DialogNode source = g.getSource(e);
            if (source.getProbSets().size() > 0)
                for (int c = 0; c < source.getChildren().length; c++)
                    if (source.getChildren()[c].equals(g.getDest(e))) {
                        if (source.getProbSets().get(strategy) != null)
                            return Double.toString(source.getProbSets().get(strategy)[c]);
                        else //If node does not have probSet for that strategy, default to strategy 0.
                            return Double.toString(source.getProbSets().get(0)[c]);
                    }
            return null;
        }
    });
    vv.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);

    //Routines for editing mode.
    EditingModalGraphMouse<DialogNode, Double> gm = new EditingModalGraphMouse<DialogNode, Double>(
            vv.getRenderContext(),
            //Runs when a new node is created.
            new Factory<DialogNode>() {
                @Override
                public DialogNode create() {
                    DialogNode n = new DialogNode(isNPCBoxChecked, textField.getText(),
                            new ArrayList<double[]>(), new DialogNode[0]);
                    dg.addNode(n);
                    return n;
                }
            },
            //Runs when a new edge is created.
            new Factory<Double>() {
                @Override
                public Double create() {
                    addedEdgeID = Math.random();
                    return addedEdgeID;
                }
            });
    vv.setGraphMouse(gm);

    //Frame and mode menu.
    JMenuBar menuBar = new JMenuBar();
    JMenu modeMenu = gm.getModeMenu();
    modeMenu.setText("Mouse Mode");
    modeMenu.setIcon(null);
    modeMenu.setPreferredSize(new Dimension(100, 20));
    menuBar.add(modeMenu);
    frame.setJMenuBar(menuBar);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(vv);
    frame.pack();
    frame.setVisible(true);

    //      //Sets position of each node.
    //      for (DialogNode n : nodeMap.values()) {
    //         Point2D.Double point = new Point2D.Double(n.getX(), n.getY());
    //         if (point.x != 0.0 && point.y != 0.0)
    //            layout.setLocation(n, point);
    //      }
}

From source file:Main.java

public void itemStateChanged(ItemEvent ie) {
    String s = (String) ie.getItem();
    System.out.println(s);
}

From source file:Main.java

public void itemStateChanged(ItemEvent ie) {
    JCheckBox cb = (JCheckBox) ie.getItem();
    int state = ie.getStateChange();
    if (state == ItemEvent.DESELECTED) {
        System.out.println(cb.getText() + " DESELECTED");
    }//from   w  ww . j  a  v  a  2 s.com
}

From source file:Main.java

public void itemStateChanged(ItemEvent ie) {
    JCheckBox cb = (JCheckBox) ie.getItem();
    int state = ie.getStateChange();
    if (state == ItemEvent.SELECTED) {
        System.out.println(cb.getText() + " selected");
    } else {//from   w  w w . j a  va 2s.  c om
        System.out.println(cb.getText() + " cleared");
    }
}

From source file:Main.java

public Main() {
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);

    String[] sList = new String[] { "Spring", "Summer", "Fall", "Winter" };
    JComboBox<String> seasons = new JComboBox<>(sList);

    seasons.addItemListener((ItemEvent e) -> {
        Object item = e.getItem();
        if (e.getStateChange() == ItemEvent.SELECTED) {
            // Item has been selected
            System.out.println(item + "  has  been  selected");
        } else if (e.getStateChange() == ItemEvent.DESELECTED) {
            // Item has been deselected
            System.out.println(item + "  has  been  deselected");
        }/*from   ww  w  . java 2 s.com*/
    });

    Container contentPane = this.getContentPane();
    contentPane.add(seasons, BorderLayout.CENTER);
}

From source file:Main.java

public void itemStateChanged(ItemEvent ie) {
    JCheckBox cb = (JCheckBox) ie.getItem();
    int state = ie.getStateChange();
    if (state == ItemEvent.SELECTED)
        System.out.println(cb.getText() + " selected");
    else//from  w  ww. j av a  2 s.  c  o m
        System.out.println(cb.getText() + " cleared");
}