List of usage examples for javax.swing JComboBox setSelectedIndex
@BeanProperty(bound = false, preferred = true, description = "The item at index is selected.") public void setSelectedIndex(int anIndex)
anIndex
. From source file:DateTimeEditor.java
public static void main(String[] args) { JFrame frame = new JFrame(); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { System.exit(0);/* ww w . j a va 2s.c om*/ } }); JPanel panel = new JPanel(new BorderLayout()); panel.setBorder(new EmptyBorder(5, 5, 5, 5)); frame.setContentPane(panel); final DateTimeEditor field = new DateTimeEditor(DateTimeEditor.DATETIME, DateFormat.FULL); panel.add(field, "North"); JPanel buttonBox = new JPanel(new GridLayout(2, 2)); JButton showDateButton = new JButton("Show Date"); buttonBox.add(showDateButton); final JComboBox timeDateChoice = new JComboBox(); timeDateChoice.addItem("Time"); timeDateChoice.addItem("Date"); timeDateChoice.addItem("Date/Time"); timeDateChoice.setSelectedIndex(2); timeDateChoice.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { field.setTimeOrDateType(timeDateChoice.getSelectedIndex()); } }); buttonBox.add(timeDateChoice); JButton toggleButton = new JButton("Toggle Enable"); buttonBox.add(toggleButton); showDateButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { System.out.println(field.getDate()); } }); toggleButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { field.setEnabled(!field.isEnabled()); } }); panel.add(buttonBox, "South"); final JComboBox lengthStyleChoice = new JComboBox(); lengthStyleChoice.addItem("Full"); lengthStyleChoice.addItem("Long"); lengthStyleChoice.addItem("Medium"); lengthStyleChoice.addItem("Short"); lengthStyleChoice.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { field.setLengthStyle(lengthStyleChoice.getSelectedIndex()); } }); buttonBox.add(lengthStyleChoice); frame.pack(); Dimension dim = frame.getToolkit().getScreenSize(); frame.setLocation(dim.width / 2 - frame.getWidth() / 2, dim.height / 2 - frame.getHeight() / 2); frame.show(); }
From source file:Main.java
/** * select index silently//from w ww .ja v a2 s .c o m * @param aComboBox combo box * @param anIndex index * @param aListener listener to be removed when selecting index */ public final static void SelectIndexSilently(JComboBox aComboBox, int anIndex, ItemListener aListener) { aComboBox.removeItemListener(aListener); aComboBox.setSelectedIndex(anIndex); aComboBox.addItemListener(aListener); }
From source file:Main.java
/** * Creates a channel selector combobox./* w w w . j a v a 2 s. c om*/ * * @param aChannelCount * the number of channels to include in the combobox options; * @param aDefaultSelectedindex * the default selected index; * @param aAddUnusedOption * <code>true</code> to add "unused" as first option, * <code>false</code> to omit this option. * @return a combobox with channel selector options. */ private static JComboBox internalCreateChannelSelector(final int aChannelCount, final int aDefaultSelectedindex, final boolean aAddUnusedOption) { int modelSize = Math.max(0, Math.min(32, aChannelCount)); if (aAddUnusedOption) { modelSize++; } final String dataChannels[] = new String[modelSize]; int i = 0; if (aAddUnusedOption) { dataChannels[i++] = "Unused"; } for (; i < modelSize; i++) { final int index = aAddUnusedOption ? i - 1 : i; dataChannels[i] = String.format("Channel %d", Integer.valueOf(index)); } int selectedIndex = aDefaultSelectedindex < 0 ? 0 : aDefaultSelectedindex % modelSize; final JComboBox result = new JComboBox(dataChannels); result.setSelectedIndex(selectedIndex); return result; }
From source file:de.mpg.mpi_inf.bioinf.netanalyzer.ui.IntHistogramVisualizer.java
/** * Creates the combo box (drop-down list) to choose between visualization of the histogram as bar chart or * as a scatter plot.// ww w . j a v a 2 s.c om * * @param aContainer Container control, to which the combo box is to be added. * @param aSelectedIndex Which choice is to be initially selected. This parameter must have one of the * values <code>{1; 2}</code>. * @return The newly created combo box control. */ private static JComboBox addChoice(Box aContainer, int aSelectedIndex) { final String[] choices = new String[] { Messages.DI_SHOWHIST, Messages.DI_SHOWSCAT }; JComboBox choiceCombo = new JComboBox(choices); choiceCombo.setSelectedIndex(aSelectedIndex); choiceCombo.setEditable(false); JPanel choicePanel = new JPanel(); choicePanel.add(choiceCombo); aContainer.add(choicePanel); return choiceCombo; }
From source file:Main.java
public Main() { String[] comboTypes = { "Numbers", "Alphabets", "Symbols" }; JComboBox<String> comboTypesList = new JComboBox<>(comboTypes); comboTypesList.setSelectedIndex(2); comboTypesList.addActionListener(new ActionListener() { @Override//from w w w .j ava2 s . c o m public void actionPerformed(ActionEvent e) { JComboBox jcmbType = (JComboBox) e.getSource(); String cmbType = (String) jcmbType.getSelectedItem(); System.out.println(cmbType); } }); setLayout(new BorderLayout()); add(comboTypesList, BorderLayout.NORTH); }
From source file:Main.java
public Main() { JComboBox<String> cb = new JComboBox<>(new String[] { "a", "m", "b", "c", "v" }); cb.setSelectedIndex(-1); JButton button = new JButton("Print Selection"); button.addActionListener(e -> {// w w w. j a va2 s. c o m if (cb.getSelectedIndex() != -1) System.out.println(cb.getSelectedItem()); else System.out.println("Not selected"); }); add(cb); add(button); }
From source file:ComboBoxDemo.java
public ComboBoxDemo() { super(new BorderLayout()); String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" }; //Create the combo box, select the item at index 4. //Indices start at 0, so 4 specifies the pig. JComboBox petList = new JComboBox(petStrings); petList.setSelectedIndex(4); petList.addActionListener(this); //Set up the picture. picture = new JLabel(); picture.setFont(picture.getFont().deriveFont(Font.ITALIC)); picture.setHorizontalAlignment(JLabel.CENTER); updateLabel(petStrings[petList.getSelectedIndex()]); picture.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0)); //The preferred size is hard-coded to be the width of the //widest image and the height of the tallest image + the border. //A real program would compute this. picture.setPreferredSize(new Dimension(177, 122 + 10)); //Lay out the demo. add(petList, BorderLayout.PAGE_START); add(picture, BorderLayout.PAGE_END); setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); }
From source file:events.ListSelectionDemo.java
public ListSelectionDemo() { super(new BorderLayout()); String[] listData = { "one", "two", "three", "four", "five", "six", "seven" }; String[] columnNames = { "French", "Spanish", "Italian" }; list = new JList(listData); listSelectionModel = list.getSelectionModel(); listSelectionModel.addListSelectionListener(new SharedListSelectionHandler()); JScrollPane listPane = new JScrollPane(list); JPanel controlPane = new JPanel(); String[] modes = { "SINGLE_SELECTION", "SINGLE_INTERVAL_SELECTION", "MULTIPLE_INTERVAL_SELECTION" }; final JComboBox comboBox = new JComboBox(modes); comboBox.setSelectedIndex(2); 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); }/*from w ww. j av a 2 s . c o m*/ 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)); listContainer.setBorder(BorderFactory.createTitledBorder("List")); listContainer.add(listPane); topHalf.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5)); topHalf.add(listContainer); //topHalf.add(tableContainer); topHalf.setMinimumSize(new Dimension(100, 50)); topHalf.setPreferredSize(new Dimension(100, 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, 135)); splitPane.add(bottomHalf); }
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); 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); }/* ww w . j a va 2 s .c o m*/ 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:ListSelectionDemo.java
public ListSelectionDemo() { super(new BorderLayout()); String[] listData = { "one", "two", "three", "four", "five", "six", "seven" }; String[] columnNames = { "French", "Spanish", "Italian" }; list = new JList(listData); listSelectionModel = list.getSelectionModel(); listSelectionModel.addListSelectionListener(new SharedListSelectionHandler()); JScrollPane listPane = new JScrollPane(list); JPanel controlPane = new JPanel(); String[] modes = { "SINGLE_SELECTION", "SINGLE_INTERVAL_SELECTION", "MULTIPLE_INTERVAL_SELECTION" }; final JComboBox comboBox = new JComboBox(modes); comboBox.setSelectedIndex(2); 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); }// ww w .j a v a 2 s . c o m 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)); listContainer.setBorder(BorderFactory.createTitledBorder("List")); listContainer.add(listPane); topHalf.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5)); topHalf.add(listContainer); // topHalf.add(tableContainer); topHalf.setMinimumSize(new Dimension(100, 50)); topHalf.setPreferredSize(new Dimension(100, 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, 135)); splitPane.add(bottomHalf); }