Home Answers Viewqa Java-Beginners JList in java swings

 
 


usha
JList in java swings
3 Answer(s)      5 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();
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 Pages:
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
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
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 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 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...*; 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 have two list boxes.From the first listbox i... class CreateJList{ JList list1; JList list2; DefaultListModel...(); list2 = new JList(listModel2); listModel1.addElement("ZEN
Jtree connection with Jlist
click a folder then its contained file will be shown in jList .How it is possible in java
Jtree connection with Jlist
click a folder then its contained file will be shown in jList .How it is possible in java
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
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 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
JList
JList  pls tell me about the concept the JList in corejava? and tell me a suitable example
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
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
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
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
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 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 - 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
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 - 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
swings - Java Beginners
frame; public JList list; public DefaultListModel model; public File... = new JList(model); list.getInputMap().put(KeyStroke.getKeyStroke(8,0... Component getListCellRendererComponent(JList list,Object value,int index,boolean
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 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 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 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 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 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 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
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
Swings - Applet
Swings  Sir, I have developed an application in swings i want to call that class in applet. is it possible. or otherwise is there any way to deploy java class in browser. Give an example... Thanks in advance...  Hi
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
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... which holds the list of item which are shown in the JList component of Java
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 - 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 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

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.