JList in java swings

JList in java swings

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();
lm.addElement("Amardeep");
lm.addElement("Deepak");
lm.addElement("Vinod");
lm.addElement("chandan");
lm.addElement("noor");
lm.addElement("vikash");
lm.addElement("suman");
lm.addElement("Neelam");
lm.addElement("soroj");

//Create the list and put it in a scroll pane.
list = new JList(lm);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setSelectedIndex(0);
list.addListSelectionListener(this);
list.setVisibleRowCount(5);
JScrollPane listScrollPane = new JScrollPane(list);

JButton sbutton = new JButton(save);
SaveListener slist = new SaveListener(sbutton);
sbutton.setActionCommand(save);
sbutton.addActionListener(slist);
sbutton.setEnabled(false);

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

textfield = new JTextField(10);
textfield.addActionListener(slist);
textfield.getDocument().addDocumentListener(slist);
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(jbutton);
buttonPane.add(Box.createHorizontalStrut(5));
buttonPane.add(new JSeparator(SwingConstants.VERTICAL));
buttonPane.add(Box.createHorizontalStrut(5));
buttonPane.add(textfield);
buttonPane.add(sbutton);
buttonPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
add(listScrollPane, BorderLayout.CENTER);
add(buttonPane, BorderLayout.PAGE_END);
}
class DeleteListener implements ActionListener {
public void actionPerformed(ActionEvent e){
int index = list.getSelectedIndex();
lm.remove(index);
int size = lm.getSize();
if (size == 0) { //Nobody's left, disable firing.
jbutton.setEnabled(false);

} else { //Select an index.
if (index == lm.getSize()) {
//removed item in last position
index--;
}
list.setSelectedIndex(index);
list.ensureIndexIsVisible(index);
}
}
}

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)) {
Toolkit.getDefaultToolkit().beep();
textfield.requestFocusInWindow();
textfield.selectAll();
return;
}

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

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

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

private void enableButton() {
if (!alreadyEnabled) {
button.setEnabled(true);
}
}
private boolean handleTextField(DocumentEvent e) {
if (e.getDocument().getLength() <= 0) {
button.setEnabled(false);
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.
jbutton.setEnabled(false);

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

-------------------------------------------------------

Read for more information,

http://www.roseindia.net/java

Thank.









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
Advertisements
jList
jList  how to add checkbox for every value in jlist having values populated from ms access database using java netbeans
jList
jList  how to remove value from jlist after clicking on that value
Jlist
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
JList  pls tell me about the concept the JList in corejava? and tell me a suitable example
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
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
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
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 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 code for click the refresh button then list values will be refresh.Please send the code ........... Thanks, Valarmathi
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 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 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
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 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
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  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
SWINGS
SWINGS  WHAT ARE THE DIFFERENCES BETWEEN AWT AND SWINGS
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
swings - Java Beginners
swings   how t upload images in swings. thanks   ... extends JFrame { public static JFrame frame; public JList list; public... DefaultListModel(); list = new JList(model); list.getInputMap().put
java swings - Swing AWT
write the code for bar charts using java swings.  Hi friend, I am...java swings  I am doing a project for my company. I need a to show.... http://www.roseindia.net/java/example/java/swing/draw-simple-bar
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
java swings - Java Beginners
java swings   Hi, I already posted the question for three times.... I have two listboxes.If i select first value in the first listbox and moved to second listbox.The next time the same value will not able to move the first
java swings - Java Beginners
java swings  Hi, This is my java code.How can i select...; JList jbossactivemqlist, jbossibmmqlist; JLabel jbossactivemqlabel...(); jbossibmmqlistmodel = new DefaultListModel(); jbossibmmqlist = new JList
java swings - Java Beginners
java swings  Hi, I have two listboxes.If i select first listbox... serialVersionUID = 6877636757727044238L; JList jbossactivemqlist, jbossibmmqlist... = new JList(jbossibmmqlistmodel); jbossaddbutton = new JButton(">>
java swings - Java Beginners
java swings  hi, This is my code. I need the code for disable...; JList jbossactivemqlist, jbossibmmqlist; JLabel jbossactivemqlabel,jbossibmmqlabel...(); jbossibmmqlist = new JList(jbossibmmqlistmodel); jbossaddbutton = new JButton
java swings - Java Beginners
java swings  Hi, I have array of values in the jcombobox. I need... serialVersionUID = 6877636757727044238L; JList jbossactivemqlist... DefaultListModel(); jbossibmmqlist = new JList(jbossibmmqlistmodel
java swings - Java Beginners
java swings  Hi, For the same code .... If i select the first... = 6877636757727044238L; JList jbossactivemqlist, jbossibmmqlist; JLabel...(); jbossibmmqlistmodel = new DefaultListModel(); jbossibmmqlist = new JList(jbossibmmqlistmodel
java swings - Java Beginners
java swings  Hi, I have array of values in the jcombobox. I need... static final long serialVersionUID = 6877636757727044238L; JList...(); jbossibmmqlist = new JList(jbossibmmqlistmodel); jbossaddbutton = new
java swings - Java Beginners
java swings  hi, I need the listbox value enable and disable... serialVersionUID = 6877636757727044238L; JList jbossactivemqlist, jbossibmmqlist; JLabel...(); jbossibmmqlistmodel = new DefaultListModel(); jbossibmmqlist = new JList(jbossibmmqlistmodel
java swings - JavaMail
java swings  Hi sir,i am doing a project on swings,i don't have any...://www.roseindia.net/java/example/java/swing/GrideComponents.shtml http://www.roseindia.net/java/example/java/swing/AbsCoordinate.shtml http://www.roseindia.net
java swings - Java Beginners
java swings  Hi, I need the list box example. I have given my... box only.Please help me.This is very urgent... This is my java code... static final long serialVersionUID = 6877636757727044238L; JList
java swings - Java Beginners
java swings   Hi, I have two classes(two tabbed panes). how can i get the one class jtextfield value into another class method. Please send the code as sson as possible. Thanks, Valarmathi  Hi Friend, Try
swings - Java Beginners
for this in google, but i got very difficult programs. i am beginner in java swings . i want...); } } For more information, visit the following link: http://www.roseindia.net/java/example/java/swing/ Thanks
Jlist and JTextfield
Jlist and JTextfield  How can we filter values from jlist by adding only a single letter in jtextfield such that when letter S is pressed in jtextfield then jlist should diplay all the values starting from letter S.I am using
java swings - Java Beginners
java swings   Hi, This is my code. How can i set the jtextfield...", Font.BOLD, 12)); javapathname = new JLabel("Java Path"); javatextpath = new JTextField(33); javatextpath.setToolTipText("Java Installation Path
java swings - Java Beginners
java swings  Hi , I have integrated the code for jtextfield validation. If i run the code ,i am getting nullpointer exception.Please correct... JLabel jbossActivemqLabel, jbossIbmmqLabel,activemqtitle; JList
java swings - Java Beginners
java swings   hi, I posted the same question two times,but i didnt get the reply. Please change the jframe look and feel type of code.please send the code immediately. This is my code package com.zsl.ibm.mqtool; import

Ads