Solved

how do I display customer info

Posted on 2007-04-01
3
411 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SHA2 certs for IIS AND Java? 2 136
servlet and mdb, jms error 1 83
Cisco ASA: Java web start no go, asdm launcher no go 3 88
Java ArrayList and if statement 2 55
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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.
Suggested Courses

710 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