Link to home
Start Free TrialLog in
Avatar of Halt123
Halt123

asked on

Enter TextField info into TextBox

Dear experts,

I would like my program to when the user clicks on AddJob the information is deisplayed in a TextBox.

My code so far is:

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

public class JobApplication extends JFrame implements ActionListener
{
  //declaring the variables
  private JTextField Id;
  private  JTextField Customer;
  private  JTextField Paid;
  private  JTextField MaterialCost;
  private  JTextField NumberHourWorked;
  private  JTextField LabourCost;
  private  JTextField RatePerHour;
  private  JTextField TotalPayment;
  private static JobCollection collection = new JobCollection(100);

public JobApplication()
     {
   
              //panels
              JPanel p1 = new JPanel(false);
              JPanel p2 = new JPanel(false);
              p2.setLayout(new GridLayout(10,1));
             JPanel p3 = new JPanel(false);
             p3.setLayout(new GridLayout(10,1));//row/column
             JPanel p4 = new JPanel(false);

              //Place panels on frame
              getContentPane().add(p1, "South");
              getContentPane().add(p2, BorderLayout.CENTER);
              getContentPane().add(p3, BorderLayout.WEST);
              getContentPane().add(p4, "East");
     
     
               //panel P1 to hold the buttons
               JButton AddJob = new JButton("AddJob");
      AddJob.setForeground(Color.blue);
      AddJob.addActionListener(this);
                p1.add(AddJob);
           
                     
             //Panel2 to hold text
             //creating a ID Textfield
       
        Id = new JTextField(10);
        Id.setHorizontalAlignment(JTextField.RIGHT);
        Id.addActionListener(this);
        p2.add(Id);

       //creating a Customer Textfield
       Customer= new JTextField(10);
       Customer.setHorizontalAlignment(JTextField.RIGHT);
       Customer.addActionListener(this);
       p2.add(Customer);

      //creating a Paid Textfield
      Paid= new JTextField(10);
      Paid.addActionListener(this);
      Paid.setHorizontalAlignment(JTextField.RIGHT);
      p2.add(Paid);
         
     //creating a Material Cost Textfield
     MaterialCost= new JTextField(10);
     MaterialCost.setHorizontalAlignment(JTextField.RIGHT);
     MaterialCost.addActionListener(this);
     p2.add(MaterialCost);

     //creating a LabourCost Textfield
     LabourCost= new JTextField(10);
     LabourCost.setHorizontalAlignment(JTextField.RIGHT);
     LabourCost.addActionListener(this);
     p2.add(LabourCost);

     TotalPayment= new JTextField(10);
     TotalPayment.setHorizontalAlignment(JTextField.RIGHT);
     TotalPayment.addActionListener(this);
     p2.add(TotalPayment);

            //Panel p3 to hold labels
           //creating the ID label
           JLabel myLabel;
           myLabel =new JLabel("Input ID", SwingConstants.RIGHT);
           p3.add(myLabel);

          //creating a Customer label
          JLabel myLabel1;
          myLabel1 = new JLabel("Input Customer",SwingConstants.RIGHT);
          p3.add(myLabel1);

         //creating a Paid label
         JLabel myLabel2;
         myLabel2 = new JLabel("Input Paid",SwingConstants.RIGHT);
         p3.add(myLabel2);

         //creating a Material Cost label
         JLabel myLabel3;
         myLabel3 = new JLabel("Input Material Cost",SwingConstants.RIGHT);
         p3.add(myLabel3);
         
          //creating a LabourCost label
         JLabel myLabel6;
         myLabel6 = new JLabel("Input LabourCost",SwingConstants.RIGHT);
          p3.add(myLabel6);

         JLabel myLabel7;
         myLabel7 = new JLabel("Total Payment",SwingConstants.RIGHT);
          p3.add(myLabel7);

               //TextArea created
      JTextArea displayArea;
      displayArea = new JTextArea(15, 11);
      p4.add(displayArea);

              //making the frame
               setTitle("Job Application");
               setBounds(0,0,700,500);
               setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               setVisible(true);

public void actionPerformed(ActionEvent e)
{
      String text1 = Id.getText();
     String text2 = Customer.getText();
      String text = Paid.getText();
 
            
                  try
                  {
                    Double.parseDouble(MaterialCost.getText());
                  }
                       catch (NumberFormatException nfe)
                       {
                                 if(MaterialCost.getText().equals(""))
            {
                 System.out.println("ERROR: Please enter an Material Cost ");
            }
                      else
                      {
                             System.out.println("ERROR: Material cost is not an integer");
                      }
                        }


                try
                {
                               Double.parseDouble(LabourCost.getText());
                 }
            catch (NumberFormatException nfe)
            {
             if(LabourCost.getText().equals(""))
             {
             System.out.println("ERROR: Please enter an Labour Cost ");
             }
                else
             {
             System.out.println("ERROR: Labour Cost is not an integer");
             }
            }


      String actionCommand = e.getActionCommand();
     if(e.getSource() instanceof JButton)
    {
         if("AddJob".equals(actionCommand))
         addFJob();
     }

public  void addFJob()
{

        FixedRateJob job = new  FixedRateJob(Id.getText(), Customer.getText(), Paid.getText(),  Double.parseDouble(MaterialCost.getText()), Double.parseDouble(LabourCost.getText()));
        collection.addFJob(job);
        Id.setText("");
        Customer.setText("");
        Paid.setText("");
        MaterialCost.setText("");
        LabourCost.setText("");

}

At the moment the program clear the text field once the Addbutton has been pressed. I still want to keep inthis function but have the information that has been entered desplayed in the textArea

thank you
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Halt123
Halt123

ASKER

I've started to write the code here is this the right place?

Id = new JTextField(10);
        Id.setHorizontalAlignment(JTextField.RIGHT);
        Id.addActionListener(this);
      displayArea.setText("");
      displayArea.append("New Job:\n");
      displayArea.append("ID: "+newjob.getID()+"\n");
      displayArea.append("Customer: "+newjob.getCustomer()+"\n");
                p2.add(Id);

But im not sure what to call newJob..
you would create and add your text area in your constructor making it a member variable. The same as u have done for the other fields.

And add the code to actually display the text in your action ;listener
Avatar of Halt123

ASKER

like this:

                JTextArea displayArea;
      displayArea = new JTextArea(15, 11);
      displayArea.setText("");
      displayArea.append("New Job:\n");
               p4.add(displayArea);

              Id = new JTextField(10);
              Id.setHorizontalAlignment(JTextField.RIGHT);
              Id.addActionListener(this);
              displayArea.append("ID: "+AddJob.getID()+"\n");
              p2.add(Id);

      
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Halt123

ASKER

ok ill take a look thank you
>    JTextArea displayArea;

make that member variable so you can access it in your action listener
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
A JTable would seem a good way of doing this and would give you a better display
Avatar of Halt123

ASKER

Thank you for all your comments.
no worries :)