Java Video Tutorial 25

Java Swing JListHere I continue making the most comprehensive Java Video Tutorial ever. Today I’ll cover how to create list boxes using Java Swing.

More specifically I’ll cover JLists, DefaultListModel, using JScrollpane with JList and a whole bunch more.

Like always, the heavily commented code follows the video. 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 Lesson25 extends JFrame{
	JButton button1;
	String infoOnComponent = "";
	JList favoriteMovies, favoriteColors;
	DefaultListModel defListModel = new DefaultListModel();
	JScrollPane scroller;
	public static void main(String[] args){
		new Lesson25();
	public Lesson25(){
		this.setTitle("My Fifth Frame");
		JPanel thePanel = new JPanel();
		// Create a button
		button1 = new JButton("Get Answer");
		ListenForButton lForButton = new ListenForButton();
		String[] movies = {"Matrix", "Minority Report", "Big"};
		// Creating a List Box
		favoriteMovies = new JList(movies);
		// Define the height of each cell
		// Define the width of each cell
		// Define how many selections can be made
		// MULTIPLE_INTERVAL_SELECTION: Select what ever you want
		// SINGLE_SELECTION: Select only one
		// SINGLE_INTERVAL_SELECTION: Select as many as you want if in order
		// All the methods for lists
		 * getSelectedIndex(): returns the index for the first selected item
		 * getSelectedIndexes(): returns every selection in a list
		 * getSelectedValue(): returns the value of the first selected
		 * getSelectedValues(): returns an array of all values
		 * isSelectedIndex(): returns true if index is selected
		// You can't change items in a list unless you store the items
		// in a DefaultListModel
		String[] colors = {"Black", "Blue", "White", "Green", "Orange", "Gray", "Pink"};
		// How to load a String array into a DefaultListModel
		for(String color: colors){
		// Add item named Purple to index number 2
		defListModel.add(2, "Purple");
		// Create a List box filled with items in the DefaultListModel
		favoriteColors = new JList(defListModel);
		// Only display 4 items at a time
		// Create a scroll bar panel to hold the list box
		scroller = new JScrollPane(favoriteColors, 
		// Define the height of each cell
		// Define the width of each cell
		// You add the scroll bar container, not the list
		private class ListenForButton implements ActionListener{
			public void actionPerformed(ActionEvent e){
				if(e.getSource() == button1){
					// contains returns true if the item is in the list
					if(defListModel.contains("Black")) infoOnComponent += "Black is here\n";
					// Check if the list isn't empty
					if(!defListModel.isEmpty()) infoOnComponent += "Isn't Empty\n";
					// return the number of items in the DefaultListModel
					infoOnComponent += "Elements in the list " + defListModel.size() + "\n";
					// return the first element in the list
					infoOnComponent += "Last Element " + defListModel.firstElement() + "\n";
					// return the last element in the list
					infoOnComponent += "Last Element " + defListModel.lastElement() + "\n";
					// return the last element in the list
					infoOnComponent += "Element in index 1 " + defListModel.get(1) + "\n";
					// Remove the item in index 0
					// Remove the item named Big
					// Create an array filled with the list items
					Object[] arrayOfList = defListModel.toArray();
					// Iterate through the array
					for(Object color: arrayOfList){
						infoOnComponent += color + "\n";
					JOptionPane.showMessageDialog(Lesson25.this, infoOnComponent, "Information", JOptionPane.INFORMATION_MESSAGE);
					infoOnComponent = "";

15 Responses to “Java Video Tutorial 25”

  1. Anonymous says:

    nice video as usual…
    can you refer me any book for swing, anyone which covers more standalone applications..?

    Thank you..

  2. smart crash course with healthy knowledge.
    these like tutorials acknowledge the beginners how to learn java.
    I think it is better and better to teach and learn Java style……

    • admin says:

      Thank you 🙂 I thought it would be nice to create a tutorial that completely covered Java. I’m just getting started and plan to cover everything including good coding practices using design patterns

  3. ben says:

    Hi, i was just wondering if you could give me some advice on which languages to learn and in what order? I already know html and css obviously…and am now attempting to master javascript 😛
    next i was thinking the logical step would be Jquery and ajax…then maybe php and finally java?

    • admin says:

      Hi, That is the exact order I would learn them. Just make sure you have a firm grasp on how to make cross browser sites using CSS. Then JavaScript, JQuery and PHP & MySQL are logical next steps.

      After that you need to decide what you want to use for application and mobile development. Java is a logical choice, but if you want to develop for Apple devices Objective C may be a better option. That is 100% up to you.

      I hope that helps

  4. Borko says:

    Hi Derek

    I am a little confused with default list model and the jlist component. They have different methods, like the component can get the selected value, but the model cant.They are made to be used both for their different methods, or the models has the same methods but i don’t see them, because you said that the component methods are old and broken down.

    And it came to my mind can you tell me what are Maps and Enumerations, and what are their uses. I think enumaration is an array of constants but i dont see the point of its use.

    P.S I dont say thank you anymore because words cant explain my gratitude 😀

    Goodbye, take care and keep on the excellent work!!!

    • admin says:

      You’re very welcome 🙂 A map is sort of a collection class that stores key value pairs of information. It is like an array, but instead of having index values it has a key which can be more descriptive. I talked about Arrays, ArrayLists, LinkedLists and Maps / HashMaps, which you can find here Java Video Tutorial.

      An enum type is a datatype whose fields have a fixed set of constants. I’ve talked about them in other tutorials, but haven’t covered them on their own.

      Thank you for bring those to my attention. I’ll cover them very soon on their own


  5. Mohamed Kamel says:

    Thanks Derek, wherever it is the best tutorial .

  6. Alexander says:

    I was just playing around with the JList and I noticed that it doesn’t have ActionListener.I can’t find a favoriteMovies.addActionListener(l);. Is it okay if I’ll use a MouseListener? I’m trying to make an event, when I’ll click an item, java will send me a message dialog with the chosen item.

Leave a Reply

Your email address will not be published.