JList

JList

View Answers

August 24, 2008 at 9:05 PM

hi,
DefaultListModel listModel =3D new DefaultListModel(); // model for =
the JList
JList list =3D new JList(listModel);

you can add element at runtime using
listModel.addElement(Object obj); or
listmodel.add(int index, Object obj)
list gets updated once you add element..

i hope this will do..


August 26, 2008 at 8:42 PM

Hi friend,


i am sending working code.

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


public class SingleList extends JPanel implements ListSelectionListener {
private JList list;
private DefaultListModel listModel;
private static final String saveString = "Save";
private static final String delString = "Delete";
private JButton deleteButton;
private JTextField ename;

public SingleList() {
super(new BorderLayout());
listModel = new DefaultListModel();
listModel.addElement("Amardeep");
listModel.addElement("Vinod");
listModel.addElement("Suman");

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

JButton hireButton = new JButton(saveString);
SaveListener save = new SaveListener(hireButton);
hireButton.setActionCommand(saveString);
hireButton.addActionListener(save);
hireButton.setEnabled(false);

deleteButton = new JButton(delString);
deleteButton.setActionCommand(delString);
deleteButton.addActionListener(new DeleteListener());

ename = new JTextField(10);
ename.addActionListener(save);
ename.getDocument().addDocumentListener(save);
String name = listModel.getElementAt(list.getSelectedIndex()).toString();

//Create a panel that uses BoxLayout.
JPanel pane = new JPanel();
pane.setLayout(new BoxLayout(pane, BoxLayout.LINE_AXIS));
pane.add(deleteButton);
pane.add(Box.createHorizontalStrut(5));
pane.add(new JSeparator(SwingConstants.VERTICAL));
pane.add(Box.createHorizontalStrut(5));
pane.add(ename);
pane.add(hireButton);
pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

add(listScrollPane, BorderLayout.CENTER);
add(pane, BorderLayout.PAGE_END);
}

class DeleteListener implements ActionListener {
public void actionPerformed(ActionEvent e) {

int index = list.getSelectedIndex();
listModel.remove(index);

int size = listModel.getSize();

if (size == 0) { //Nobody's left, disable firing.
deleteButton.setEnabled(false);

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

list.setSelectedIndex(index);
list.ensureIndexIsVisible(index);
}
}
}

August 26, 2008 at 8:42 PM

//This listener is shared by the text field and the save 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 = ename.getText();

//User didn't type in a unique name...
if (name.equals("") || alreadyInList(name)) {
Toolkit.getDefaultToolkit().beep();
ename.requestFocusInWindow();
ename.selectAll();
return;
}

int index = list.getSelectedIndex();
if (index == -1) {
index = 0;
} else {
index++;
}

listModel.insertElementAt(ename.getText(), index);

//Reset the text field.
ename.requestFocusInWindow();
ename.setText("");

//Select the new item and make it visible.
list.setSelectedIndex(index);
list.ensureIndexIsVisible(index);
}
protected boolean alreadyInList(String name) {
return listModel.contains(name);
}

//Required by DocumentListener.
public void insertUpdate(DocumentEvent e) {
enableButton();
}

//Required by DocumentListener.
public void removeUpdate(DocumentEvent e) {
handleEmptyTextField(e);
}

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

private void enableButton() {
if (!alreadyEnabled) {
button.setEnabled(true);
}
}

private boolean handleEmptyTextField(DocumentEvent e) {
if (e.getDocument().getLength() <= 0) {
button.setEnabled(false);
alreadyEnabled = false;
return true;
}
return false;
}
}

August 26, 2008 at 8:43 PM

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

if (list.getSelectedIndex() == -1) {
//No selection, disable deleteButton.
deleteButton.setEnabled(false);

} else {
//Selection, enable the deleteButton button.
deleteButton.setEnabled(true);
}
}
}

private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Single list example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.
JComponent newContentPane = new SingleList();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
frame.setSize(300,200);
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/

Thanks.

Amardeep.









Related Tutorials/Questions & Answers:
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
Advertisements
JList
JList  pls tell me about the concept the JList in corejava? and tell me a suitable example
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 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 in swings
jlist in swings  how to populate jlist with all the data retrieved from the ms access database?plzz help
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
JList Background Images..Urgent
JList Background Images..Urgent  I has done a JList that can change... javax.swing.event.*; public class ListTest extends JFrame{ private JList colorList... FlowLayout()); colorList= new JList(colorNames
Jtree connection with Jlist
Jtree connection with Jlist  I have a Jtree which contain different folders having files but the jtree will show only the folders. Now I want when I click a folder then its contained file will be shown in jList .How
Jtree connection with Jlist
Jtree connection with Jlist  I have a Jtree which contain different folders having files but the jtree will show only the folders. Now I want when I click a folder then its contained file will be shown in jList .How
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 - Swing AWT
JList  May i know how to add single items to JList. What...(); // model for = the JList JList list =3D new JList(listModel); you can... ListSelectionListener { private JList list; private 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
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
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
Display JList value selected from the JOptionPane
Display JList value selected from the JOptionPane In this section, we... that value in the JList. For this purpose, we have created a button and JList..."; String input = ""; DefaultListModel listModel; JList list; public
how to know which jList is selected and according to that fetch the value
how to know which jList is selected and according to that fetch the value ... rows of databse with employee id as primary key) How to know which jList is selected so that i get the value of employee_id from the correct jList (i.e which
Setting Tool Tip Text for items in a JList Component
Setting Tool Tip Text for items in a JList Component... the tool tip text for items present in the JList component of the Java Swing... in the JList component in Java Swing. In this program, you can add more and more items
Java Swing dependent JList
Java Swing dependent JList In this section, you will learn how to create...(" "); final JList list1 = new JList(db); JScrollPane pane1 = new...(); final JList list2 = new JList(model); JScrollPane pane2 = new JScrollPane(list2
Setting the Dimensions of an Item in a JList Component in Java
Setting the Dimensions of an Item in a JList Component in Java...;the Dimensions of an Item in  a JList Component...;JList(model){   public String getToolTipText(MouseEvent e
java - Java Beginners
java  How do i display data into a JList from a Database
database
database  In my project where u helped me to display the databases present im mysql database on the Jlist and then the respective tables in that database on another jlist and then the column names on another jlist I dont want
java
java  how to populate jlist with all the data retrieved from the ms access database?plzz help
coding problem - Java Beginners
to add values to JList from ArrayList object  Hi Friend, Try... CreateJList{ public static void main(String[] args) { JList jlist = new JList...( )); jlist.setFocusable(false); JFrame frame = new JFrame("Creating a JList
Database
Database  Thank you for ur immediate reply.... I wanted to know if it is possible to the retrieve and display the names of the databases that are present in mysql on a jlist
database
the names of the databases present in mysql database on a Jlist that is already... present under that database should be displayed on another Jlist which is present... on another jlist which is present on the gui. Kindly help me.....Thank You
complilation error - Java Beginners
JListSimpleExample(); } private JList sampleJList; private JTextField valueField; public JListSimpleExample(){ super("creating a simple JList...()); ContainerContent = get.ContentPanel(); //create the jlist , set the number of visible
How to save data - Swing AWT
then in jList or or Jtable many data viewer in one button,then another button must save data from jList and Jtable(in jList or jTable data will be many).Thank's... the Dimensions of an Item in a JList Component"); JPanel panel = new JPanel
java swing - Swing AWT
java swing  how i can insert multiple cive me exampleolumn and row in one JList in swing?plz g  Hi Friend, Please clarify your question. Thanks
jbutton - Java Beginners
jbutton  Hi, I have jlist and jbutton options.If i select... are displyed in the jlist using jbutton(refresh button). I want to know how to refresh the jlist while clicking the refresh button .please send the sample code. its
java swings - Java Beginners
*; public class CreateJList { JList list1; JList list2; DefaultListModel...(); listModel2 = new DefaultListModel(); list2 = new JList(listModel2...); list1 = new JList(listModel1); button.addActionListener(new ActionListener
jsplitpane - Java Beginners
(400, 300); list = new JList(texts); list.addListSelectionListener..."); } public void valueChanged(ListSelectionEvent event) { JList source = (JList...(); description.setText(value.getDescription()); } private JList list; private JTextArea
java swings - Java Beginners
*; public class CreateJList{ JList list1; JList list2; DefaultListModel...(); list2 = new JList(listModel2); String[] listval={"one","two","three","four","five","six","seven"}; list1.setListData(listval); list1 = new JList
java swings - Java Beginners
, Valarmathi  class Listbox implements ListSelectionListener { private JList list1; private JList list2; private Vector v; public void valueChanged...", "three", "four"}; list1= new JList(data1
java swings - Java Beginners
class CreateJList{ JList list1; JList list2; DefaultListModel...(); list2 = new JList(listModel2); listModel1.addElement("ZEN...("Honda City"); list1 = new JList(listModel1); button.addActionListener(new
3 queries on java class
3 queries on java class  Hey. I need to do 3 queries on same class ( JAVA) One is to populate the combobox, the another one to populate tje jlist and the last one to use on button to open a file from database (BLOB) Can some
Adding checkbox to List as item
(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JList list = new JList(new CheckListItem[] { new CheckListItem("1"), new... mouseClicked(MouseEvent event){ JList list = (JList) event.getSource
java swings - Java Beginners
{ JList list1; JList list2; DefaultListModel listModel1, listModel2... DefaultListModel(); list2 = new JList(listModel2); String[] listval={"one","two","three","four"}; list1.setListData(listval); list1 = new JList
Java GUI problem with linklists and administration - Java Beginners
Java GUI problem with linklists and administration  Hi, I'm having trouble making a GUI interface in java, where a list of names are displayed using a jList. The java program should be able to allow the user to add names, remove
jsplitpane - Java Beginners
; public SplitPaneFrame() { setSize(400, 300); list = new JList(texts... valueChanged(ListSelectionEvent event) { JList source = (JList) event.getSource...(value.getDescription()); } private JList list; private JTextArea
jsplitpane - Java Beginners
{ public SplitPaneFrame(){ setSize(400, 300); list = new JList(texts... void valueChanged(ListSelectionEvent event) { JList source = (JList...(); description.setText(value.getDescription()); } private JList list; private
List to be visible only when entering any string into textfield
List to be visible only when entering any string into textfield  how to do auto complete with jlist or combobox...actually using in the combobox i have to get the name associated with the entered first letter only....if using
problem in using getKeyCode()
problem in using getKeyCode()  i am working with the jlist...while i was trying to use the arrow keys in the keyboard to select the values in the list it is not working properly...the code which i used is given below
java swings - Java Beginners
; JList jbossactivemqlist,jbossibmmqlist; JLabel jbosslabel; JButton... DefaultListModel(); jbossibmmqlist = new JList(jbossibmmqlistmodel... StringBuilder(); jbossactivemqlist = new JList(jbossactivemqlistmodel
java swings - Java Beginners
= 6877636757727044238L; JList jbossactivemqlist,jbossibmmqlist; JLabel...(); jbossibmmqlistmodel = new DefaultListModel(); jbossibmmqlist = new JList... strbuf = new StringBuilder(); jbossactivemqlist = new JList
jsplitpane - Java Beginners
, 400); list = new JList(texts); list.addListSelectionListener..., "Center"); } public void valueChanged(ListSelectionEvent event) { JList source = (JList) event.getSource(); Display value = (Display
Java-swings - Java Beginners
(Locale.US); JList list = new JList(symbols.getMonths
Java Swing
class AddValueToList extends JFrame { JList list; DefaultListModel model... DefaultListModel(); list = new JList(model); model.addElement("C/C
jdbc & sql related project - JDBC
= null; JList list; DefaultListModel model; public Application...){} list = new JList(model); scrollpane=new JScrollPane(list...()){ model1.addElement(rs.getString(3)); } final JList list1 = new JList(model1
java swings - Java Beginners
{ /** * */ private static final long serialVersionUID = 6877636757727044238L; JList... JList(jbossibmmqlistmodel); jbossactivemqlist = new JList(jbossactivemqlistmodel); jbossactivemqlist = new JList

Ads