[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 391
  • Last Modified:

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
0
Halt123
Asked:
Halt123
3 Solutions
 
objectsCommented:
Ct\reate a new JTextArea to display the new job and ad that to your gui where you want it to appear
then in you actionlistener after you add the jab append the details to the new text area

// first clear it
ta.setText("");
ta.append("New Job:\n");
ta.append("ID: "+newjob.getID()+"\n");
ta.append("Customer: "+newjob.getCustomer()+"\n");
// similiar for other fields you want to append
0
 
Halt123Author Commented:
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..
0
 
objectsCommented:
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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Halt123Author Commented:
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);

      
0
 
Manikandan ThiagarajanSenior consultantCommented:
0
 
Halt123Author Commented:
ok ill take a look thank you
0
 
objectsCommented:
>    JTextArea displayArea;

make that member variable so you can access it in your action listener
0
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
0
 
CEHJCommented:
A JTable would seem a good way of doing this and would give you a better display
0
 
Halt123Author Commented:
Thank you for all your comments.
0
 
objectsCommented:
no worries :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now