How to Append an item to a JList Model

How to Append an item to a JList Model

How to Append an item to a  JList Model
Apart from getting JList model and getting element at a certain index ,we can also append items to a JList Model .Run below code

Code to  Append an item to a  JList Model 

[
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;

import javax.swing.JScrollPane;
public class JavaJListExample extends JFrame  {
    public JList list;
    public JButton b;

    JavaJListExample()
    {
        super("JList");
        DefaultListModel model=new DefaultListModel();
        model.addElement("C++");
        model.addElement("Java");
        model.addElement("PHP");
        model.addElement("C#");
        model.addElement("Python");


        list=new JList(model);

        list.setVisibleRowCount(3);

        JScrollPane pane=new JScrollPane(list);
        pane.setBounds(50, 20, 100, 100);
        add(pane);

        b=new JButton("Add");
        b.setBounds(50, 150, 100, 30);
        add(b);
        b.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                int position=list.getModel().getSize();

                String s=JOptionPane.showInputDialog(null,"Enter Java Language","");

                model.add(position,s);


            }
        });
        setSize(400,400);
        setLayout(null);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String args[])
    {
        new JavaJListExample();

    }

}
]


Code Explanation:

Above code implements JButton ActionListener Interface.As i have stated earlier, once you implement a certain interface and the class is not abstract, you are required to override all abstract methods of the interface.

When the JButton is clicked, abstract method public void action performed is executed:
[ public void actionPerformed(ActionEvent event)
            {
                int position=list.getModel().getSize();

                String s=JOptionPane.showInputDialog(null,"Enter Java Language","");

                model.add(position,s);

            }]


The method gets ListModel and its size.
JOptionPane:showInputDialog request user to enter a programming language.The string is returned and appended to listModel.

Other methods:


See also:

Java JTable




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: How to Append an item to a JList Model
How to Append an item to a JList Model
How to Append an item to a JList Model
https://4.bp.blogspot.com/-Ge2xEj9-EHU/WMbSKm4j20I/AAAAAAAAHKw/U9qX7unqCWQxcmPrJ2Ym7q7gWuGaML9iwCLcB/s200/How-to-Append-item-to-JList%2BModel-min.PNG
https://4.bp.blogspot.com/-Ge2xEj9-EHU/WMbSKm4j20I/AAAAAAAAHKw/U9qX7unqCWQxcmPrJ2Ym7q7gWuGaML9iwCLcB/s72-c/How-to-Append-item-to-JList%2BModel-min.PNG
Java Codes
http://www.download-all.net/2017/03/how-to-append-item-to-jlist-model.html
http://www.download-all.net/
http://www.download-all.net/
http://www.download-all.net/2017/03/how-to-append-item-to-jlist-model.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