?
Solved

linking GUI with program

Posted on 2006-04-01
24
Medium Priority
?
188 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:Halt123
  • 10
  • 7
  • 7
24 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16351849
Possibly a good idea to post the code, but please post a link to it if possible
0
 

Author Comment

by:Halt123
ID: 16351851
How would I post a link to it?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16351863
In public webspace of use http://pastebin.com

You basically need to have public set methods to set the fields from input
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 92

Expert Comment

by:objects
ID: 16351925
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
 

Author Comment

by:Halt123
ID: 16351934
Ok i will give that a go and get back to you in a bit.

Thank you
0
 

Author Comment

by:Halt123
ID: 16351979
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
 
LVL 92

Assisted Solution

by:objects
objects earned 800 total points
ID: 16351983
does Job expects strings?
If so use:

Job job = new  Job(Id.getText(), Customer.getText(), Paid.getText(), MaterialCost.getText(), LabourCost.getText());
0
 
LVL 92

Expert Comment

by:objects
ID: 16351989
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
 

Author Comment

by:Halt123
ID: 16352016
Its compiled
thank you

is there a way to check that it is working in the GUI?
0
 
LVL 92

Expert Comment

by:objects
ID: 16352049
maybe add some debug println() to your Job class to see what values it gets
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16352063
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
 

Author Comment

by:Halt123
ID: 16352106
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16352124
You need to call setText("");

on your text fields
0
 
LVL 92

Expert Comment

by:objects
ID: 16352132
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
 

Author Comment

by:Halt123
ID: 16352332
I've tried writing the code but it doesnt clear them.
0
 

Author Comment

by:Halt123
ID: 16352416
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
 
LVL 92

Expert Comment

by:objects
ID: 16352620
delete the following lines from your constructor:

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

They are shadowing the member variables
0
 

Author Comment

by:Halt123
ID: 16354278
I've changed what u suggested but its still not refreshing
0
 

Author Comment

by:Halt123
ID: 16355103
Do i need to add mouselisteners in to the program. If so how do I go about doing it?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1200 total points
ID: 16355347
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
 

Author Comment

by:Halt123
ID: 16355356
Thank you it works now

I will split the points between you and objects as you both deserve them :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16355364
OK - glad i could help
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16355380
:-)
0
 
LVL 92

Expert Comment

by:objects
ID: 16356212
no worries :)
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month15 days, 16 hours left to enroll

850 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