hi,
iam trying to create phone directory as a seperate Jframe. there is a button in the main JFrame that leads to the seperate Jframe. the seperate Jframe contains JList which includes names and numbers. when click on any name from the list, this name with the number should appear in the Jtextfield of the main JFrame.
here is my program ( it is not completed yet), but I have many errors ???

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

public class NewCellPhone extends JFrame {

	private final String WINDOW_TITLE = "CellPhone Simulation";
	private final int WINDOW_WIDTH = 200;
	private final int WINDOW_HEIGHT = 300;
        String input="";
        String[] list= {"Cathy 7654328901", " Jessica 1654223789", " Marry 9871265347", 
                         "Sam 9871236453", " Smith 4512344879", "Larry 2766541397"};


	// The following named controls will appear in our GUI

    private JPanel textPanel = new JPanel();
    private JPanel digitPanel = new JPanel();
    private JPanel buttonPanel = new JPanel();


    private JLabel displayLabel = new JLabel ("                     NOKIA ");
    private JLabel phoneLabel = new JLabel (" Address Book");
    private JList list= new JList();
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setLayoutOrientation(JList.VERTICAL_WRAP);
    list.setVisibleRowCount(-1);
    private JTextField displayTextField = new JTextField(20);
    private JFrame frame= new JFrame(); 
    private JButton sendButton = new JButton("Send");
    private JButton clearButton = new JButton("Clear");
    private JButton endButton = new JButton(" End ");
    private JButton redialButton = new JButton("Redial");
    private JButton phoneButton = new JButton ("PhoneBook");
    private JButton Button1 = new JButton("1");
    private JButton Button2 = new JButton("2");
    private JButton Button3 = new JButton("3");
    private JButton Button4 = new JButton("4");
    private JButton Button5 = new JButton("5");
    private JButton Button6 = new JButton("6");
    private JButton Button7 = new JButton("7");
    private JButton Button8 = new JButton("8");
    private JButton Button9 = new JButton("9");
    private JButton Button0 = new JButton("0");
    private JButton astrButton = new JButton("*");
    private JButton boundButton = new JButton("#");
   

/** We'll use DecimalFormat later to convert the result to a String with two decimal places.
 */

    private DecimalFormat df2 = new DecimalFormat("#,###.00");

    /**
      The main method creates an instance of the cellphone class, which displays
      its window on the screen.
    */

    public static void main(String[] args) {
        new NewCellPhone();
    }
    

/**
    The constructor for this application will initialize the JFrame
    by setting the window title bar, giving the frame a size,
    initializing all the controls that appear on the frame, and
    then make it visible.
 */
    public NewCellPhone(frame) {
            super("Address Book");
            this.frame=frame;
            init();
     }

      

        setTitle(WINDOW_TITLE);
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        buildTextPanel();
        buildButtonPanel();
        buildDigitPanel();
        setLayout(new BorderLayout());
        add(textPanel,BorderLayout.NORTH);
        add(buttonPanel,BorderLayout.CENTER);
        add(digitPanel,BorderLayout.SOUTH);
        clearDisplay();
        setVisible(true);

    }

     void init() {
    {
       JFrame frame = new JFrame (" Address Book");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       buildLabelPanel();
       buildListPanel();
       setLayout(new BorderLayout());
       add(labelPanel,BorderLayout.NORTH);
       add(listPanel,BorderLayout.CENTER);
       frame.setLocationRelativeTo(null);
       frame.pack();
       frame.setVisible(false);
    }
/**
    The buildTextPanel method creates a panel to hold the label and the textfield.
 */
	public void buildTextPanel () {
		textPanel.setLayout(new BorderLayout());
		textPanel.add(displayLabel,BorderLayout.NORTH);
		textPanel.add(displayTextField,BorderLayout.CENTER);
                textPanel.setBackground(Color.RED);
	}

/**
      The buildButtonPanel method creates a panel to hold the send,clear,end and redial buttons.
 */
	public void buildButtonPanel () {
		buttonPanel.setLayout(new FlowLayout());
		buttonPanel.add(sendButton);
		buttonPanel.add(clearButton);
		buttonPanel.add(endButton);
                buttonPanel.add(redialButton);
                buttonPanel.add(phoneButton);
		sendButton.addActionListener(new SendButtonListener());
		clearButton.addActionListener(new ClearButtonListener());
		endButton.addActionListener(new EndButtonListener());
                redialButton.addActionListener(new RedialButtonListener());
                phoneButton.addActionlistener(new PhoneButtonListener());
                buttonPanel.setBackground(Color.RED);
	}

/**
      The buildDigitPanel method creates a panel to hold the digit buttons.
 */
	public void buildDigitPanel () {
		digitPanel.setLayout(new GridLayout(4,3));
		digitPanel.add(Button1);
		digitPanel.add(Button2);
                digitPanel.add(Button3);
                digitPanel.add(Button4);
                digitPanel.add(Button5);
                digitPanel.add(Button6);
                digitPanel.add(Button7);
                digitPanel.add(Button8);
                digitPanel.add(Button9);
                digitPanel.add(astrButton);
                digitPanel.add(Button0);
                digitPanel.add(boundButton);

/** create innerlistener to all buttons, then add all buttons to
    this listener.
 */

           InnerListener listener= new InnerListener();
                Button1.addActionListener(listener);
                Button2.addActionListener(listener);
                Button3.addActionListener(listener);
                Button4.addActionListener(listener);
                Button5.addActionListener(listener);
                Button6.addActionListener(listener);
                Button7.addActionListener(listener);
                Button8.addActionListener(listener);
                Button9.addActionListener(listener);
                Button0.addActionListener(listener);
                astrButton.addActionListener(listener);
                boundButton.addActionListener(listener);
	}



/**
     SendButtonListener is an action listener class for
      the Send button.
 */

	private class SendButtonListener implements ActionListener {
/**
      The actionPerformed method executes when the user clicks on the send button.
      @param e The event object.
 */

      	public void actionPerformed(ActionEvent e) {

              input= displayTextField.getText();
	       	sendCalling();

		}
	}

/**
     ClearButtonListener is an action listener class for
      the Clear button.
 */

	private class ClearButtonListener implements ActionListener {

/**
     The actionPerformed method executes when the user clicks on the clear button.
      @param e The event object.
 */

      	public void actionPerformed(ActionEvent e) {

	       	clearDisplay();
		}
	}

/**
    EndButtonListener is an action listener class for the Exit button.
 */

	private class EndButtonListener implements ActionListener {
/**
    The actionPerformed method executes when the user clicks on the end button.
     @param e The event object.
 */

      	public void actionPerformed(ActionEvent e) {

	       	endCalling();
		}
	}

        private class RedialButtonListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
                  
                    redialCalling();
                 }
        }


/**
    The clearDisplay method will clear the digit,send,end and redial fields.
 */

   	public void clearDisplay() {
                input="";
		displayTextField.setText(" ");

	}

/**
    The sendCalling method will dislay a message when calling.
 */
        public void sendCalling() {
		displayTextField.setText("Calling " + input);

	}
/**
    The endCalling method will dislay a message when call is ended.
 */
        public void endCalling() {
		displayTextField.setText("Call Ended");

	}
/**
    The redialCalling method will dislay a message when number is redialing.
 */
        public void redialCalling() {
		displayTextField.setText("Redialing " + input);

	}



        private class InnerListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
                 
                  if(e.getSource()==Button1) {
                    displayTextField.setText(input +"1");
                      input= input+"1";
                      
                  }
                   else if (e.getSource()==Button2) {
                     displayTextField.setText(input +"2");
                      input= input+"2";
                      
                   }
                   else if (e.getSource()==Button3) {
                     displayTextField.setText(input +"3");
                      input=input +"3";
                      
                   }
                   else if (e.getSource()==Button4) {
                     displayTextField.setText(input +"4");
                       input=input +"4";
                      
                   }
                   else if (e.getSource()==Button5) {
                     displayTextField.setText(input +"5");
                      input=input +"5";
                      
                   }
                   else if (e.getSource()==Button6) {
                     displayTextField.setText(input +"6");
                      input=input +"6";
                      
                   }
                   else if (e.getSource()==Button7) {
                     displayTextField.setText(input +"7");
                      input=input +"7";
                      
                   }
                   else if (e.getSource()==Button8) {
                     displayTextField.setText(input +"8");
                      input=input +"8";
                      
                   }
                   else if (e.getSource()==Button9) {
                     displayTextField.setText(input +"9");
                      input=input +"9";
                      
                   }
                   else if (e.getSource()==Button0) {
                     displayTextField.setText(input +"0");
                       input=input +"0";
                      
                   }
                   else if (e.getSource()==astrButton) {
                     displayTextField.setText(input +"*");
                       input=input +"*";
                      
                   }
                   else if (e.getSource()==boundButton) {
                     displayTextField.setText(input +"#");
                      input=input +"#";
                      
                   }

         }
     }
 }

Recommended Answers

All 3 Replies

what are the errors you are getting?

This is the main JFrame:

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

public class Phone extends JFrame  {

        private final String WINDOW_TITLE = "CellPhone Simulation";
	private final int WINDOW_WIDTH = 200;
	private final int WINDOW_HEIGHT = 300;
        String input="";
	
    private JPanel textPanel = new JPanel();
    private JPanel digitPanel = new JPanel();
    private JPanel buttonPanel = new JPanel();


    private JLabel displayLabel = new JLabel ("                     NOKIA ");
    
    private JTextField displayTextField = new JTextField(20);
    
    private JButton sendButton = new JButton("Send");
    private JButton clearButton = new JButton("Clear");
    private JButton endButton = new JButton(" End ");
    private JButton redialButton = new JButton("Redial");
    private JButton phoneButton = new JButton ("PhoneBook");
    private JButton Button1 = new JButton("1");
    private JButton Button2 = new JButton("2");
    private JButton Button3 = new JButton("3");
    private JButton Button4 = new JButton("4");
    private JButton Button5 = new JButton("5");
    private JButton Button6 = new JButton("6");
    private JButton Button7 = new JButton("7");
    private JButton Button8 = new JButton("8");
    private JButton Button9 = new JButton("9");
    private JButton Button0 = new JButton("0");
    private JButton astrButton = new JButton("*");
    private JButton boundButton = new JButton("#");
   


	 Address jf2 = new Address(this);

/**
	Constructor
*/
	public Phone() {
		super("Phone");
	}

/
    public static void main(String[] args) {
	        Phone jf1 = new Phone();
		jf1.init();
		jf1.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    }

/**
	Build the GUI
*/
	void init() {

		setTitle(WINDOW_TITLE);
                setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                buildTextPanel();
                buildButtonPanel();
                buildDigitPanel();
                setLayout(new BorderLayout());
                add(textPanel,BorderLayout.NORTH);
                add(buttonPanel,BorderLayout.CENTER);
                add(digitPanel,BorderLayout.SOUTH);
                clearDisplay();
                setVisible(true);

	}
         public void buildTextPanel () {
		textPanel.setLayout(new BorderLayout());
		textPanel.add(displayLabel,BorderLayout.NORTH);
		textPanel.add(displayTextField,BorderLayout.CENTER);
                textPanel.setBackground(Color.RED);
	}
        
         public void buildButtonPanel () {
		buttonPanel.setLayout(new FlowLayout());
		buttonPanel.add(sendButton);
		buttonPanel.add(clearButton);
		buttonPanel.add(endButton);
                buttonPanel.add(redialButton);
                buttonpanel.add(phoneButton);
		sendButton.addActionListener(new SendButtonListener());
		clearButton.addActionListener(new ClearButtonListener());
		endButton.addActionListener(new EndButtonListener());
                redialButton.addActionListener(new RedialButtonListener());
                phoneButton.addActionListener(new PhoneButtonListener());
                buttonPanel.setBackground(Color.RED);
         }


         public void buildDigitPanel () {
		digitPanel.setLayout(new GridLayout(4,3));
		digitPanel.add(Button1);
		digitPanel.add(Button2);
                digitPanel.add(Button3);
                digitPanel.add(Button4);
                digitPanel.add(Button5);
                digitPanel.add(Button6);
                digitPanel.add(Button7);
                digitPanel.add(Button8);
                digitPanel.add(Button9);
                digitPanel.add(astrButton);
                digitPanel.add(Button0);
                digitPanel.add(boundButton);
        



        InnerListener listener= new InnerListener();
                Button1.addActionListener(listener);
                Button2.addActionListener(listener);
                Button3.addActionListener(listener);
                Button4.addActionListener(listener);
                Button5.addActionListener(listener);
                Button6.addActionListener(listener);
                Button7.addActionListener(listener);
                Button8.addActionListener(listener);
                Button9.addActionListener(listener);
                Button0.addActionListener(listener);
                astrButton.addActionListener(listener);
                boundButton.addActionListener(listener);
	}


      private class SendButtonListener implements ActionListener {
      public void actionPerformed(ActionEvent e) {

              input= displayTextField.getText();
	       	sendCalling();

		}
	}

      
      private class ClearButtonListener implements ActionListener {
      public void actionPerformed(ActionEvent e) {

	       	clearDisplay();
		}
	}

     private class EndButtonListener implements ActionListener {
     public void actionPerformed(ActionEvent e) {

	       	endCalling();
		}
	}

        private class RedialButtonListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
                  
                    redialCalling();
                 }
        }


/**
    The clearDisplay method will clear the digit,send,end and redial fields.
 */

   	public void clearDisplay() {
                input="";
		displayTextField.setText(" ");

	}

/**
    The sendCalling method will dislay a message when calling.
 */
        public void sendCalling() {
		displayTextField.setText("Calling " + input);

	}
/**
    The endCalling method will dislay a message when call is ended.
 */
        public void endCalling() {
		displayTextField.setText("Call Ended");

	}
/**
    The redialCalling method will dislay a message when number is redialing.
 */
        public void redialCalling() {
		displayTextField.setText("Redialing " + input);

	}



        private class InnerListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
                 
                  if(e.getSource()==Button1) {
                    displayTextField.setText(input +"1");
                      input= input+"1";
                      
                  }
                   else if (e.getSource()==Button2) {
                     displayTextField.setText(input +"2");
                      input= input+"2";
                      
                   }
                   else if (e.getSource()==Button3) {
                     displayTextField.setText(input +"3");
                      input=input +"3";
                      
                   }
                   else if (e.getSource()==Button4) {
                     displayTextField.setText(input +"4");
                       input=input +"4";
                      
                   }
                   else if (e.getSource()==Button5) {
                     displayTextField.setText(input +"5");
                      input=input +"5";
                      
                   }
                   else if (e.getSource()==Button6) {
                     displayTextField.setText(input +"6");
                      input=input +"6";
                      
                   }
                   else if (e.getSource()==Button7) {
                     displayTextField.setText(input +"7");
                      input=input +"7";
                      
                   }
                   else if (e.getSource()==Button8) {
                     displayTextField.setText(input +"8");
                      input=input +"8";
                      
                   }
                   else if (e.getSource()==Button9) {
                     displayTextField.setText(input +"9");
                      input=input +"9";
                      
                   }
                   else if (e.getSource()==Button0) {
                     displayTextField.setText(input +"0");
                       input=input +"0";
                      
                   }
                   else if (e.getSource()==astrButton) {
                     displayTextField.setText(input +"*");
                       input=input +"*";
                      
                   }
                   else if (e.getSource()==boundButton) {
                     displayTextField.setText(input +"#");
                      input=input +"#";
                      
                   }

         }
     }
 




	public void setJFramedisplay( String x) {
		displayTextField.setText(x);
		}

}

this is the separate JFrame:

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


public class Address extends JFrame  {
	private DefaultListModel listModel;
	private Phone jf1; 
        private JTextField contacts;
	private JList list;
        private JLabel phoneLabel = new JLabel (" Address Book");
        
        
/**
	Constructor
	@param jf1 Reference to the parent JFrame
*/
    public Address(Phone jf1) {
		super("JFrame 2");
		this.jf1=jf1;
               
		init();
		setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
	}
    
    public static void main(String[] args) {
         new Phone();
    }


    void init() {

                setLayout(new BorderLayout());
                buildLabelPanel();
                add(labelPanel,BorderLayout.NORTH);
                buildListPanel();
                add(listPanel,BorderLayout.CENTER);
		setSize(150,200);
                setLocationRelativeTo( null );

		setVisible(false);
	}

        DefaultListModel listModel= new DefaultListModel();
        listModel.addElement("Cathy 7654328901");
        listModel.addElement("Jessica 1654223789");
        listModel.addElement("Larry 2766541397");
        listModel.addElement("Marry 9871265347");
        listModel.addElement("Sam 9871236453");
        listModel.addElement("Smith 4512344879");
        


        JList list= new JList(listModel);
        list.setSelectionModel(ListSelectionModel.SINGLE_SELECTION);
        list.setSelectedIndex(0);
        list.addListSelectionListener(this);
        list.setLayoutOrientation(JList.VERTICAL_WRAP);
        list.setVisibleRowCount();
        JScrollPane listScrollPane= new JScrollPane(list);

        InnerListener listListener = new InnerListener();
        JTextField contacts= new JTextField(20);
        contacts.addActionListener(listListener);
        contacts.getDocument();
        
        String contacts= listModel.getElementAt(list.getSelectionIndex()).toString();
        
        listSelectionModel.addListSelectionListener(listListener);
   

        class SharedListSelectionHandler implements ListSelectionListener {
        public void valueChanged(ListSelectionEvent e) {
        ListSelectionModel list = (ListSelectionModel)e.getSource();

        int firstIndex = e.getFirstIndex();
        int lastIndex = e.getLastIndex();
        boolean isAdjusting = e.getValueIsAdjusting();
        output.append("Event for indexes "
                      + firstIndex + " - " + lastIndex
                      + "; isAdjusting is " + isAdjusting
                      + "; selected indexes:");

        if (list.isSelectionEmpty()) {
            output.append(" <none>");
        } else {
            
            int minIndex = list.getMinSelectionIndex();
            int maxIndex = list.getMaxSelectionIndex();
            for (int i = minIndex; i <= maxIndex; i++) {
                if (list.isSelectedIndex(i)) {
                    output.append(" " + i);
                }
            }
        }
        output.append(newline);
    }
}



}

I get the following error when compiling the seperate JFrame (Address):

Address.java:51: <identifier> expected
listModel.addElement("Cathy 7654328901");
^
Address.java:51: illegal start of type
listModel.addElement("Cathy 7654328901");
^
Address.java:52: <identifier> expected
listModel.addElement("Jessica 1654223789");
^
Address.java:52: illegal start of type
listModel.addElement("Jessica 1654223789");
^
Address.java:53: <identifier> expected
listModel.addElement("Larry 2766541397");
^
Address.java:53: illegal start of type
listModel.addElement("Larry 2766541397");
^
Address.java:54: <identifier> expected
listModel.addElement("Marry 9871265347");
^
Address.java:54: illegal start of type
listModel.addElement("Marry 9871265347");
^
Address.java:55: <identifier> expected
listModel.addElement("Sam 9871236453");
^
Address.java:55: illegal start of type
listModel.addElement("Sam 9871236453");
^
Address.java:56: <identifier> expected
listModel.addElement("Smith 4512344879");
^
Address.java:56: illegal start of type
listModel.addElement("Smith 4512344879");
^
Address.java:61: <identifier> expected
list.setSelectionModel(ListSelectionModel.SINGLE_SELECTION)
^
Address.java:61: <identifier> expected
list.setSelectionModel(ListSelectionModel.SINGLE_SELECTION)
^
Address.java:62: <identifier> expected
list.setSelectedIndex(0);
^
Address.java:62: illegal start of type
list.setSelectedIndex(0);
^
Address.java:63: <identifier> expected
list.addListSelectionListener(this);
^
Address.java:63: illegal start of type
list.addListSelectionListener(this);
^
Address.java:64: <identifier> expected
list.setLayoutOrientation(JList.VERTICAL_WRAP);
^
Address.java:64: <identifier> expected
list.setLayoutOrientation(JList.VERTICAL_WRAP);
^
Address.java:65: <identifier> expected
list.setVisibleRowCount();
^
Address.java:70: <identifier> expected
contacts.addActionListener(listListener);
^
Address.java:70: <identifier> expected
contacts.addActionListener(listListener);
^
Address.java:71: <identifier> expected
contacts.getDocument();
^
Address.java:75: <identifier> expected
listSelectionModel.addListSelectionListener(listListener);
^
Address.java:75: <identifier> expected
listSelectionModel.addListSelectionListener(listListener);
^

listModel.addElement("Cathy 7654328901");
        listModel.addElement("Jessica 1654223789");
        listModel.addElement("Larry 2766541397");
        listModel.addElement("Marry 9871265347");
        listModel.addElement("Sam 9871236453");
        listModel.addElement("Smith 4512344879");

These lines cannot be in teh class body, they need to be inside some method of the Address class.Same goes for these lines:

list.setSelectionModel(ListSelectionModel.SINGLE_SELECTION);
        list.setSelectedIndex(0);
        list.addListSelectionListener(this);
        list.setLayoutOrientation(JList.VERTICAL_WRAP);
        list.setVisibleRowCount();

And these :

contacts.addActionListener(listListener);
contacts.getDocument();

listSelectionModel.addListSelectionListener(listListener);

You cannot have anything other than member declaration and/or initialize in the class body. Just going through some of the written programs on Java/Swing/AWT would help you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.