Note that there are some explanatory texts on larger screens.

plurals
  1. POThe Stubborn JComboBox
    primarykey
    data
    text
    <p>I have a JComboBox shown in the code below. When the program starts its actionPerformed event fires up immediately causing some null pointer exceptions so I want to start with none of the elements selected. However, for some reason it does not work (it always start with displaying "USD/TRY" whatever I do). Anyone has any idea ?</p> <pre><code>JComboBox comboBox = new JComboBox(new String[]{"USD/TRY", "EUR/TRY", "GBP/TRY"}); comboBox.setSelectedIndex(-1); // doesnt change anything comboBox.setSelectedIndex(2); // doesnt change anything comboBox.setSelectedItem(null); // doesnt change anything </code></pre> <p>UPDATE: Building the combo box like below doesnt change anything either</p> <pre><code>JComboBox comboBox = new JComboBox(); comboBox.addItem("USD/TRY"); comboBox.addItem("EUR/TRY"); comboBox.addItem("GBP/TRY"); </code></pre> <p>Here is the SSCCE:</p> <pre><code>public class MainFrame { private final JTextArea textArea = new JTextArea(); private IExchangeSource s; public MainFrame(final IExchangeSource s) { //build gui final JComboBox comboBox = new JComboBox(); comboBox.addItem("USD/TRY"); comboBox.addItem("EUR/TRY"); comboBox.addItem("GBP/TRY"); comboBox.setSelectedIndex(-1); // doesnt change anything //comboBox.setSelectedIndex(2); // doesnt change anything JFrame f = new JFrame("Currency Converter"); JPanel p = new JPanel(new BorderLayout()); textArea.setName("textarea"); textArea.setWrapStyleWord(true); textArea.setLineWrap(true); this.s = s; comboBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String exchange = (String) comboBox.getSelectedItem(); s.getData(exchange); } }); p.add(comboBox, BorderLayout.NORTH); p.add(textArea, BorderLayout.CENTER); f.setPreferredSize(new Dimension(300, 300)); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.pack(); f.add(p); comboBox.setSelectedIndex(0); f.setVisible(true); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload