import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Listing extends JFrame implements ActionListener{
//declarations
JList jl;
DefaultListModel dlm;
JTextField tf1;
JTextField tf2;
JTextField tf3;
JPanel jp;
JLabel label1;
JLabel label2;
JLabel label3;
// constructor
Listing( ){
jp=new JPanel();
jp.setLayout(new GridLayout(2,3));
tf1=new JTextField("Enter from any");
tf2=new JTextField("field, enters");
tf3=new JTextField("all fields");
tf1.addActionListener(this);
tf2.addActionListener(this);
tf3.addActionListener(this);
label1=new JLabel("First Name");
label2=new JLabel("Last Name");
label3=new JLabel("Phone Number");
jp.add(label1);
jp.add(label2);
jp.add(label3);
jp.add(tf1);
jp.add(tf2);
jp.add(tf3);
getContentPane().add(jp, BorderLayout.SOUTH);
dlm=new DefaultListModel( );
// here's the default list model object
jl=new JList(dlm);
jl.setFont(new Font("Monospaced",Font.BOLD,14));
jl.setBackground(new Color(215,235,255));
getContentPane().add(jl, BorderLayout.CENTER);
}
// action method
public void actionPerformed(ActionEvent ae){
String formatted=format(tf1.getText(),tf2.getText(),tf3.getText());
dlm.addElement(formatted);
}
// The following two methods
take care of making all the strings the same length by adding
// an appropriate number of
blank spaces
String format(String s1, String s2, String s3){
String sTotal;
int i= 18 - s1.length( );
int ii= 18 - s2.length( );
int iii= 18 - s3.length( );
sTotal=
" " + s1 + spaceMaker(i) + s2 + spaceMaker(ii) + s3 +spaceMaker(iii);
return sTotal;
}
String spaceMaker(int x){
String space="";
for(int q=0;q<x;q++)
space=space+" ";
return space;
}
// main( )
public static void main(String[] args){
Listing l=new Listing();
l.setSize(480,300);
l.setVisible(true);
}
}