Link to home
Start Free TrialLog in
Avatar of Halt123
Halt123

asked on

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.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Possibly a good idea to post the code, but please post a link to it if possible
Avatar of Halt123
Halt123

ASKER

How would I post a link to it?
In public webspace of use http://pastebin.com

You basically need to have public set methods to set the fields from input
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.
Avatar of Halt123

ASKER

Ok i will give that a go and get back to you in a bit.

Thank you
Avatar of Halt123

ASKER

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.
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
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
Avatar of Halt123

ASKER

Its compiled
thank you

is there a way to check that it is working in the GUI?
maybe add some debug println() to your Job class to see what values it gets
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)
Avatar of Halt123

ASKER

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

}
You need to call setText("");

on your text fields
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("");


}
Avatar of Halt123

ASKER

I've tried writing the code but it doesnt clear them.
Avatar of Halt123

ASKER

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
delete the following lines from your constructor:

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

They are shadowing the member variables
Avatar of Halt123

ASKER

I've changed what u suggested but its still not refreshing
Avatar of Halt123

ASKER

Do i need to add mouselisteners in to the program. If so how do I go about doing it?
ASKER CERTIFIED 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

Thank you it works now

I will split the points between you and objects as you both deserve them :)
OK - glad i could help
:-)
no worries :)