Monday, 20 April 2015

Filled Under:

Selection Of Items In Jlist With Selection Model In Java

Jlist in java have different selection models it's depend upon the user which selection model is to used by default multiple selection mode is selected means user can select multiple items at a time jlist  have a option of three selection mode any one mode can be selected at a time.following are the descriptions of selection model of jlist.
SELECTION MODE
DESCRIPTION
SINGLE_SELECTION
User have a choice to select only one item at a time. When the user selects an item, any previously selected item is deselected first.
SINGLE_INTERVAL_SELECTION
Multiple items can be selected by a user. When the user begins a new selection range previously selected items are deselected.In this mode user can select multiple items but he have to select it in a interval.
MULTIPLE_INTERVAL_SELECTION
In this selection user can select multiple selection at a time.



JLIST WITH SINGLE SELECTION MODEL:

import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
class myframe extends JFrame implements ListSelectionListener
{
JList l;
String[] item = {"Item 1A","Item 1B","Item 1C","Item 1D"};
JLabel j1;
myframe()
{
setTitle("Interval Selection In List");
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j1 = new JLabel("Select item from list");
l = new JList(item);
l.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
l.setPreferredSize(new Dimension(200,150));
add(l);
l.addListSelectionListener(this);
add(j1);
setSize(700,500);
setVisible(true);

}


public void valueChanged(ListSelectionEvent eve)
{
int returns = l.getSelectedIndex();
if(returns != -1)
{
j1.setText(item[returns]);
}
}

}
public class workinglist {

public static void main(String[] args) {

myframe fr = new myframe();

}
}

OUTPUT:




SELECTION MODELS OF JLIST :

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class myframe extends JFrame {

myframe()
{
setTitle("Interval Selection In List");
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] names = {"Item 1A","Item 1B","Item 1C","Item 1D"};
JList l1 = new JList(names);
l1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
l1.setPreferredSize(new Dimension(200,150));
add(new JLabel("Single Selection Example"));
add(l1);
String[] names1 = {"Item 2A","Item 2B","Item 2C","Item 2D"};
JList l2 = new JList(names1);
l2.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
l2.setPreferredSize(new Dimension(200,150));
add(new JLabel("Singal Interval Selection Example"));
add(l2);
String[] names3 = {"Item 3A","Item 3B","Item 3C","Item 3D"};
JList l3 = new JList(names3);
l3.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
l3.setPreferredSize(new Dimension(200,150));
add(new JLabel("Multiple Interval Selection Example"));
add(l3);
setSize(700,500);
setVisible(true);
}
}

public class list {

public static void main(String[] args) {

myframe fr = new myframe();
}
}

OUTPUT:


0 comments:

Post a Comment