JComboBox and JList in Java with Examples

JComboBox (Item Event) and JList (List Selection Event) in Java with Examples

logo

Table of Contents

a) Introduction to JComboBox
b) Ways to add items in combo box
c) Example of JComboBox with Source Code and Output
d) Introduction to JList
e) Constructors of JList
f) Example with Source Code and Ouput
g) Difference Between JComboBox and JList

*********************

JComboBox Introduction:

JComboBox is the drop down menu in java. We can select only one item of the combo box. JComboBox generates both ActionEvent and ItemEvent when item is selected in combo box. ItemEvent is generated when a new item is selected whereas ActionEvent is generated when any item is selected. By default scroll bar appears automatically when there are more than eight items in combo box. But also we can specify the numbers of visible items using setMaxiumRowCount(int ).

There are some methods which is used in JComboBox, they are;

getSelectedItem() – It returns the item selected.
getSelectedIndex() – It returns the index of the item selected

Items in combo box can be added in two ways;

a) Using parameterized constructor

for e.g.

String items[]={“a”,”b”,”c”,”d”};
JComboBOx cb=new JComboBox(items);

b)Using addItem() method

for e.g.

JComboBox cb=new JComboBox();
cb.addItem(“a”);
cb.addItem(“b”);

Let us see the example of JComboBox;

Source Code:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author AnkitPC
 */
/*
Creating a GUI with a combo box and a text field when item is combobox is selected , it should
be displayed in the text field.
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JComboBoxDemo {
    JFrame f;
    JComboBox b;
    JTextField t;
 
    JComboBoxDemo(){
        f=new JFrame("Drop Down Menu");
        f.setSize(400,300);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(new FlowLayout());
        String items[]={"Jhapa","Morang","Kathmandu","Illam","Taplejung","Sunsari"};
       
        b=new JComboBox(items);
        b.setMaximumRowCount(5);
        t=new JTextField(20);
       
       
        f.add(b);
        b.addItemListener(new ItemEventDemo());
       
        f.add(t);
        
        f.setVisible(true);
    }
    public static void main(String[] args) {
        new JComboBoxDemo();
    }
   
    class ItemEventDemo implements ItemListener{
        public void itemStateChanged(ItemEvent e){
            String s=b.getSelectedItem().toString();
          //  t.setText(s);
            int a=b.getSelectedIndex();
            t.setText(s+" "+a);
       
    }
    }
   
}

Output:


jcombobox

***************************

JList Introduction:

JList creates a list of items in which one or more than item can be selected. JList allows you to do multiple selections. JList generates the ListSelectionEvent when the item is selected. There is no default scroll bar in JList. We should use the scroll bar manually. But we can specify the number of the visible items in the JList using setVisibleRowCount(int).

JList have two constructor, they are;

a) JList(): Used to create an empty list. Items can be added to the list by using              void setListData(Object []items).

b) JList(Object []items): It is used to create a list with the specified number of         items. void setSelectionmode(int mode) allows you to specify whether the list is           single selection or multiple selection.

Mode can be;

a) ListSelectionModel.SINGLE_SELECTION
b) ListSelectionModel.SINGLE_INTERVAL_SELECTION
c) ListSelectionModel.MULTIPLE_INTERVAL_SELECTION (default)

Let us see the example using List;
Source Code:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author AnkitPC
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class JListDemo implements ListSelectionListener {
    JFrame f;
    JTextField t;
    JList jl;
    JScrollPane jp; // for scrollbar
    JListDemo(){
        f=new JFrame("Welcome");
        f.setSize(400,200);
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        f.setLayout(new FlowLayout());
        String items[]={"Jhapa","Morang","Sunsari","Bagmati"};
       
        t=new JTextField(20);
        jl=new JList(items);
        jp=new JScrollPane();
        
        jl.setVisibleRowCount(5); // shows 5 lists values for scroll bar
       
        jl.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); // for model
       
        jl.addListSelectionListener(this);
       
        f.add(jp);       
        f.add(t);
        f.add(jl);
       
       
        f.setVisible(true);
    }
   
   
   
    
    public static void main(String[] args) {
        new JListDemo();
    }
   
    public void valueChanged(ListSelectionEvent e){
       
        /*
        for single selection
        String item=jl.getSelectedValue().toString();
        t.setText(item);
        */
        // for multiple items
        String item=jl.getSelectedValuesList().toString();
        t.setText(item);
    }
}

Output:

jlist


In summary here are some differences between the JComboBox and JList;

JComboBox
JList
It allows only single selection.
It allows multiple selection.
Scroll bar is automatically added when items are more than 8 in combo box.
Scroll bar should me manually added to the list.
setMaximumRowCount(int) method is used to visible the number of items in combo box.
setVisibleRowCount(int) method is used to visible the number of items in list,
Item Event and Action Event both are generated.
ListSelectionEvent is generated,
Event is in java.awt.event package.
Event is in javax.swing,event package.

******************

Post a Comment

0 Comments