I'm trying to write myself a code in which I can store names and then create a JList so that I can scroll through these names and then have a JMenuItem in the Contacts menu I have used to add a new name to the list. I have written the following that works for scrolling through the names as well as exiting the system but I think my poor programming has stopped me from being able to add a code so that new people can be "Added" Could someone please help because I'm stumped and it's starting to annoy me
//
//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class ListTest extends JFrame {
private JList nameList;
private Container container;
private final String Names [] = {"Morten Gamst", "Freddie Fred", "Steve Ridby"};
//
public ListTest(){
super ("List Test");
//
JMenu fileMenu = new JMenu ("File");
fileMenu.setMnemonic ('F');
JMenu contactMenu = new JMenu ("Contacts");
fileMenu.setMnemonic ('C');
//
container = getContentPane ();
container.setLayout (new GridLayout());
//
nameList = new JList (Names);
//
nameList.setSelectionMode (ListSelectionModel.SINGLE_SELECTION);
//
container.add (new JScrollPane (nameList));
JMenuItem exitItem = new JMenuItem ("Exit");
exitItem.setMnemonic ('x');
fileMenu.add (exitItem);
exitItem.addActionListener(
new ActionListener(){
public void actionPerformed (ActionEvent event)
{
System.exit (0);
}
}
);
JMenuItem addItem = new JMenuItem ("Add Contact");
addItem.setMnemonic ('a');
contactMenu.add(addItem);
JMenuBar bar = new JMenuBar ();
setJMenuBar (bar);
bar.add (fileMenu);
bar.add (contactMenu);
setSize (250,200);
setVisible (true);
}//
public static void main (String args[])
{
ListTest application = new ListTest();
application.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
}