Example usage for javax.swing JComboBox JComboBox

List of usage examples for javax.swing JComboBox JComboBox

Introduction

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

Prototype

public JComboBox(Vector<E> items) 

Source Link

Document

Creates a JComboBox that contains the elements in the specified Vector.

Usage

From source file:cool.pandora.modeller.ui.BagTableFormBuilder.java

/**
 * addList./*from www  .  j a  v a2s  .  c om*/
 *
 * @param isRequired   boolean
 * @param label        String
 * @param elements     Collection
 * @param defaultValue String
 * @param checkbox     JComponent
 * @return addBinding
 */
public JComponent[] addList(final boolean isRequired, final String label, final Collection<String> elements,
        final String defaultValue, final JComponent checkbox) {
    final ArrayList<String> listModel = new ArrayList<>();
    listModel.addAll(elements);

    // Set default value selected from value list
    final JComboBox<String> dropDownTextField = new JComboBox<>(
            listModel.toArray(new String[listModel.size()]));
    dropDownTextField.setSelectedItem(defaultValue);
    final Object obj = dropDownTextField.getSelectedItem();
    dropDownTextField.setSelectedItem(obj);

    return addBinding(isRequired, label, dropDownTextField, checkbox);
}

From source file:BlendCompositeDemo.java

public BlendCompositeDemo() {
    super("Blend Composites");

    compositeTestPanel = new CompositeTestPanel();
    compositeTestPanel.setComposite(BlendComposite.Average);
    add(compositeTestPanel);/*  w  w  w . ja  v a  2 s  .  c  om*/

    combo = new JComboBox(BlendComposite.BlendingMode.values());
    combo.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            compositeTestPanel.setComposite(BlendComposite.getInstance(
                    BlendComposite.BlendingMode.valueOf(combo.getSelectedItem().toString()),
                    slider.getValue() / 100.0f));
        }
    });

    slider = new JSlider(0, 100, 100);
    slider.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            BlendComposite blend = (BlendComposite) compositeTestPanel.getComposite();
            blend = blend.derive(slider.getValue() / 100.0f);
            compositeTestPanel.setComposite(blend);
        }
    });

    JPanel controls = new JPanel(new FlowLayout(FlowLayout.LEFT));
    controls.add(combo);
    controls.add(new JLabel("0%"));
    controls.add(slider);
    controls.add(new JLabel("100%"));
    add(controls, BorderLayout.SOUTH);

    pack();
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

From source file:net.chaosserver.timelord.swingui.AddTimeDialog.java

/**
 * Builds the Add Time Panel.//from   ww  w .  ja v a  2 s .c  o  m
 *
 * @return the newly created add time panel
 */
public JPanel buildAddTimePanel() {
    JPanel addTimePanel = new JPanel();

    Collection<TimelordTask> taskCollection = timelordData.getTaskCollection();

    Vector<TimelordTask> taskVector = new Vector<TimelordTask>(taskCollection);

    comboBox = new JComboBox(taskVector);
    comboBox.setEditable(true);
    addTimePanel.add(comboBox);

    JButton button = new JButton("+0.25");
    button.setActionCommand(ACTION_OK);
    button.addActionListener(this);
    addTimePanel.add(button);

    return addTimePanel;
}

From source file:events.TableListSelectionDemo.java

public TableListSelectionDemo() {
    super(new BorderLayout());

    String[] columnNames = { "French", "Spanish", "Italian" };
    String[][] tableData = { { "un", "uno", "uno" }, { "deux", "dos", "due" }, { "trois", "tres", "tre" },
            { "quatre", "cuatro", "quattro" }, { "cinq", "cinco", "cinque" }, { "six", "seis", "sei" },
            { "sept", "siete", "sette" } };

    table = new JTable(tableData, columnNames);
    listSelectionModel = table.getSelectionModel();
    listSelectionModel.addListSelectionListener(new SharedListSelectionHandler());
    table.setSelectionModel(listSelectionModel);
    JScrollPane tablePane = new JScrollPane(table);

    //Build control area (use default FlowLayout).
    JPanel controlPane = new JPanel();
    String[] modes = { "SINGLE_SELECTION", "SINGLE_INTERVAL_SELECTION", "MULTIPLE_INTERVAL_SELECTION" };

    final JComboBox comboBox = new JComboBox(modes);
    comboBox.setSelectedIndex(2);//from  w  ww  .j  ava  2  s.c  o  m
    comboBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String newMode = (String) comboBox.getSelectedItem();
            if (newMode.equals("SINGLE_SELECTION")) {
                listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            } else if (newMode.equals("SINGLE_INTERVAL_SELECTION")) {
                listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
            } else {
                listSelectionModel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
            }
            output.append("----------" + "Mode: " + newMode + "----------" + newline);
        }
    });
    controlPane.add(new JLabel("Selection mode:"));
    controlPane.add(comboBox);

    //Build output area.
    output = new JTextArea(1, 10);
    output.setEditable(false);
    JScrollPane outputPane = new JScrollPane(output, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    //Do the layout.
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    add(splitPane, BorderLayout.CENTER);

    JPanel topHalf = new JPanel();
    topHalf.setLayout(new BoxLayout(topHalf, BoxLayout.LINE_AXIS));
    JPanel listContainer = new JPanel(new GridLayout(1, 1));
    JPanel tableContainer = new JPanel(new GridLayout(1, 1));
    tableContainer.setBorder(BorderFactory.createTitledBorder("Table"));
    tableContainer.add(tablePane);
    tablePane.setPreferredSize(new Dimension(420, 130));
    topHalf.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5));
    topHalf.add(listContainer);
    topHalf.add(tableContainer);

    topHalf.setMinimumSize(new Dimension(250, 50));
    topHalf.setPreferredSize(new Dimension(200, 110));
    splitPane.add(topHalf);

    JPanel bottomHalf = new JPanel(new BorderLayout());
    bottomHalf.add(controlPane, BorderLayout.PAGE_START);
    bottomHalf.add(outputPane, BorderLayout.CENTER);
    //XXX: next line needed if bottomHalf is a scroll pane:
    //bottomHalf.setMinimumSize(new Dimension(400, 50));
    bottomHalf.setPreferredSize(new Dimension(450, 110));
    splitPane.add(bottomHalf);
}

From source file:ComboBoxDemo2.java

public ComboBoxDemo2() {
    setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    String[] patternExamples = { "dd MMMMM yyyy", "dd.MM.yy", "MM/dd/yy", "yyyy.MM.dd G 'at' hh:mm:ss z",
            "EEE, MMM d, ''yy", "h:mm a", "H:mm:ss:SSS", "K:mm a,z", "yyyy.MMMMM.dd GGG hh:mm aaa" };

    currentPattern = patternExamples[0];

    //Set up the UI for selecting a pattern.
    JLabel patternLabel1 = new JLabel("Enter the pattern string or");
    JLabel patternLabel2 = new JLabel("select one from the list:");

    JComboBox patternList = new JComboBox(patternExamples);
    patternList.setEditable(true);/*w  ww .  j a va 2 s. c om*/
    patternList.addActionListener(this);

    //Create the UI for displaying result.
    JLabel resultLabel = new JLabel("Current Date/Time", JLabel.LEADING); //==
    // LEFT
    result = new JLabel(" ");
    result.setForeground(Color.black);
    result.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.black),
            BorderFactory.createEmptyBorder(5, 5, 5, 5)));

    //Lay out everything.
    JPanel patternPanel = new JPanel();
    patternPanel.setLayout(new BoxLayout(patternPanel, BoxLayout.PAGE_AXIS));
    patternPanel.add(patternLabel1);
    patternPanel.add(patternLabel2);
    patternList.setAlignmentX(Component.LEFT_ALIGNMENT);
    patternPanel.add(patternList);

    JPanel resultPanel = new JPanel(new GridLayout(0, 1));
    resultPanel.add(resultLabel);
    resultPanel.add(result);

    patternPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
    resultPanel.setAlignmentX(Component.LEFT_ALIGNMENT);

    add(patternPanel);
    add(Box.createRigidArea(new Dimension(0, 10)));
    add(resultPanel);

    setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

    reformat();
}

From source file:MainClass.java

protected void buildChooser() {
    comboBox = new JComboBox(labels);
    comboBox.addItemListener(this);
    add(comboBox);
}

From source file:SimpleDateFormatDemo.java

public SimpleDateFormatDemo() {

    today = new Date();
    availableLocales = new LocaleGroup();

    String[] patternExamples = { "dd MMMMM yyyy", "dd.MM.yy", "MM/dd/yy", "yyyy.MM.dd G 'at' hh:mm:ss z",
            "EEE, MMM d, ''yy", "h:mm a", "H:mm:ss:SSS", "K:mm a,z", "yyyy.MMMMM.dd GGG hh:mm aaa" };

    currentPattern = patternExamples[0];

    // Set up the UI for selecting a pattern.
    JLabel patternLabel1 = new JLabel("Enter the pattern string or");
    JLabel patternLabel2 = new JLabel("select one from the list:");
    patternLabel1.setAlignmentX(Component.LEFT_ALIGNMENT);
    patternLabel2.setAlignmentX(Component.LEFT_ALIGNMENT);

    JComboBox patternList = new JComboBox(patternExamples);
    patternList.setSelectedIndex(0);//ww w.  j  av a 2s.  c om
    patternList.setEditable(true);
    patternList.setAlignmentX(Component.LEFT_ALIGNMENT);
    PatternListener patternListener = new PatternListener();
    patternList.addActionListener(patternListener);

    // Set up the UI for selecting a locale.
    JLabel localeLabel = new JLabel("Select a Locale from the list:");
    localeLabel.setAlignmentX(Component.LEFT_ALIGNMENT);

    JComboBox localeList = new JComboBox(availableLocales.getStrings());
    localeList.setSelectedIndex(0);
    localeList.setAlignmentX(Component.LEFT_ALIGNMENT);
    LocaleListener localeListener = new LocaleListener();
    localeList.addActionListener(localeListener);

    // Create the UI for displaying result
    JLabel resultLabel = new JLabel("Current Date and Time", JLabel.LEFT);
    resultLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
    result = new JLabel(" ");
    result.setForeground(Color.black);
    result.setAlignmentX(Component.LEFT_ALIGNMENT);
    result.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.black),
            BorderFactory.createEmptyBorder(5, 5, 5, 5)));

    // Lay out everything
    JPanel patternPanel = new JPanel();
    patternPanel.setLayout(new BoxLayout(patternPanel, BoxLayout.Y_AXIS));
    patternPanel.add(patternLabel1);
    patternPanel.add(patternLabel2);
    patternPanel.add(patternList);

    JPanel localePanel = new JPanel();
    localePanel.setLayout(new BoxLayout(localePanel, BoxLayout.Y_AXIS));
    localePanel.add(localeLabel);
    localePanel.add(localeList);

    JPanel resultPanel = new JPanel();
    resultPanel.setLayout(new GridLayout(0, 1));
    resultPanel.add(resultLabel);
    resultPanel.add(result);

    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    patternPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
    localePanel.setAlignmentX(Component.CENTER_ALIGNMENT);
    resultPanel.setAlignmentX(Component.CENTER_ALIGNMENT);

    add(patternPanel);
    add(Box.createVerticalStrut(10));
    add(localePanel);
    add(Box.createVerticalStrut(10));
    add(resultPanel);

    setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

    reformat();

}

From source file:cl.almejo.vsim.gui.actions.preferences.ColorPreferences.java

private JComboBox<String> createSchemeCombobox() {
    JComboBox<String> _comboBox = new JComboBox<String>(ColorScheme.getNames());
    _comboBox.addActionListener(new ActionListener() {
        @Override/* ww w.  j ava 2  s .  c om*/
        public void actionPerformed(ActionEvent e) {
            JComboBox combobox = (JComboBox) e.getSource();
            ColorScheme.setCurrent((String) combobox.getSelectedItem());
        }
    });
    _comboBox.setSelectedItem("default");
    return _comboBox;
}

From source file:events.FocusEventDemo.java

public void addComponentsToPane(final Container pane) {
    GridBagLayout gridbag = new GridBagLayout();
    pane.setLayout(gridbag);//from   www.j  a v a  2  s.  c om

    GridBagConstraints c = new GridBagConstraints();

    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 1.0; //Make column as wide as possible.
    JTextField textField = new JTextField("A TextField");
    textField.setMargin(new Insets(0, 2, 0, 2));
    textField.addFocusListener(this);
    gridbag.setConstraints(textField, c);
    add(textField);

    c.weightx = 0.1; //Widen every other column a bit, when possible.
    c.fill = GridBagConstraints.NONE;
    JLabel label = new JLabel("A Label");
    label.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
    label.addFocusListener(this);
    gridbag.setConstraints(label, c);
    add(label);

    String comboPrefix = "ComboBox Item #";
    final int numItems = 15;
    Vector<String> vector = new Vector<String>(numItems);
    for (int i = 0; i < numItems; i++) {
        vector.addElement(comboPrefix + i);
    }
    JComboBox comboBox = new JComboBox(vector);
    comboBox.addFocusListener(this);
    gridbag.setConstraints(comboBox, c);
    add(comboBox);

    c.gridwidth = GridBagConstraints.REMAINDER;
    JButton button = new JButton("A Button");
    button.addFocusListener(this);
    gridbag.setConstraints(button, c);
    add(button);

    c.weightx = 0.0;
    c.weighty = 0.1;
    c.fill = GridBagConstraints.BOTH;
    String listPrefix = "List Item #";
    Vector<String> listVector = new Vector<String>(numItems);
    for (int i = 0; i < numItems; i++) {
        listVector.addElement(listPrefix + i);
    }
    JList list = new JList(listVector);
    list.setSelectedIndex(1); //It's easier to see the focus change
    //if an item is selected.
    list.addFocusListener(this);
    JScrollPane listScrollPane = new JScrollPane(list);

    gridbag.setConstraints(listScrollPane, c);
    add(listScrollPane);

    c.weighty = 1.0; //Make this row as tall as possible.
    c.gridheight = GridBagConstraints.REMAINDER;
    //Set up the area that reports focus-gained and focus-lost events.
    display = new JTextArea();
    display.setEditable(false);
    //The setRequestFocusEnabled method prevents a
    //component from being clickable, but it can still
    //get the focus through the keyboard - this ensures
    //user accessibility.
    display.setRequestFocusEnabled(false);
    display.addFocusListener(this);
    JScrollPane displayScrollPane = new JScrollPane(display);

    gridbag.setConstraints(displayScrollPane, c);
    add(displayScrollPane);
    setPreferredSize(new Dimension(450, 450));
    ((JPanel) pane).setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
}

From source file:XPathTest.java

public XPathFrame() {
        setTitle("XPathTest");

        JMenu fileMenu = new JMenu("File");
        JMenuItem openItem = new JMenuItem("Open");
        openItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                openFile();/* w ww  .ja v  a 2  s  .  c o  m*/
            }
        });
        fileMenu.add(openItem);

        JMenuItem exitItem = new JMenuItem("Exit");
        exitItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }
        });
        fileMenu.add(exitItem);

        JMenuBar menuBar = new JMenuBar();
        menuBar.add(fileMenu);
        setJMenuBar(menuBar);

        ActionListener listener = new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                evaluate();
            }
        };
        expression = new JTextField(20);
        expression.addActionListener(listener);
        JButton evaluateButton = new JButton("Evaluate");
        evaluateButton.addActionListener(listener);

        typeCombo = new JComboBox(new Object[] { "STRING", "NODE", "NODESET", "NUMBER", "BOOLEAN" });
        typeCombo.setSelectedItem("STRING");

        JPanel panel = new JPanel();
        panel.add(expression);
        panel.add(typeCombo);
        panel.add(evaluateButton);
        docText = new JTextArea(10, 40);
        result = new JTextField();
        result.setBorder(new TitledBorder("Result"));

        add(panel, BorderLayout.NORTH);
        add(new JScrollPane(docText), BorderLayout.CENTER);
        add(result, BorderLayout.SOUTH);

        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            builder = factory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            JOptionPane.showMessageDialog(this, e);
        }

        XPathFactory xpfactory = XPathFactory.newInstance();
        path = xpfactory.newXPath();
        pack();
    }