SwingFrame Modified       // to add TextField contents to the JList
                                                                     Peter Komisar

/* Instead of adding an array of Objects to the JList directly, this program adds the
array to a DefaultListModel. This class encapsulates the data and offers methods
to manipulate the data. Now pressing the button adds the contents of the text fields
to the JList. Old comments are removed and changes are highlighted in red        */

import javax.swing.*;  
import java.awt.*;
import java.awt.event.*;
 

class SwingFrame3 extends JFrame implements ActionListener{

JTextField field1;
JTextField field2;
JTextField field3;
JLabel label1;
JLabel label2;
JLabel label3;
DefaultListModel dl;              //  here's something new a DefaultListModel
  SwingFrame3(String s){
    super(s);

    JPanel jp = new JPanel( );  
      jp.setBackground(Color.white);
      jp.setForeground(Color.yellow);
      jp.setLayout(new GridLayout(3,2));
      label1=new JLabel("Country",0);
      label2=new JLabel("Government",0);
      label3=new JLabel("Language",0);

      field1=new JTextField("France");
      field1.setBackground(Color.black);
      field1.setForeground(Color.white);
      field2=new JTextField("Democracy");
      field3=new JTextField("French");
      field3.setEditable(false);
      field3.setBackground(Color.blue);
      field3.setForeground(Color.white);

      jp.add(label1);
      jp.add(field1);
      jp.add(label2);
      jp.add(field2);
      jp.add(label3);
      jp.add(field3);

     getContentPane( ).add(jp,BorderLayout.NORTH);

// the Object array

   Object[] countries=new Object[]{" ",
       "Some of the countries of the world in alphabetical order", " ",
       "Arabia","Borneo","Canada","Denmark","England","France","Holland",
       "Korea","Malaysia","Nepal","Paraguay","Sweden","Uraguay"};

// A DefaultListModel is built and a loop adds the contents of the countries array to it

      dl=new DefaultListModel( );
     for(int i=0;i<countries.length;i++){
       dl.add(i,countries[i]);
       }
// then the default list model object is added as an argument to the JList constructor

      JList jl= new JList(dl);
      JScrollPane jsp=new JScrollPane(jl);
 
      getContentPane().add(jsp);
 

      JButton jbS=new JButton("Add a Country");
      jbS.setToolTipText(" Press this to add a new country ");
      jbS.addActionListener(this);       // the listener, here the container, is added to the button
      getContentPane().add(jbS,BorderLayout.SOUTH);

      setSize(400,400);
      setVisible(true);

// window closer
                  addWindowListener(new WindowAdapter( ){
                  public void windowClosing(WindowEvent e){
                  System.exit(0);
                  }
               });
}

// action method

public void actionPerformed(ActionEvent ae){
System.out.println("Here's the action outputed to console");
String s= field1.getText( ) + " " + field2.getText( ) + " " + field3.getText( );
dl.addElement(s);
}

// the main( ) method

    public static void main(String args[]){
       SwingFrame3 sf=new SwingFrame3("Countries of the World");
       }
   }