jlist

jlist

View Answers

June 12, 2009 at 12:01 PM

Hi Friend,

We are providing you a code that will allow you to clear the list data.

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

public class ClearListData extends JPanel implements ListSelectionListener {
JList list;
DefaultListModel listModel;
JButton deleteAll;
JButton deleteButton;
JTextField nameField;
public ClearListData() {
super(new BorderLayout());
listModel = new DefaultListModel();
listModel.addElement("ZEN");
listModel.addElement("Alto");
listModel.addElement("Swift");
listModel.addElement("Centro");
listModel.addElement("Innova");
listModel.addElement("Scorpio");
listModel.addElement("Chevrolet");
listModel.addElement("Honda City");
list = new JList(listModel);
list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
list.setSelectedIndex(0);
list.addListSelectionListener(this);
JScrollPane listScrollPane = new JScrollPane(list);
deleteButton = new JButton("Clear Selected list item");
deleteButton.addActionListener(new DeleteButtonListener());

deleteAll = new JButton("Clear All");
deleteAll.addActionListener(new DeleteAllButtonListener());

nameField = new JTextField(15);
String name = listModel.getElementAt(list.getSelectedIndex()).toString();
nameField.setText(name);
JPanel panel = new JPanel();
panel.add(nameField);
panel.add(deleteButton);
panel.add(deleteAll);
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, panel,listScrollPane);
splitPane.setResizeWeight(0.5);
add(splitPane, BorderLayout.CENTER);
}
class DeleteButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
ListSelectionModel lsm = list.getSelectionModel();
int firstSelected = lsm.getMinSelectionIndex();
int lastSelected = lsm.getMaxSelectionIndex();
listModel.removeRange(firstSelected, lastSelected);
int size = listModel.size();
if (firstSelected == listModel.getSize()) {
firstSelected--;
}
list.setSelectedIndex(firstSelected);
}
}

class DeleteAllButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
listModel.clear();
}
}
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting() == false) {
if (list.getSelectedIndex() == -1) {
deleteButton.setEnabled(false);
deleteAll.setEnabled(false);
nameField.setText("");

} else if (list.getSelectedIndices().length > 1) {
deleteButton.setEnabled(true);
deleteAll.setEnabled(false);

} else {
deleteButton.setEnabled(true);
deleteAll.setEnabled(true);
nameField.setText(list.getSelectedValue().toString());
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Clear ListData");
JComponent component = new ClearListData();
component.setOpaque(true);
frame.setContentPane(component);
component.setMinimumSize(new Dimension(component.getPreferredSize().width, 100));
frame.pack();
frame.setVisible(true);
}
}
Thanks









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