linking GUI with program

Dear experts,

I have written a GUI application that has buttons/labels and text boxes on it. I have also written a program that is made to run in the comand prompt.This program uses several different classes to run and it allows the user to add info, find info and do other things aswell.

However i am unsure how I am meant to link the two programs together.

If you would like me to post the code that I have please let me know:)

Thank you in advance.
Halt123Asked:
Who is Participating?
 
CEHJCommented:
No.  You've only got buttons by the looks. The following:

>>
            if (e.getSource() instanceof JMenuItem)
                  if ("AddJob".equals(actionCommand))
                        addFJob();
>>

should be

            if (e.getSource() instanceof JButton)
                  if ("AddJob".equals(actionCommand))
                        addFJob();

Also, you clear the text from some of the fields before you create the Job. This will give you errors
0
 
CEHJCommented:
Possibly a good idea to post the code, but please post a link to it if possible
0
 
Halt123Author Commented:
How would I post a link to it?
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
CEHJCommented:
In public webspace of use http://pastebin.com

You basically need to have public set methods to set the fields from input
0
 
objectsCommented:
You can use the classes from your command line app in the same way you use any classes.
So just use them in your GUI as you need them.
0
 
Halt123Author Commented:
Ok i will give that a go and get back to you in a bit.

Thank you
0
 
Halt123Author Commented:
When I run the GUI with the same class in my program from the command line it brings up the error:

The error is
JobApplication.java:187: cannot find symbol
symbol  : constructor Job(javax.swing.JTextField,javax.swing.JTextField,javax.swing.JTextField,javax.swing.JTextField,javax.swing.JTextField)
location: class Job
   Job job = new  Job(Id, Customer, Paid, MaterialCost, LabourCost);

However with the program that runs from the command line it compiles are doesnt seem to have a problem.
0
 
objectsCommented:
does Job expects strings?
If so use:

Job job = new  Job(Id.getText(), Customer.getText(), Paid.getText(), MaterialCost.getText(), LabourCost.getText());
0
 
objectsCommented:
If you need an int you use something like:

int id = Integer.parseInt(Id.getText());

you'll find other conversions here:

http://www.objects.com.au/java/conversions.do
0
 
Halt123Author Commented:
Its compiled
thank you

is there a way to check that it is working in the GUI?
0
 
objectsCommented:
maybe add some debug println() to your Job class to see what values it gets
0
 
CEHJCommented:
If you don't already have a toString method in Job, provide an implementation that prints the fields:

System.out.println(job);

will show everything (where 'job' is the appropriate reference)
0
 
Halt123Author Commented:
Ah ok thank you

one last question:

Is there a way for the fields to empty once the user has clicked add job

the code for add job is:

public void actionPerformed(ActionEvent e)
{
                 String text1 = Id.getText();
       String text2 = Customer.getText();
       String text = Paid.getText();
                 Double.parseDouble(MaterialCost.getText());
        Double.parseDouble(LabourCost.getText());



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

}
      

public  void addFJob()
{

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

}
0
 
CEHJCommented:
You need to call setText("");

on your text fields
0
 
objectsCommented:
public  void addFJob()
{

     Job job = new  Job(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("");


}
0
 
Halt123Author Commented:
I've tried writing the code but it doesnt clear them.
0
 
Halt123Author Commented:
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 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

      //Place panels on frame
      getContentPane().add(p1, "South");
      getContentPane().add(p2, BorderLayout.EAST);
      getContentPane().add(p3, BorderLayout.CENTER);
      
      
               //panel P1 to hold the buttons
               JButton AddJob = new JButton("AddJob");
               AddJob.addActionListener(this);
              p1.add(AddJob);
           
                 JButton FindJob = new JButton("Find Job");
                 FindJob.addActionListener(this);
       p1.add(FindJob);

                 JButton Calulate = new JButton("Calulate");
                 AddJob.addActionListener(this);
       p1.add(Calulate);

            
          //Panel2 to hold text
         //creating a ID Textfield
         TextField Id;
         Id = new JTextField(10);
         Id.setHorizontalAlignment(JTextField.RIGHT);
         Id.addActionListener(this);
         p2.add(Id);

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

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

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



             //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);

                       //making the menu
                      JMenuBar bar = new JMenuBar();
                      setJMenuBar(bar);
                      JMenu app = new JMenu("Application");
                      bar.add(app);
                     JMenu helpMenu = new JMenu("Help");
                     bar.add(helpMenu);

                     JMenu actionappsubmenu = new JMenu("Action");
                      app.add(actionappsubmenu);
                      actionappsubmenu.addActionListener(this);


                       actionappsubmenu.add(new JMenuItem("AddJob"));
                       actionappsubmenu.add(new JMenuItem("FindJob"));
                       actionappsubmenu.add(new JMenuItem("Calulate"));

                       JMenuItem exitApp = new JMenuItem("Exit");
                       app.add(exitApp);
                       exitApp.addActionListener(this);
                       app.addSeparator();
                      //Adds short cuts
                       exitApp.setMnemonic(KeyEvent.VK_E);
                       KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_E,InputEvent.ALT_MASK);
                        exitApp.setAccelerator(key);


            

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


                 
   }
   public void actionPerformed(ActionEvent e)
      
 {
                 String text1 = Id.getText();
       String text2 = Customer.getText();
       String text = Paid.getText();
                 Double.parseDouble(MaterialCost.getText());
        Double.parseDouble(LabourCost.getText());



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

       
      if(e.getSource() instanceof JMenuItem)
      if("FindJob".equals(actionCommand))
      findFJob();

}
      

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("");

}


public  Job findFJob()
{
        return collection.findFJob(Id.getText());
}

      
public static void main(String args[])
 
 {
String lookAndFeel =
 "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
       try
      {
           UIManager.setLookAndFeel(lookAndFeel);
        }
           catch (Exception e)
       {
          e.printStackTrace();
        }

            new JobApplication();
  }
}

I've posted the code above if that helps... :)

thank you
0
 
objectsCommented:
delete the following lines from your constructor:

     JTextField Id;
     JTextField Customer;
     JTextField Paid;          
     JTextField MaterialCost;
     JTextField LabourCost;

They are shadowing the member variables
0
 
Halt123Author Commented:
I've changed what u suggested but its still not refreshing
0
 
Halt123Author Commented:
Do i need to add mouselisteners in to the program. If so how do I go about doing it?
0
 
Halt123Author Commented:
Thank you it works now

I will split the points between you and objects as you both deserve them :)
0
 
CEHJCommented:
OK - glad i could help
0
 
CEHJCommented:
:-)
0
 
objectsCommented:
no worries :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.