JComboBox (Item Event) and JList (List Selection Event) in Java with Examples
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:
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);
}
}
}
JList Introduction:
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);
}
}
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.
|