3

I'm just new learning OOP sorry for this basic post. I don't know why it returns me a null when I'm trying to get the value of the selected item in my JComboBox.

public class AddEmployee extends javax.swing.JInternalFrame{
   public AddEmployee() 
    {
    initComponents();
    this.setSize(1100,500);
    setMonths();
    setJComboBoxProperties();
    check();
    }

    private void setMonths()
    {
       String[] monthsObj = {"January", "February", "March", "April", "May", "June", "July",
    "August", "September", "October", "November", "December"};

       DefaultComboBoxModel monthsModel = new DefaultComboBoxModel(monthsObj);

       cbMonths.setModel((ComboBoxModel)monthsModel);

    }

    private void setJComboBoxProperties()
    {
      cbMonths.setSelectedIndex(-1);
    }

    private String check()
    {
       String cb = (String)cbMonths.getSelectedItem();
       System.out.println(cb);
       return cb;
    }

}

I cast the String cb so it won't give me a null. But I'm trying to check out the selected item but it gives me null.

John Francis
  • 83
  • 1
  • 7

2 Answers2

2

Call setSelectedItem after initialize the Combobox. See the documentation.

  DefaultComboBoxModel monthsModel = new DefaultComboBoxModel(monthsObj);
  monthsModel.setSelectedItem('September');
Jens
  • 67,715
  • 15
  • 98
  • 113
  • No. I'm just trying to check which item in drop down is selected by the user. – John Francis Sep 30 '16 at 10:27
  • 2
    @JohnFrancis if you get null, no entry is selected by the user. See the docu i have provided `The combo box value or null for no selection.` – Jens Sep 30 '16 at 10:27
  • Check `getSelectedIndex()`. Should return `-1` if not selected. – alex Sep 30 '16 at 10:28
  • 1
    @dit OP Calls `getSelectedItem` not `getSelectedIndex`. read the question properbly – Jens Sep 30 '16 at 10:29
  • I did not say he should use `getSelectedIndex()`. I mean just to test... – alex Sep 30 '16 at 10:31
  • @dit And what shoulod the test Show? The documentation is very clear – Jens Sep 30 '16 at 10:34
  • I don't need to justify myself. By I just wanted to give some other option to debug the issue. But sorry for my suggestion, Your Majesty. – alex Sep 30 '16 at 10:37
  • @Jens I see why it's giving me a `null` value. Because I created a method where I set the `cbMonths` as `selectedIndex(-1)` How can avoid this null withouth removing `selectedIndex(-1)`? :D – John Francis Sep 30 '16 at 10:37
  • @JohnFrancis You should Show more all relevant code. You can not. If you use selectedIndex(-1) you unselect all values and then getSelectedItem() Returns null. – Jens Sep 30 '16 at 10:39
  • @Jens I updated my post. I tried to remove `selectedIndex(-1)` and check which combobox is selected. But it always print me first the first index of my element. How can I test which element is selected by user? – John Francis Sep 30 '16 at 10:43
  • @JohnFrancis After a user select a value you have to use getSelectedItem() or getSelectedIndex(). But before a user must select an item – Jens Sep 30 '16 at 10:45
  • @JohnFrancis BTW: You should not Change your question because the answers make no scence after you shange your question. Add such Details marked as `UPDATE` – Jens Sep 30 '16 at 10:47
  • @Jens Yes I checked that using `String cb = (String)cbMonths.getSelectedItem();` But when the program starts it always gives me the first element. When I select a item from drop down. It doesn't print anything? :D – John Francis Sep 30 '16 at 10:49
  • @JohnFrancis The method runs only one. If you Need it after a Change, you have to add a listener – Jens Sep 30 '16 at 10:53
2

You're calling cbMonths.setSelectedIndex(-1);. This sets no item (null) as the selected item, as per the documentation.

Until the user changes the selection, getSelectedItem() will always return null. This is the correct, documented behaviour.

daiscog
  • 11,441
  • 6
  • 50
  • 62
  • I tried to removed `setSelectedIndex(-1);` and the first element is January right? It always prints the first element but when I select other element it prints nothing. How can I solve this? – John Francis Sep 30 '16 at 10:52
  • You need to add a listener to react to the ActionEvent triggered when the user changes the selection. See this question: http://stackoverflow.com/questions/58939/jcombobox-selection-change-listener – daiscog Sep 30 '16 at 10:55
  • At first I thought I'm not gonna use a Listener. Is this actually the behavior of JComboBox? Just asking :D – John Francis Sep 30 '16 at 10:58
  • Yes. In your code, you're printing the selected item _before_ anyone has changed it. If you want to print the selected item every time it gets changed, then you need to react to the user changing the value. That's what listeners are for; they allow you to react to Events. Of course, there could be other triggers for printing the value, like clicking something else, or a periodical timer, or anything you want, really. – daiscog Sep 30 '16 at 12:24