In 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.setSize(400,400); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 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"); thePanel.add(favoriteShows); // Create a button button1 = new JButton("Get Answer"); ListenForButton lForButton = new ListenForButton(); button1.addActionListener(lForButton); thePanel.add(button1); this.add(thePanel); this.setVisible(true); // Add an item to a combo box at index 1 favoriteShows.insertItemAt("Dexter", 1); // Only show 3 items at a time favoriteShows.setMaximumRowCount(3); // Remove the item named Dexter //favoriteShows.removeItem("Dexter"); // Remove the item at index 1 //favoriteShows.removeItemAt(1); // Remove all items // favoriteShows.removeAllItems(); } private class ListenForButton implements ActionListener{ public void actionPerformed(ActionEvent e){ if(e.getSource() == button1){ favoriteShows.setEditable(true); // 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 = ""; } } } }
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.
You’re very welcome 🙂 You want to do something like this
myComboBox.addActionListener (new ActionListener () {
public void actionPerformed(ActionEvent e) {
doSomething();
}
});
Derek,
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.
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.
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].
That is really odd? I think the file may have been messed up on YouTubes servers. I’ll see if there is any way to fix that. I do have the code below to help either way. Sorry about that.
I am facing the same issue. It doesn’t play beyond 2:07 mins. 🙁
Thanks Derek for the great videos. You have made it easy to learn Java.
Sorry, but YouTube won’t let me update it 🙁
Could you perhaps put a link to the correct website in the comments section of YouTube? This is a topic we’re covering in class as well. Love your videos by the way. They’re a life saver.
Thank you 🙂 I fixed the link
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
Hi, Sorry, but it seems like YouTube messed up that video and won’t let me replace it. The code and comments will have to fill in. Sorry
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 ?
Hi Ben, Take a look here https://docs.oracle.com/javase/7/docs/api/java/util/Map.html