JList in java swings



Share on Google+Share on Google+

JList in java swings
3 Answer(s)      9 years ago
Posted in : Java Beginners
View Answers

June 16, 2008 at 6:34 PM

hi usha

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

public class JListExample extends JPanel implements ListSelectionListener {
private JList list;
private DefaultListModel lm;

private static final String save = "Save";
private static final String del = "Delete";
private JButton jbutton;
private JTextField textfield;

public JListExample() {
super(new BorderLayout());

lm = new DefaultListModel();

//Create the list and put it in a scroll pane.
list = new JList(lm);
JScrollPane listScrollPane = new JScrollPane(list);

JButton sbutton = new JButton(save);
SaveListener slist = new SaveListener(sbutton);

jbutton = new JButton(del);
jbutton.addActionListener(new DeleteListener());

textfield = new JTextField(10);
String name = lm.getElementAt(list.getSelectedIndex()).toString();
//Create a panel that uses BoxLayout.
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
buttonPane.add(new JSeparator(SwingConstants.VERTICAL));
add(listScrollPane, BorderLayout.CENTER);
add(buttonPane, BorderLayout.PAGE_END);
class DeleteListener implements ActionListener {
public void actionPerformed(ActionEvent e){
int index = list.getSelectedIndex();
int size = lm.getSize();
if (size == 0) { //Nobody's left, disable firing.

} else { //Select an index.
if (index == lm.getSize()) {
//removed item in last position

June 16, 2008 at 6:35 PM

//This listener is shared by the text field and the hire button.
class SaveListener implements ActionListener, DocumentListener {
private boolean alreadyEnabled = false;
private JButton button;
public SaveListener(JButton button){
this.button = button;
//Required by ActionListener.
public void actionPerformed(ActionEvent e) {
String name = textfield.getText();

//User didn't type in a unique name...
if (name.equals("") || alreadyInList(name)) {

int index = list.getSelectedIndex(); //get selected index
if (index == -1) { //no selection, so insert at beginning
index = 0;
} else { //add after the selected item

lm.insertElementAt(textfield.getText(), index);
//Reset the text field.
//Select the new item and make it visible.
protected boolean alreadyInList(String name) {
return lm.contains(name);
//Required by DocumentListener.
public void insertUpdate(DocumentEvent e) {
//Required by DocumentListener.
public void removeUpdate(DocumentEvent e){

//Required by DocumentListener.
public void changedUpdate(DocumentEvent e) {
if (!handleTextField(e)) {

private void enableButton() {
if (!alreadyEnabled) {
private boolean handleTextField(DocumentEvent e) {
if (e.getDocument().getLength() <= 0) {
alreadyEnabled = false;
return true;
return false;

June 16, 2008 at 6:36 PM

//This method is required by ListSelectionListener.
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting() == false) {

if (list.getSelectedIndex() == -1) {
//No selection, disable fire button.

} else {
//Selection, enable the fire button.
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("JList frame example");
JComponent newContentPane = new JListExample();
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run() {


Read for more information,




Related Tutorials/Questions & Answers:
JList in java swings - Java Beginners
JList in java swings  HI I am trying to create a JList of buttons... very urgent i tried out the following... JList controlButtons = new JList... { private JList list; private DefaultListModel lm; private static
jlist in swings
jlist in swings  how to populate jlist with all the data retrieved from the ms access database?plzz help
jList  how to add checkbox for every value in jlist having values populated from ms access database using java netbeans
jlist - Java Beginners
jlist  How to clear the display data from the jlist. Please help me... extends JPanel implements ListSelectionListener { JList list; DefaultListModel... = new JList(listModel); list.setSelectionMode(ListSelectionModel.SINGLE
jList  how to remove value from jlist after clicking on that value
JcomboBox to a Jlist  How to transfer a data from a JcomboBox to a Jlist ? Each time I select 1 item from a JComboBox, it will display in a JList
JList  pls tell me about the concept the JList in corejava? and tell me a suitable example
JList box java
JList box java We are going to describe JList box java. First of all we have created JList component of Swing. List is a component of GUI() Graphical User... you can select more than one item at once from the list. Example of JList
jList  how to get the jlist values into jtextfield after clicking... value from the jlist to textfield. import java.awt.*; import javax.swing.*; import... JPanel implements ListSelectionListener { JList list; DefaultListModel listModel
Java Swings
Java Swings  I am doing one project on java Swings, in this i have created one jframe where i defined some JButtons and Jcombobox's, here i need to insert one JTable with headers(IN CURRENT JFrame only).pls help me urgently
java swings - Java Beginners
java swings   Do you want to add textfields on the JPanel
java swings - Java Beginners
java swings   Hi , I have two listboxes.I want to move one listbox... correct the value and send it to me. This is my java code package...; JList jbossactivemqlist,jbossibmmqlist; JLabel jbosslabel; JButton
java swings - Java Beginners
java swings   Hi, I need the code for click the refresh button then list values will be refresh.Please send the code ........... Thanks, Valarmathi
java swings
java swings  Hi, I need a sample code for once click the exit tab the frame window will be closed. Instead of button close i need tab close option.Please send the sample code immediately.......?Its very urgent........ Thanks
java swings
java swings  hi... I am having multiple swing forms.. when i click on a button another form is going to opening but it is opening as different form... Now i want it to be open dynamically instead separately.... Is there any
java swings
java swings  how can we use the shared locks of dbms in java? I am working on a mini project which is based on the eamcet councilling .i am struck with a problem where if a student select a college and a branch and sees
java swings
java swings  Hi, I have one class file using three panel methods,the three methods is used for three tabs.then how can i set the background image.Please send the sample code for me. I already posted two questions,but i didnt get
java swings
java swings  Hi, I need the sample code for how to set the background image using jframe and also set the jtext field and jlable boxes under the bachground image. Please send the code immediately,its very urgent.Please send me
java swings - Java Beginners
java swings  Hi, I need the listbox example. I have two listboxes...*; public class CreateJList { JList list1; JList list2; DefaultListModel...(); listModel2 = new DefaultListModel(); list2 = new JList(listModel2
java swings - Java Beginners
java swings  Hi, I have two list boxes.From the first listbox i...*; public class CreateJList{ JList list1; JList list2; DefaultListModel...(); list2 = new JList(listModel2); String[] listval={"one","two","three","four
java swings - Java Beginners
java swings  hi, I already send the question 4 times. I have two..., Valarmathi  class Listbox implements ListSelectionListener { private JList list1; private JList list2; private Vector v; public void valueChanged
java swings - Java Beginners
java swings  Hi, I have two list boxes.From the first listbox i... class CreateJList{ JList list1; JList list2; DefaultListModel...(); list2 = new JList(listModel2); listModel1.addElement("ZEN
java code using swings
java code using swings  code that should be able to enter data of student details using all swings into the access database using jdbc connectivity
java swings - Java Beginners
java swings  Hi, I already send the question for two times. I have... { JList list1; JList list2; DefaultListModel listModel1, listModel2... DefaultListModel(); list2 = new JList(listModel2); String[] listval={"one
java swings - Java Beginners
java swings   Hi, I need the code for joptionpane with jcombobox. my requirement is click on add button,one joptionpane will come.from the option pane i need to select the combobox values. Please send the sample code
java swings - Java Beginners
java swings  Hi, I have two list boxes.From the first listbox i... send the question so many times. Please send the answer. This is my java... = 6877636757727044238L; JList jbossactivemqlist,jbossibmmqlist; JLabel
Create a JList Component in Java
Create a JList Component in Java       In this section, you will learn how to create a JList component of swing. JList is a component of GUI. It provides the multiple items
Java-swings - Java Beginners
Java-swings  How to move JLabels in a Frame  Hi friend...(Locale.US); JList list = new JList(symbols.getMonths...); } } ------------------------------------------------- Visit for more information: http://www.roseindia.net/java/example/java
java swings - Java Beginners
java swings  Hi, I have two list boxes.From the first listbox i.... This is my code...... THis is my java code... package com.zsl.swing.mq...{ /** * */ private static final long serialVersionUID = 6877636757727044238L; JList
about swings - Java Beginners
about swings   Dear sir,Good evening, i am doing mca sir,i am doing the project in swings,so plz provide the material about swings sir...: http://www.roseindia.net/java/example/java/swing/ Hope
java swings - Java Beginners
java swings  Hi, I have two splitpanes,how to set the next button in the right side splitpane. private JList list; public JPanel splitpane...(); list = new JList(texts); list.addListSelectionListener