import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class myJList extends JFrame implements ActionListener
{
String months[]={"January","February","March","April",
"May","June","July","August","September",
"October","November", "December"};
JList jlist1;
JPanel pane,pane2;
JScrollPane jsp;
JButton jbadd,jbdel,jbrall;
DefaultListModel dlm;
public myJList()
{
super("JList Example");
setSize(300,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dlm=new DefaultListModel();
int i;
for(i=0;i<months.length;i++)
dlm.addElement(months[i]);
jlist1=new JList(dlm);
jsp=new JScrollPane(jlist1);
pane=new JPanel();
pane2=new JPanel();
jbadd=new JButton("Add");
jbdel=new JButton("Remove");
jbrall=new JButton("Remove All");
pane2.add(jbadd);
pane2.add(jbdel);
pane2.add(jbrall);
jbadd.addActionListener(this);
jbdel.addActionListener(this);
jbrall.addActionListener(this);
pane.setLayout(new BorderLayout());
pane.add("Center",new JPanel().add(jsp));
pane.add("North",pane2);
setContentPane(pane);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jbrall)
{ dlm.removeAllElements();
}
if(e.getSource()==jbdel)
dlm.removeElementAt(jlist1.getSelectedIndex());
if(e.getSource()==jbadd)
{
String inputValue = JOptionPane.showInputDialog("Please input a value");
if(!inputValue.equals(""))
{
dlm.addElement(inputValue);
}
}
}
public static void main(String args[])
{
new myJList();
}
}