Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JComboBox showing blank items in Java

I have 2x JComboBox: showingsCb and timesCb

showingsCb contains a list of cinema showings that are pulled from my database.

timesCb contains a list of cinema showing times based on the selection from showingsCb which is also pulled from my database.

Now I have managed to populate them both with correct data and the logic works fine, but here is my problem:

I make a selection in showingsCb which then enables timesCb and upon clicking on timesCb, shows me a list of all the showing times based on the showing selected in showingsCb. This is working fine.

I then select a different showing in showingsCb and upon clicking on timesCb, the list drops down and shows a blank list. The show times exist in this blank list but I can't see them (I know they're there because I can click on the blank spaces where they're supposed to be and it fills the JComboBox with my selection).

I am using a DefaultComboBoxModel on the JComboBox, which is populated by an ArrayList or Vector (which retrieves results from my database using my own method). I am using an ActionListener on showingsCb to listen for the selection so it knows what showing times to return.

Here is my code:

        ArrayList<String> showingNames = Showing.getShowingList();
        showingNames.add(0, "--Select Showing--");
        DefaultComboBoxModel showingNameModel = new DefaultComboBoxModel(showingNames.toArray());
        showingsCb = new JComboBox(showingNameModel);

        Vector timesList = new Vector();
        timesList.add("--Select Time--");
        DefaultComboBoxModel timesModel = new DefaultComboBoxModel(timesList);
        timesCb = new JComboBox(timesModel);
        timesCb.setEnabled(false);

        showingsCb.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {

                timesList.clear();
                timesList.add("--Select Time--");
                timesCb.setSelectedIndex(0);
                if (showingsCb.getSelectedIndex() == 0) {
                    timesCb.setEnabled(false);
                } else {
                    timesCb.setEnabled(true);
                    ArrayList<String> times = Showing.getShowingTimes(Showing.getShowingIDByName(showingsCb.getSelectedItem().toString()));  //gather showing times based on showingID
                    Iterator itt = times.iterator();
                    while (itt.hasNext()) {
                        timesList.add(itt.next());
                    }
                }
            }

        });

And here is a screenshot of how the results look when they're showing as blank:

enter image description here

If I were to click anywhere on that blank dropdown list then the selection I've chosen will appear as my selected item.

I'm also having trouble obsoleting the Vector from my code on the timesCb JComboBox

like image 415
blueprintchris Avatar asked Mar 14 '23 16:03

blueprintchris


2 Answers

Ok. Your defaultcomboboxmodel is initialized with timesList. In action listener, you cleared that, you need to reinitialize your model with the new timesList. So timesCb.setModel( new DefaultComboBoxModel( timesList ) ) after while loop will solve your problem.

or actually a better approach. You shouldn't be constructing a new model class every time, instead in the action listener inside while loop do this:

timesModel.addElement( itt.next() );
like image 52
SomeDude Avatar answered Mar 28 '23 06:03

SomeDude


Here's a way you could add elements dynamically to your 2nd JComboBox and from the docs there's a JComboBox#addItem() method where you can easily add elements to your JComboBox. But I find @svasa a better approach than mine while adding elements to you DefaultComboBoxModel with DefaultComboBoxModel#addElement().

In this case you were never updating your 2nd combo box.

Here's the code which produces the following output, and as you can see, it starts disabled, it get enabled when you select a different index from 0 on the 1st combo and disables it again when you select 1st index (0 I mean) on the 1st combo.

enter image description here

enter image description here

enter image description here

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class ComboBoxExample {
    JFrame frame;
    java.util.List <String> movies, times;
    JComboBox moviesCombo, timesCombo;
    JPanel pane;
    public ComboBoxExample () {
        frame = new JFrame("Combo Box Example");
        movies = new ArrayList <String>();
        times = new ArrayList <String>();
        pane = new JPanel(new FlowLayout());

        //This simulates the data comes from a DB
        movies.add("Select a movie");
        for (int i = 0; i < 5; i++) {
            movies.add("Movie " + (i + 1));
        }

        DefaultComboBoxModel model1 = new DefaultComboBoxModel(movies.toArray());
        moviesCombo = new JComboBox(model1);

        times.add("Select time");
        DefaultComboBoxModel timesModel = new DefaultComboBoxModel(times.toArray());
        timesCombo = new JComboBox(timesModel);
        timesCombo.setEnabled(false);

        moviesCombo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                System.out.println(moviesCombo.getSelectedIndex());
                if (moviesCombo.getSelectedIndex() == 0) {
                    timesCombo.setEnabled(false);
                } else {
                    timesCombo.removeAllItems();
                    timesCombo.addItem("Select time");
                    for (int i = 0; i < 5; i++) {
                        timesCombo.addItem("" + (i + 3));
                    }
                    timesCombo.setEnabled(true);
                }
            }
        });

        pane.add(moviesCombo);
        pane.add(timesCombo);

        frame.add(pane);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    public static void main (String args[]) {
        new ComboBoxExample();
    }
}

Also, as you can see I don't need a DB to got it to work too, this is what we call a Minimal, Complete, and Verifiable example or Runnable Example, this is something we can copy-paste and for your future questions you might want to take a look at this link, read it and post your future questions with something like this code so we can copy-paste it and be able to help you in a better and faster way.

like image 25
Frakcool Avatar answered Mar 28 '23 06:03

Frakcool