Java Video Tutorial 24

Java Video Tutorial 24In this video tutorial, I show you how to create combo boxes in Java Swing. This tutorial is also a review of everything I’ve previously gone over.

I review how to create the frame and panel. How to add buttons with event listeners. I also review JOptionPane. I’m slowing down a bit so that I’m sure you understand everything that proceeded this tutorial.

All of the code follows the video below. Leave any questions below.

If you like videos like this share it

Code from the Video

import javax.swing.*;

import java.awt.event.*;

public class Lesson24 extends JFrame{
	JComboBox favoriteShows;
	JButton button1;
	String infoOnComponent = "";
	public static void main(String[] args){
		new Lesson24();
	public Lesson24(){
		this.setTitle("My Fourth Frame");
		JPanel thePanel = new JPanel();
		// Create an array that will be added to the combo box
		String[] shows = {"Breaking Bad", "Life on Mars", "Doctor Who"};
		// Create a combo box and add the array of shows
		favoriteShows = new JComboBox(shows);
		// Add an item to the combo box
		favoriteShows.addItem("Pushing Daisies");
		// Create a button
		button1 = new JButton("Get Answer");
		ListenForButton lForButton = new ListenForButton();
		// Add an item to a combo box at index 1
		favoriteShows.insertItemAt("Dexter", 1);
		// Only show 3 items at a time
		// Remove the item named Dexter
		// Remove the item at index 1
		// Remove all items
		// favoriteShows.removeAllItems();
		private class ListenForButton implements ActionListener{
			public void actionPerformed(ActionEvent e){
				if(e.getSource() == button1){
					// Get item at index 0
					infoOnComponent = "Item at 0: " + favoriteShows.getItemAt(0) + "\n";
					// Get the number of items in the combo box
					infoOnComponent += "Num of Shows: " + favoriteShows.getItemCount() + "\n";
					// Get the index for the selected item
					infoOnComponent += "Selected ID: " + favoriteShows.getSelectedIndex() + "\n";
					// Get the value for the selected item
					infoOnComponent += "Selected Show: " + favoriteShows.getSelectedItem() + "\n";
					// Find out if the values in the combo box are editable
					infoOnComponent += "Combo Box Editable: " + favoriteShows.isEditable() + "\n";
					JOptionPane.showMessageDialog(Lesson24.this, infoOnComponent, "Information", JOptionPane.INFORMATION_MESSAGE);
					infoOnComponent = "";

14 Responses to “Java Video Tutorial 24”

  1. AAMIR says:

    hi derek thanks a lot for the videos, i have a question, how can we add listeneres on combobox, so that when a particular item is selected on the combo box we wanna do something.

    • Derek Banas says:

      You’re very welcome 🙂 You want to do something like this

      myComboBox.addActionListener (new ActionListener () {
      public void actionPerformed(ActionEvent e) {

  2. Paul says:


    If they gave away Nobel Prizes for education I think that I would nominate you. Thank you for all of you work.

    I hesitate to ask this because it is a bit of work but I navigate you site by the every lesson page then to the Java tutorials. It would be nice if the links had a description next to them on the topics covered. i.e. lesson 20 Basics of Swing class. Frames, JPannel, buttons and text fields. Or something like that. often finding myself going and looking for a earlier video and having to hunt through them till I find the one I’m looking for. Not a big deal as I really don’t want to be looking a gift horse in the mouth but it would be useful.

    • Derek Banas says:

      Thank you for the nice compliment 🙂 Yes I have been working to make it easier to find information on my site. In the last few tutorials I have actually done exactly what you said, but I need to do that with my main Java video tutorial. I’ll try to get that done this week.

  3. cpa9233 says:

    Just listened to your Java#24 YouTube session today. It appeared to cut off long before you intended(?).

    I’ve been following your sessions and feel sort of “robbed”. I’ll go forward, but would like to know if there was more to the lesson or was it a cliffhanger [sort of like “who killed JR].

  4. Filip says:

    Hi Derek.
    Great and easy videos so far. Keep it going.
    I am having the same problem reported as others, the video stops at 2:02min and it doesn’t go any further, it doesn’t load. Tried different browsers and deleting cache, history etc. but still nothing

  5. Ben says:

    Hi Derek,

    Regards and thanks for your great videos. How can one add key/value pairs to a combobox , so that the combobox shows the values but you can get the key assigned to that value ?

Leave a Reply

Your email address will not be published.