Java JList

Java JList

Java JList
Java JList class inherits from JComponent class.It allows the user to select single or multiple items from a list.

How to Declare Java JList class

This is how java.swing.JList class is declared.
[pclass JList extends JComponent implements Scrollable, Accessible]

Used JList constructors

  • JList()-creates empty read-only model JList.
  • JList(array [] data)-creates a JList that display elements from a specified array.
  • JList(ListModel<array> datamodel)-creates JList that display elements from a model.

JList commonly used methods

  • void addListSelectionListener()-adds listener to a list and notifies once selection occurs.
  • int getSelectedIndex()-returns smallest selected cell index.
  • ListModel getModel()- gets model that holds list data.
  • void setListData(Object [] data)-creates ListModel from array.

Java JList Example

[
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.DefaultListModel;
public class JListExample extends JFrame{

    JListExample()
    {
        super("JFrame Example");

        DefaultListModel<String> model=new DefaultListModel();
        model.addElement("Mango");
        model.addElement("Orange");
        model.addElement("Banana");
        model.addElement("Watermelon");

        JList<String> list=new JList(model);

        list.setBounds(50,50, 75,75);
        add(list);
        setSize(400,400);
        setLayout(null);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }
    public static void main(String args[])
    {
        new JListExample();
    }

}
]



Java JList with ActionListener Example

[
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JButton;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class JListExample extends JFrame implements ActionListener{
    public JList<String> list;
    public JLabel label;
    JListExample()
    {
        super("JFrame Example");

        DefaultListModel<String> model=new DefaultListModel();
        model.addElement("Mango");
        model.addElement("Orange");
        model.addElement("Banana");
        model.addElement("Watermelon");

        list=new JList(model);

        list.setBounds(50,100, 75,75);
        JButton b=new JButton("Show");
        b.setBounds(50, 200, 100, 30);
        b.addActionListener(this);

        add(b);



        add(list);
        label=new JLabel("Label");
        label.setBounds(50,50,300,50);
        add(label);

        setSize(400,400);
        setLayout(null);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }
    public static void main(String args[])
    {
        new JListExample();
    }

    public void actionPerformed(ActionEvent event)
    {
        String s="";
        if(list.getSelectedIndex()!=-1)
        {
            s="Fruit selected is "+list.getSelectedValue();
            label.setText(s);

        }
    }

}
]

Output:

Java JList2


See also:

Java JTable

Java JComboBox


COMMENTS

Name

android android Apps android emulators antivirus blogger bluestack Break Statement in Java constructors download downloader drivers emulators java java control statements java do-while loop java for loop java infinitive do-while loop java system methods java tutorials Java While Loop java.awt java.swing javalookandfeel javascript JButton JCheckBox JColorChooser JComboBox JDialog JFileChooser JFrame JLayeredPane JList JMenuBar joomla JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JScrollBar JSlider JSpinner JTabbedPane JTable JTextArea JTextField JToolBar mysql php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
false
ltr
item
Java Codes: Java JList
Java JList
Java JList
https://3.bp.blogspot.com/-rOq91g5gdRY/WMOhz3oiqFI/AAAAAAAAHIU/7xnma_3jU1EVL8OZ18i-GD0o5m4oV7kIQCLcB/s200/Java-JList-example-min.PNG
https://3.bp.blogspot.com/-rOq91g5gdRY/WMOhz3oiqFI/AAAAAAAAHIU/7xnma_3jU1EVL8OZ18i-GD0o5m4oV7kIQCLcB/s72-c/Java-JList-example-min.PNG
Java Codes
http://www.download-all.net/2017/03/java-jlist.html
http://www.download-all.net/
http://www.download-all.net/
http://www.download-all.net/2017/03/java-jlist.html
true
7826648292785071009
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy