Example usage for javax.swing JComboBox setSelectedItem

List of usage examples for javax.swing JComboBox setSelectedItem

Introduction

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

Prototype

@BeanProperty(bound = false, preferred = true, description = "Sets the selected item in the JComboBox.")
public void setSelectedItem(Object anObject) 

Source Link

Document

Sets the selected item in the combo box display area to the object in the argument.

Usage

From source file:shuffle.fwk.service.teams.EditTeamService.java

private Component createTeamComponent(Species s) {
    Team curTeam = getCurrentTeam();//from ww  w .j a v a 2s. c  om
    JPanel ret = new JPanel(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.gridx = 1;
    c.gridy = 1;
    c.gridwidth = 2;
    Indicator<SpeciesPaint> ind = new Indicator<SpeciesPaint>(this);
    boolean isMega = megaProgress >= megaThreshold && s.getName().equals(curTeam.getMegaSlotName());
    SpeciesPaint paint = new SpeciesPaint(s, s.equals(Species.FREEZE), isMega);
    ind.setVisualized(paint);
    ret.add(ind, c);
    c.gridy += 1;
    c.gridwidth = 1;
    JButton removeButton = new JButton(getString(KEY_REMOVE));
    removeButton.setToolTipText(getString(KEY_REMOVE_TOOLTIP));
    removeButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            removeSpeciesFromTeam(s.getName());
            updateTeamPanel();
        }
    });
    removeButton.setEnabled(
            s.getEffect(getUser().getRosterManager()).isPickable() && !s.getType().equals(PkmType.NONE));
    ret.add(removeButton, c);

    c.gridx += 1;
    JComboBox<Character> keybindsComboBox = new JComboBox<Character>();
    Character curBinding = curTeam.getBinding(s);
    LinkedHashSet<Character> allBindingsFor = new LinkedHashSet<Character>(Arrays.asList(curBinding));
    LinkedHashSet<Character> availableBindings = myData.getAllAvailableBindingsFor(s.getName(), curTeam);
    allBindingsFor.addAll(availableBindings);
    for (Character ch : allBindingsFor) {
        keybindsComboBox.addItem(ch);
    }
    keybindsComboBox.setSelectedItem(curBinding);
    final ItemListener bindingListener = new ItemListener() {
        @Override
        public void itemStateChanged(ItemEvent e) {
            JComboBox<?> source = (JComboBox<?>) e.getSource();
            int selectedIndex = source.getSelectedIndex();
            Character selected = (Character) source.getItemAt(selectedIndex);
            setBinding(s, selected);
            updateKeybindComboBoxes();
        }
    };
    nameToKeybindComboboxMap.put(s.getName(), keybindsComboBox);
    nameToItemListenerMap.put(s.getName(), bindingListener);
    keybindsComboBox.addItemListener(bindingListener);
    keybindsComboBox.setToolTipText(getString(KEY_KEYBINDS_TOOLTIP));
    ret.add(keybindsComboBox, c);

    MouseAdapter ma = new PressToggleMouseAdapter() {

        @Override
        protected void onRight(MouseEvent e) {
            doToggle();
        }

        @Override
        protected void onLeft(MouseEvent e) {
            doToggle();
        }

        private void doToggle() {
            toggleSupport(s);
            updateTeamPanel();
        }
    };
    ret.addMouseListener(ma);

    setBorderFor(ret, false, false);
    if (!Species.FIXED_SPECIES.contains(s)) {
        boolean isSupport = !curTeam.isNonSupport(s);
        Color indColor = isSupport ? Color.GREEN : Color.RED;
        ret.setBackground(indColor);
        ret.setOpaque(true);
    }
    return ret;
}

From source file:shuffle.fwk.service.teams.EditTeamService.java

private void updateKeybindComboBoxes() {
    Team curTeam = getCurrentTeam();/*  ww w. j ava 2 s.c  o m*/
    for (String name : curTeam.getNames()) {
        ItemListener itemListener = nameToItemListenerMap.get(name);
        JComboBox<Character> box = nameToKeybindComboboxMap.get(name);
        box.removeItemListener(itemListener);
        Character prevSelected = box.getItemAt(box.getSelectedIndex());
        box.removeAllItems();
        Character curBinding = curTeam.getBinding(name);
        LinkedHashSet<Character> availableBindings = new LinkedHashSet<Character>(Arrays.asList(curBinding));
        availableBindings.addAll(myData.getAllAvailableBindingsFor(name, curTeam));
        for (Character ch : availableBindings) {
            if (ch != null) {
                box.addItem(ch);
            }
        }
        box.setSelectedItem(prevSelected);
        if (box.getSelectedIndex() < 0) {
            LOG.warning(getString(KEY_NO_BINDINGS));
        }
        box.addItemListener(itemListener);
    }
}

From source file:test.visualization.PluggableRendererDemo.java

/**
 * @param jp    panel to which controls will be added
 *//*from w  w  w  .j  a va 2s  .c om*/
@SuppressWarnings("serial")
protected void addBottomControls(final JPanel jp) {
    final JPanel control_panel = new JPanel();
    jp.add(control_panel, BorderLayout.EAST);
    control_panel.setLayout(new BorderLayout());
    final Box vertex_panel = Box.createVerticalBox();
    vertex_panel.setBorder(BorderFactory.createTitledBorder("Vertices"));
    final Box edge_panel = Box.createVerticalBox();
    edge_panel.setBorder(BorderFactory.createTitledBorder("Edges"));
    final Box both_panel = Box.createVerticalBox();

    control_panel.add(vertex_panel, BorderLayout.NORTH);
    control_panel.add(edge_panel, BorderLayout.SOUTH);
    control_panel.add(both_panel, BorderLayout.CENTER);

    // set up vertex controls
    v_color = new JCheckBox("seed highlight");
    v_color.addActionListener(this);
    v_stroke = new JCheckBox("stroke highlight on selection");
    v_stroke.addActionListener(this);
    v_labels = new JCheckBox("show voltage values");
    v_labels.addActionListener(this);
    v_shape = new JCheckBox("shape by degree");
    v_shape.addActionListener(this);
    v_size = new JCheckBox("size by voltage");
    v_size.addActionListener(this);
    v_size.setSelected(true);
    v_aspect = new JCheckBox("stretch by degree ratio");
    v_aspect.addActionListener(this);
    v_small = new JCheckBox("filter when degree < " + VertexDisplayPredicate.MIN_DEGREE);
    v_small.addActionListener(this);

    vertex_panel.add(v_color);
    vertex_panel.add(v_stroke);
    vertex_panel.add(v_labels);
    vertex_panel.add(v_shape);
    vertex_panel.add(v_size);
    vertex_panel.add(v_aspect);
    vertex_panel.add(v_small);

    // set up edge controls
    JPanel gradient_panel = new JPanel(new GridLayout(1, 0));
    gradient_panel.setBorder(BorderFactory.createTitledBorder("Edge paint"));
    no_gradient = new JRadioButton("Solid color");
    no_gradient.addActionListener(this);
    no_gradient.setSelected(true);
    //      gradient_absolute = new JRadioButton("Absolute gradient");
    //      gradient_absolute.addActionListener(this);
    gradient_relative = new JRadioButton("Gradient");
    gradient_relative.addActionListener(this);
    ButtonGroup bg_grad = new ButtonGroup();
    bg_grad.add(no_gradient);
    bg_grad.add(gradient_relative);
    //bg_grad.add(gradient_absolute);
    gradient_panel.add(no_gradient);
    //gradientGrid.add(gradient_absolute);
    gradient_panel.add(gradient_relative);

    JPanel shape_panel = new JPanel(new GridLayout(3, 2));
    shape_panel.setBorder(BorderFactory.createTitledBorder("Edge shape"));
    e_line = new JRadioButton("line");
    e_line.addActionListener(this);
    e_line.setSelected(true);
    //        e_bent = new JRadioButton("bent line");
    //        e_bent.addActionListener(this);
    e_wedge = new JRadioButton("wedge");
    e_wedge.addActionListener(this);
    e_quad = new JRadioButton("quad curve");
    e_quad.addActionListener(this);
    e_cubic = new JRadioButton("cubic curve");
    e_cubic.addActionListener(this);
    e_ortho = new JRadioButton("orthogonal");
    e_ortho.addActionListener(this);
    ButtonGroup bg_shape = new ButtonGroup();
    bg_shape.add(e_line);
    //        bg.add(e_bent);
    bg_shape.add(e_wedge);
    bg_shape.add(e_quad);
    bg_shape.add(e_ortho);
    bg_shape.add(e_cubic);
    shape_panel.add(e_line);
    //        shape_panel.add(e_bent);
    shape_panel.add(e_wedge);
    shape_panel.add(e_quad);
    shape_panel.add(e_cubic);
    shape_panel.add(e_ortho);
    fill_edges = new JCheckBox("fill edge shapes");
    fill_edges.setSelected(false);
    fill_edges.addActionListener(this);
    shape_panel.add(fill_edges);
    shape_panel.setOpaque(true);
    e_color = new JCheckBox("highlight edge weights");
    e_color.addActionListener(this);
    e_labels = new JCheckBox("show edge weight values");
    e_labels.addActionListener(this);
    e_uarrow_pred = new JCheckBox("undirected");
    e_uarrow_pred.addActionListener(this);
    e_darrow_pred = new JCheckBox("directed");
    e_darrow_pred.addActionListener(this);
    e_darrow_pred.setSelected(true);
    JPanel arrow_panel = new JPanel(new GridLayout(1, 0));
    arrow_panel.setBorder(BorderFactory.createTitledBorder("Show arrows"));
    arrow_panel.add(e_uarrow_pred);
    arrow_panel.add(e_darrow_pred);

    e_show_d = new JCheckBox("directed");
    e_show_d.addActionListener(this);
    e_show_d.setSelected(true);
    e_show_u = new JCheckBox("undirected");
    e_show_u.addActionListener(this);
    e_show_u.setSelected(true);
    JPanel show_edge_panel = new JPanel(new GridLayout(1, 0));
    show_edge_panel.setBorder(BorderFactory.createTitledBorder("Show edges"));
    show_edge_panel.add(e_show_u);
    show_edge_panel.add(e_show_d);

    shape_panel.setAlignmentX(Component.LEFT_ALIGNMENT);
    edge_panel.add(shape_panel);
    gradient_panel.setAlignmentX(Component.LEFT_ALIGNMENT);
    edge_panel.add(gradient_panel);
    show_edge_panel.setAlignmentX(Component.LEFT_ALIGNMENT);
    edge_panel.add(show_edge_panel);
    arrow_panel.setAlignmentX(Component.LEFT_ALIGNMENT);
    edge_panel.add(arrow_panel);

    e_color.setAlignmentX(Component.LEFT_ALIGNMENT);
    edge_panel.add(e_color);
    e_labels.setAlignmentX(Component.LEFT_ALIGNMENT);
    edge_panel.add(e_labels);

    // set up zoom controls
    zoom_at_mouse = new JCheckBox("<html><center>zoom at mouse<p>(wheel only)</center></html>");
    zoom_at_mouse.addActionListener(this);
    zoom_at_mouse.setSelected(true);

    final ScalingControl scaler = new CrossoverScalingControl();

    JButton plus = new JButton("+");
    plus.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            scaler.scale(vv, 1.1f, vv.getCenter());
        }
    });
    JButton minus = new JButton("-");
    minus.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            scaler.scale(vv, 1 / 1.1f, vv.getCenter());
        }
    });

    JPanel zoomPanel = new JPanel();
    zoomPanel.setBorder(BorderFactory.createTitledBorder("Zoom"));
    plus.setAlignmentX(Component.CENTER_ALIGNMENT);
    zoomPanel.add(plus);
    minus.setAlignmentX(Component.CENTER_ALIGNMENT);
    zoomPanel.add(minus);
    zoom_at_mouse.setAlignmentX(Component.CENTER_ALIGNMENT);
    zoomPanel.add(zoom_at_mouse);

    JPanel fontPanel = new JPanel();
    // add font and zoom controls to center panel
    font = new JCheckBox("bold text");
    font.addActionListener(this);
    font.setAlignmentX(Component.CENTER_ALIGNMENT);
    fontPanel.add(font);

    both_panel.add(zoomPanel);
    both_panel.add(fontPanel);

    JComboBox modeBox = gm.getModeComboBox();
    modeBox.setAlignmentX(Component.CENTER_ALIGNMENT);
    JPanel modePanel = new JPanel(new BorderLayout()) {
        public Dimension getMaximumSize() {
            return getPreferredSize();
        }
    };
    modePanel.setBorder(BorderFactory.createTitledBorder("Mouse Mode"));
    modePanel.add(modeBox);
    JPanel comboGrid = new JPanel(new GridLayout(0, 1));
    comboGrid.add(modePanel);
    fontPanel.add(comboGrid);

    JComboBox cb = new JComboBox();
    cb.addItem(Renderer.VertexLabel.Position.N);
    cb.addItem(Renderer.VertexLabel.Position.NE);
    cb.addItem(Renderer.VertexLabel.Position.E);
    cb.addItem(Renderer.VertexLabel.Position.SE);
    cb.addItem(Renderer.VertexLabel.Position.S);
    cb.addItem(Renderer.VertexLabel.Position.SW);
    cb.addItem(Renderer.VertexLabel.Position.W);
    cb.addItem(Renderer.VertexLabel.Position.NW);
    cb.addItem(Renderer.VertexLabel.Position.N);
    cb.addItem(Renderer.VertexLabel.Position.CNTR);
    cb.addItem(Renderer.VertexLabel.Position.AUTO);
    cb.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            Renderer.VertexLabel.Position position = (Renderer.VertexLabel.Position) e.getItem();
            vv.getRenderer().getVertexLabelRenderer().setPosition(position);
            vv.repaint();
        }
    });
    cb.setSelectedItem(Renderer.VertexLabel.Position.SE);
    JPanel positionPanel = new JPanel();
    positionPanel.setBorder(BorderFactory.createTitledBorder("Label Position"));
    positionPanel.add(cb);

    comboGrid.add(positionPanel);

}

From source file:uk.nhs.cfh.dsp.yasb.searchpanel.SearchPanel.java

/**
 * Creates the control panel./*from  ww  w  .  j ava2  s. c om*/
 */
private synchronized void createControlPanel() {
    // set controls for rendering concept ids and labels in a collapsible pane
    controlsPane = new JXCollapsiblePane(new GridLayout(0, 1));
    controlsPane.setName("controlsPane");

    final JComboBox conceptTypeBox = new JComboBox(ConceptType.values());
    // set default value to UNKNOWN, which will return all types
    conceptTypeBox.setSelectedItem(ConceptType.UNKNOWN);

    conceptTypeBox.setAction(new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            // get selected value
            Object selection = conceptTypeBox.getSelectedItem();
            if (selection instanceof ConceptType) {
                selectedConceptType = (ConceptType) conceptTypeBox.getSelectedItem();
                doSearch();
            }

        }
    });
    JPanel panel1 = new JPanel();
    panel1.setLayout(new BoxLayout(panel1, BoxLayout.LINE_AXIS));
    panel1.add(new JLabel("Concept Type"));
    panel1.add(Box.createHorizontalStrut(10));
    panel1.add(conceptTypeBox);

    final JComboBox statusBox = new JComboBox(ComponentStatus.values());
    statusBox.setSelectedItem(ComponentStatus.CURRENT);
    statusBox.setAction(new AbstractAction() {

        public void actionPerformed(ActionEvent arg0) {

            Object selection = statusBox.getSelectedItem();
            if (selection instanceof ComponentStatus) {
                selectedConceptStatus = (ComponentStatus) statusBox.getSelectedItem();
                doSearch();
            }
        }
    });
    JPanel panel2 = new JPanel();
    panel2.setLayout(new BoxLayout(panel2, BoxLayout.LINE_AXIS));
    panel2.add(new JLabel("Concept Status"));
    panel2.add(Box.createHorizontalStrut(10));
    panel2.add(statusBox);

    // create stemming enabling checkbox
    final JCheckBox enableStemmingCheckBox = new JCheckBox();
    enableStemmingCheckBox.setAction(new AbstractAction("Enable Stemming") {
        public void actionPerformed(ActionEvent arg0) {
            if (enableStemmingCheckBox.isSelected()) {
                // change analyser
                selectedAnalyzer = new SnowballAnalyzer("English");
                logger.debug("Enabled Stemming");
                doSearch();
            } else {
                selectedAnalyzer = new StandardAnalyzer();
                logger.debug("Disabled Stemming");
                doSearch();
            }
        }

    });

    renderConceptIdsBox = new JCheckBox(new AbstractAction("Render Concept IDs") {

        public void actionPerformed(ActionEvent e) {
            // toggle status in tree cell renderer
            renderer.setRenderConceptId(renderConceptIdsBox.isSelected());
            // refresh tree
            SwingUtilities.updateComponentTreeUI(resultsList);
        }
    });
    renderConceptIdsBox.setName("preferFSNOverPTJCheckBox");

    preferFSNOverPTJCheckBox = new JCheckBox(new AbstractAction("Prefer FSN over PT") {
        public void actionPerformed(ActionEvent e) {
            // toggle preference in renderer
            renderer.setPreferFSNOverPT(preferFSNOverPTJCheckBox.isSelected());
            // refresh tree
            SwingUtilities.updateComponentTreeUI(resultsList);
        }
    });
    preferFSNOverPTJCheckBox.setName("preferFSNOverPTJCheckBox");

    // add panels to controlsPane
    controlsPane.add(panel1);
    controlsPane.add(panel2);
    controlsPane.add(enableStemmingCheckBox);
    controlsPane.add(renderConceptIdsBox);
    controlsPane.add(preferFSNOverPTJCheckBox);
    controlsPane.setCollapsed(true);
}

From source file:UNUSED.JUNGsamples.ShowLayouts.java

private static JPanel getGraphPanel() {
    g_array = (Graph<? extends Object, ? extends Object>[]) new Graph<?, ?>[graph_names.length];

    Factory<Graph<Integer, Number>> graphFactory = new Factory<Graph<Integer, Number>>() {
        public Graph<Integer, Number> create() {
            return new SparseMultigraph<Integer, Number>();
        }/*w  w w.  ja v  a2  s.  co m*/
    };

    Factory<Integer> vertexFactory = new Factory<Integer>() {
        int count;

        public Integer create() {
            return count++;
        }
    };
    Factory<Number> edgeFactory = new Factory<Number>() {
        int count;

        public Number create() {
            return count++;
        }
    };

    g_array[0] = TestGraphs.createTestGraph(false);
    g_array[1] = MixedRandomGraphGenerator.generateMixedRandomGraph(graphFactory, vertexFactory, edgeFactory,
            new HashMap<Number, Number>(), 20, true, new HashSet<Integer>());
    g_array[2] = TestGraphs.getDemoGraph();
    g_array[3] = TestGraphs.createDirectedAcyclicGraph(6, 40, 0.3);
    g_array[4] = TestGraphs.getOneComponentGraph();
    g_array[5] = TestGraphs.createChainPlusIsolates(60, 5);
    g_array[6] = TestGraphs.createChainPlusIsolates(0, 40);

    Graph<? extends Object, ? extends Object> g = g_array[4]; // initial graph

    final VisualizationViewer<Integer, Number> vv = new VisualizationViewer<Integer, Number>(new FRLayout(g));

    vv.getRenderContext().setVertexFillPaintTransformer(
            new PickableVertexPaintTransformer<Integer>(vv.getPickedVertexState(), Color.red, Color.yellow));

    final DefaultModalGraphMouse<Integer, Number> graphMouse = new DefaultModalGraphMouse<Integer, Number>();
    vv.setGraphMouse(graphMouse);

    final ScalingControl scaler = new CrossoverScalingControl();

    JButton plus = new JButton("+");
    plus.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            scaler.scale(vv, 1.1f, vv.getCenter());
        }
    });
    JButton minus = new JButton("-");
    minus.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            scaler.scale(vv, 1 / 1.1f, vv.getCenter());
        }
    });
    JButton reset = new JButton("reset");
    reset.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Layout<Integer, Number> layout = vv.getGraphLayout();
            layout.initialize();
            Relaxer relaxer = vv.getModel().getRelaxer();
            if (relaxer != null) {
                //            if(layout instanceof IterativeContext) {
                relaxer.stop();
                relaxer.prerelax();
                relaxer.relax();
            }
        }
    });

    JComboBox modeBox = graphMouse.getModeComboBox();
    modeBox.addItemListener(((DefaultModalGraphMouse<Integer, Number>) vv.getGraphMouse()).getModeListener());

    JPanel jp = new JPanel();
    jp.setBackground(Color.WHITE);
    jp.setLayout(new BorderLayout());
    jp.add(vv, BorderLayout.CENTER);
    Class[] combos = getCombos();
    final JComboBox jcb = new JComboBox(combos);
    // use a renderer to shorten the layout name presentation
    jcb.setRenderer(new DefaultListCellRenderer() {
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
                boolean cellHasFocus) {
            String valueString = value.toString();
            valueString = valueString.substring(valueString.lastIndexOf('.') + 1);
            return super.getListCellRendererComponent(list, valueString, index, isSelected, cellHasFocus);
        }
    });
    jcb.addActionListener(new LayoutChooser(jcb, vv));
    jcb.setSelectedItem(FRLayout.class);

    JPanel control_panel = new JPanel(new GridLayout(2, 1));
    JPanel topControls = new JPanel();
    JPanel bottomControls = new JPanel();
    control_panel.add(topControls);
    control_panel.add(bottomControls);
    jp.add(control_panel, BorderLayout.NORTH);

    final JComboBox graph_chooser = new JComboBox(graph_names);

    graph_chooser.addActionListener(new GraphChooser(jcb));

    topControls.add(jcb);
    topControls.add(graph_chooser);
    bottomControls.add(plus);
    bottomControls.add(minus);
    bottomControls.add(modeBox);
    bottomControls.add(reset);
    return jp;
}