Example usage for javax.swing JComboBox setSelectedIndex

List of usage examples for javax.swing JComboBox setSelectedIndex

Introduction

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

Prototype

@BeanProperty(bound = false, preferred = true, description = "The item at index is selected.")
public void setSelectedIndex(int anIndex) 

Source Link

Document

Selects the item at index anIndex.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Object[] options = { "Option 1", "Option 2", "Option 3", "Option 4", "Option 5", "Option 6", "Option 7",
            "None of the above" };
    JComboBox optionList = new JComboBox(options);
    optionList.setSelectedIndex(7);
    JOptionPane.showMessageDialog(null, optionList, "Title", JOptionPane.QUESTION_MESSAGE);
}

From source file:JComboBoxTest.java

public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("JComboBox Test");
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    String[] selections = { "green", "red", "orange", "dark blue" };
    JComboBox comboBox = new JComboBox(selections);
    comboBox.setSelectedIndex(1);
    System.out.println(comboBox.getSelectedItem());
    frame.add(comboBox);/*from  w w  w.jav  a  2 s  .c o m*/
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    Object[] options = { "Option 1", "Option 2", "Option 3", "None of the above" };
    JComboBox optionControl = new JComboBox(options);
    optionControl.setSelectedIndex(3);
    JOptionPane.showMessageDialog(null, optionControl, "Option", JOptionPane.QUESTION_MESSAGE);
    System.out.println(optionControl.getSelectedItem());

    String graphSelection = (String) JOptionPane.showInputDialog(null, "Choose from the following options...",
            "Choose From DropDown", JOptionPane.QUESTION_MESSAGE, null, options, options[3]);
    System.out.println(graphSelection);

    JOptionPane.showMessageDialog(null, optionControl, "Option", JOptionPane.QUESTION_MESSAGE);
}

From source file:Main.java

public static void main(String[] args) {
    JTextField account = new JTextField(10);
    JPanel accountPanel = new JPanel(new GridLayout());
    accountPanel.add(account);// ww  w.j ava  2s  . co m
    accountPanel.setBorder(new TitledBorder("Account"));

    String[] firstDigitList = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };

    JLabel firstDigitListLabel = new JLabel("Leading Digit Change");
    JPanel firstDigitListPanel = new JPanel(new BorderLayout(4, 2));
    firstDigitListPanel.add(firstDigitListLabel, BorderLayout.WEST);
    JComboBox firstDigitCombo = new JComboBox(firstDigitList);
    firstDigitListPanel.add(firstDigitCombo);
    firstDigitCombo.setSelectedIndex(0);
    firstDigitListPanel.setBorder(new TitledBorder("LDC"));

    JPanel panel = new JPanel();
    panel.add(accountPanel);
    panel.add(firstDigitListPanel);

    int result = JOptionPane.showConfirmDialog(null, panel, "Please Enter Values",
            JOptionPane.OK_CANCEL_OPTION);
}

From source file:Main.java

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

    JPanel mainPanel = new JPanel();
    JPanel buttonsPanel = new JPanel();
    frame.add(mainPanel);//from  w  w w. j  av a 2 s.co  m
    frame.add(buttonsPanel, BorderLayout.SOUTH);

    String[] options = { "S", "G", "I", "T" };

    JComboBox comboBox = new JComboBox(options);
    comboBox.setRenderer(new MyComboBoxRenderer("COUNTRY"));
    comboBox.setSelectedIndex(-1);
    mainPanel.add(comboBox);

    JButton clearSelectionButton = new JButton("Clear selection");
    clearSelectionButton.addActionListener(e -> {
        comboBox.setSelectedIndex(-1);
    });
    buttonsPanel.add(clearSelectionButton);

    frame.pack();
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String[] items = new String[] { "", "Apple", "Banana", "Carrot" };
    Color bgColor = UIManager.getColor("TextField.background");

    UIManager.put("ComboBox.selectionBackground", new ColorUIResource(bgColor));

    JComboBox<String> combo1 = new JComboBox<>(items);
    combo1.setPrototypeDisplayValue("XXXXXXXXXXXXXXX");
    combo1.setEditable(true);//w  ww  . j  a v a2 s  .c om
    combo1.setSelectedIndex(-1);

    JComboBox<String> combo2 = new JComboBox<>(items);
    combo2.setPrototypeDisplayValue("XXXXXXXXXXXXXXX");
    combo2.setEditable(false);
    combo2.setSelectedIndex(-1);
    combo2.setBackground(bgColor);

    JFrame frame = new JFrame();
    Container c = frame.getContentPane();
    c.setLayout(new FlowLayout());
    c.add(combo1);
    c.add(combo2);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 100);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String args[]) {
    String rows[][] = { { "A", "a" }, { "B", "b" }, { "E", "e" } };
    String headers[] = { "Upper", "Lower" };

    final int modeKey[] = { JTable.AUTO_RESIZE_ALL_COLUMNS, JTable.AUTO_RESIZE_LAST_COLUMN,
            JTable.AUTO_RESIZE_NEXT_COLUMN, JTable.AUTO_RESIZE_OFF, JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS };

    final JTable table = new JTable(rows, headers);
    JScrollPane scrollPane = new JScrollPane(table);

    String modes[] = { "Resize All Columns", "Resize Last Column", "Resize Next Column", "Resize Off",
            "Resize Subsequent Columns" };

    final JComboBox resizeModeComboBox = new JComboBox(modes);
    int defaultMode = 4;
    table.setAutoResizeMode(modeKey[defaultMode]);
    resizeModeComboBox.setSelectedIndex(defaultMode);
    ItemListener itemListener = new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            int index = resizeModeComboBox.getSelectedIndex();
            table.setAutoResizeMode(modeKey[index]);
        }//  w  w w  . ja  v a  2  s  .  c  o m
    };
    resizeModeComboBox.addItemListener(itemListener);

    JFrame frame = new JFrame("Resizing Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(resizeModeComboBox, BorderLayout.NORTH);
    frame.add(scrollPane, BorderLayout.CENTER);

    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JComboBox<String> my = new JComboBox<>(new String[] { "HELLO WORLD", "java2s.com" });
    JFrame frame = new JFrame("");
    frame.add(my);/*from  ww w . j a  v a 2  s  .com*/
    frame.pack();
    frame.setVisible(true);
    my.setSelectedIndex(1);
}

From source file:ResizeTable.java

public static void main(String args[]) {

    Object rowData[][] = { { "1", "one", "ichi - \u4E00", "un", "I" },
            { "2", "two", "ni - \u4E8C", "deux", "II" }, { "3", "three", "san - \u4E09", "trois", "III" },
            { "4", "four", "shi - \u56DB", "quatre", "IV" }, { "5", "five", "go - \u4E94", "cinq", "V" },
            { "6", "six", "roku - \u516D", "treiza", "VI" }, { "7", "seven", "shichi - \u4E03", "sept", "VII" },
            { "8", "eight", "hachi - \u516B", "huit", "VIII" }, { "9", "nine", "kyu - \u4E5D", "neur", "IX" },
            { "10", "ten", "ju - \u5341", "dix", "X" } };

    String columnNames[] = { "#", "English", "Japanese", "French", "Roman" };

    final JTable table = new JTable(rowData, columnNames);
    JScrollPane scrollPane = new JScrollPane(table);

    String modes[] = { "Resize All Columns", "Resize Last Column", "Resize Next Column", "Resize Off",
            "Resize Subsequent Columns" };
    final int modeKey[] = { JTable.AUTO_RESIZE_ALL_COLUMNS, JTable.AUTO_RESIZE_LAST_COLUMN,
            JTable.AUTO_RESIZE_NEXT_COLUMN, JTable.AUTO_RESIZE_OFF, JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS };
    JComboBox resizeModeComboBox = new JComboBox(modes);
    int defaultMode = 4;
    table.setAutoResizeMode(modeKey[defaultMode]);
    resizeModeComboBox.setSelectedIndex(defaultMode);
    ItemListener itemListener = new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            JComboBox source = (JComboBox) e.getSource();
            int index = source.getSelectedIndex();
            table.setAutoResizeMode(modeKey[index]);
        }//from   w  ww. ja  v  a  2s  .c o  m
    };
    resizeModeComboBox.addItemListener(itemListener);

    JFrame frame = new JFrame("Resizing Table");
    Container contentPane = frame.getContentPane();

    contentPane.add(resizeModeComboBox, BorderLayout.NORTH);
    contentPane.add(scrollPane, BorderLayout.CENTER);

    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JPanel panel = new JPanel(new BorderLayout());
    String[] values = new String[] { "One", "Two", "Three" };
    JComboBox<String> comboBox = new JComboBox<>(values);
    panel.add(comboBox, BorderLayout.NORTH);
    JTextArea textArea = new JTextArea(2, 2);
    panel.add(textArea, BorderLayout.CENTER);
    JButton button = new JButton("Action");
    button.addActionListener(new ActionListener() {
        @Override/*from w w w. j  a v  a2  s  .c  om*/
        public void actionPerformed(ActionEvent e) {
            textArea.setText((String) comboBox.getSelectedItem());
            comboBox.setSelectedIndex(0);
        }
    });
    panel.add(button, BorderLayout.SOUTH);
    JFrame frame = new JFrame();
    frame.add(panel);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}