JList with a DefaultListModel||Java Codes

JList with a DefaultListModel||Java Codes

JList with a DefaultListMode Example
 DefaultListModel is a ListModel implementation that extends from AbstractListModel class.Below program is example of how you can implement DefaultListModel in a JList

JList with a DefaultListMode Example

[
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JList;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JScrollPane;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;


public class DefaultListModelExample  extends JPanel{

    public JList list;
    public JButton addB,removeB;
    public static JFrame f;
    public DefaultListModel m;
    DefaultListModelExample()
    {
        m=new DefaultListModel();
        list=new JList(m);


        JScrollPane pane = new JScrollPane(list);
        add(pane);
        addB=new JButton("Add");
        addB.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                String s=JOptionPane.showInputDialog(null,
                        "Enter your favourite programming language",
                        "",JOptionPane.INFORMATION_MESSAGE);
                m.add(0,s);


            }

        });

        add(addB);

        removeB=new JButton("Remove");
        removeB.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event )
            {
                m.removeElementAt(0);
            }
        });

        add(pane, BorderLayout.NORTH);
        add(addB, BorderLayout.WEST);
        add(removeB, BorderLayout.EAST);

    }
    public static void main(String args[])
    {
        f=new JFrame(" DefaultListModelExample ");
        f.setContentPane(new  DefaultListModelExample ());
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(300,230);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

}

]

Code Explanation

Above class extends JPanel.Code [ f.setContentPane(new DefaultListModelExample ());] gets all JPanel contents and displays them.

The class has two JButtons.One button addElement to DefaultListModel while the other JButton removeElementAt index 0.

Code to addElement at Index 0 is [addB.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                String s=JOptionPane.showInputDialog(null,
                        "Enter your favourite programming language",
                        "",JOptionPane.INFORMATION_MESSAGE);
                m.add(0,s);


            }
        });]

[JOptionPane.showInputDialog]-gets the string to be added to DefaultListModel.

Code to remove Element at index 0 is [removeB.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event )
            {
                m.removeElementAt(0);
            }
        });

      
    }]
[setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);]-closes JFrame and exits application when close button is clicked.

[ add(pane, BorderLayout.NORTH);
        add(addB, BorderLayout.WEST);
        add(removeB, BorderLayout.EAST);]-this is the code that setLayout of components in JPanel.

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: JList with a DefaultListModel||Java Codes
JList with a DefaultListModel||Java Codes
JList with a DefaultListModel||Java Codes
https://3.bp.blogspot.com/-qwY-qkbpYU0/WMgVvc6qWLI/AAAAAAAAHL0/qM3GnBXltXsP57r4meshQqOswxjnRZ3RwCLcB/s200/JList-with-DefaultListMode-Example-min.PNG
https://3.bp.blogspot.com/-qwY-qkbpYU0/WMgVvc6qWLI/AAAAAAAAHL0/qM3GnBXltXsP57r4meshQqOswxjnRZ3RwCLcB/s72-c/JList-with-DefaultListMode-Example-min.PNG
Java Codes
http://www.download-all.net/2017/03/jlist-with-defaultlistmodeljava-codes.html
http://www.download-all.net/
http://www.download-all.net/
http://www.download-all.net/2017/03/jlist-with-defaultlistmodeljava-codes.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