Solved

how do I display customer info

Posted on 2007-04-01
3
401 Views
Last Modified: 2013-11-23
I am new to Java and I need Help with displaying customer info
here is my code
public class Customer
{
      // attribute definitions
       private String name;
       private String address;
       private String phoneNo;
            
      // constructor with parameters
      public Customer(String aName, String anAddress, String aPhoneNo)
      {
            // invoke setters to populate attributes
            setName(aName);
            setAddress(anAddress);
            setPhoneNo(aPhoneNo);
      }
      
      // getters
       public String getName()
             { return name;}
      public String getAddress()
             { return address;}
      public String getPhoneNo()
             { return phoneNo;}

      // setters
      public void setName(String newName)
            { name = newName;}
      public void setAddress(String newAddress)
            { address = newAddress;}
      public void setPhoneNo(String newPhoneNo)
            { phoneNo = newPhoneNo;}
      
      public String tellAboutSelf()
            {
                  String info;
                  info = "Customer name = "
                              + getName() + ", Address = "
                              + getAddress() + ", Phone Number = "
                              + getPhoneNo();
                  return info;
            }
}
and my display class
import java.awt.*;                   // Font, Color, Layout managers            
import javax.swing.*;             // GUI components            
import java.awt.event.*;       // ActionEvent, ActionListener

public class DisplayCustomer extends JFrame implements ActionListener
{
      // variables needing class scope
      JTextField customerNameText, customerAddressText, customerPhoneText;
      JButton acceptButton, clearButton, closeButton;
      Customer aCustomer;       
      String customerName, customerAddress, customerPhone;         

      public static void main(String args[])
      {      // create instance of Frame
            DisplayCustomer frame = new DisplayCustomer();
      }
      // constructor
      public DisplayCustomer()
      {
            Container c = this.getContentPane();
            c.setLayout(new GridLayout(3,1));
            JPanel centerPanel = new JPanel(new GridLayout(3,2));
            JPanel lowerPanel = new JPanel(new FlowLayout());

            // create logo
            JLabel logoLabel = new JLabel(" ",SwingConstants.CENTER);
            logoLabel.setForeground(Color.red);
            logoLabel.setFont(new Font("TimesRoman", Font.ITALIC,36));
            logoLabel.setText("Bradshaw Marina");
            c.add(logoLabel);       

            // create TextFields for name, address & phone
            customerNameText = new JTextField();
            customerAddressText = new JTextField();            
            customerPhoneText = new JTextField();
            // add labels and TextFields to the panels
            centerPanel.add(new JLabel("Name: ", SwingConstants.RIGHT));
            centerPanel.add(customerNameText);
            centerPanel.add(new JLabel("Address: ", SwingConstants.RIGHT));
            centerPanel.add(customerAddressText);            
            centerPanel.add(new JLabel("Phone: ", SwingConstants.RIGHT));
            centerPanel.add(customerPhoneText);            
            c.add(centerPanel);
            // create & add Buttons to bottom panel
            acceptButton = new JButton("Accept");
            clearButton = new JButton("Clear");
            closeButton = new JButton("Close");
            lowerPanel.add(acceptButton);
            lowerPanel.add(clearButton);
            lowerPanel.add(closeButton);
            c.add(lowerPanel);             
            
            // register frame as listener for button events
            acceptButton.addActionListener(this);
            clearButton.addActionListener(this);            
            closeButton.addActionListener(this);            

            this.setSize(300,200);    
            this.setTitle("Display A Customer");
            this.setVisible(true);

            // create anonymous inner class to handle window closing event
            this.addWindowListener(new WindowAdapter()
                  {
                        public void windowClosing(WindowEvent event)
                                 {shutDown();}
                  }       
             );
   }
   // actionPerformed is invoked when a Button is clicked
   public void actionPerformed(ActionEvent e)
   {
            // see which button was clicked
            if(e.getSource() == acceptButton)
                 {displayCustomer();}
              if(e.getSource() == clearButton)
                 {clearForm();}
            if(e.getSource() == closeButton)
                  {shutDown();}
   }
      private void displayCustomer()
      {
            customerName = customerNameText.getText();      
            customerAddress = customerAddressText.getText();            
            customerPhone = customerPhoneText.getText();      
            
            if(customerName.length() == 0 || customerAddress.length() == 0 || customerPhone.length() == 0)
               JOptionPane.showMessageDialog(this, "Please Enter All Data ");
         else  
                  {
                     aCustomer = new Customer(customerName, customerAddress, customerPhone);
                   JOptionPane.showMessageDialog(this, /*System.out.print*/(aCustomer));
                   System.out.print(aCustomer);
                        
                        clearForm();
                  }

      }
      private void clearForm()
      {
            customerNameText.setText("");      
            customerAddressText.setText("");            
            customerPhoneText.setText("");
            customerNameText.requestFocus();
      }
      
       public void shutDown()
            { System.exit(0); }
}
0
Comment
Question by:jhelp_1
3 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 18833138
What's the problem you are facing/
0
 

Author Comment

by:jhelp_1
ID: 18833355
I want to display name, address and phone using print ln & a message box
JOptionPane.showMessageDialog(this, " Name " + customerName + "\n" + " Address " + customerAddress +
      "\n" + " Phone " + customerPhone);

this is how i am doing it now or
System.out.print(" Name " + customerNameText.getText() + "\n" +
                   " Address " + customerAddressText.getText() + "\n" + " Phone " + customerPhoneText.getText()); also works
but  
JoptionPane.showMessageDialog(System.out.print(" Name " + customerNameText.getText() + "\n" +
                   " Address " + customerAddressText.getText() + "\n" + " Phone " + customerPhoneText.getText()));
gives me an error      
                                           clearForm();
here iss the error
DisplayCustomer.java:103: 'void' type not allowed here
0
 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 500 total points
ID: 18834319
the method signature for joptionPane.showMessageDialog takes a component and a object..
you are passing neither of these..

the code needs to be changed as

JOptionPane.showMessageDialog(this, " Name " + customerName + "\n" + " Address " + customerAddress +
      "\n" + " Phone " + customerPhone);

not a system.out.println inside the method..
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
map related example 6 47
PHPStorm debugging issues 1 45
Best environment for cross platform web development? 5 45
How to determine if a string is a valid SHA value 7 31
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question