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.
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.
Possibly a good idea to post the code, but please post a link to it if possible
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 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.
So just use them in your GUI as you need them.
ASKER
Ok i will give that a go and get back to you in a bit.
Thank you
Thank you
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.swi ng.JTextFi eld,javax. swing.JTex tField,jav ax.swing.J TextField, javax.swin g.JTextFie ld)
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.
The error is
JobApplication.java:187: cannot find symbol
symbol : constructor Job(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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you need an int you use something like:
int id = Integer.parseInt(Id.getTex t());
you'll find other conversions here:
http://www.objects.com.au/java/conversions.do
int id = Integer.parseInt(Id.getTex
you'll find other conversions here:
http://www.objects.com.au/java/conversions.do
ASKER
Its compiled
thank you
is there a way to check that it is working in the GUI?
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)
System.out.println(job);
will show everything (where 'job' is the appropriate reference)
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(ActionEven t e)
{
String text1 = Id.getText();
String text2 = Customer.getText();
String text = Paid.getText();
Double.parseDouble(Materia lCost.getT ext());
Double.parseDouble(LabourC ost.getTex t());
String actionCommand = e.getActionCommand();
if(e.getSource() instanceof JMenuItem)
if("AddJob".equals(actionC ommand))
addFJob();
}
public void addFJob()
{
Job job = new Job(Id.getText(), Customer.getText(), Paid.getText(), Double.parseDouble(Materia lCost.getT ext()), Double.parseDouble(LabourC ost.getTex t()));
collection.addFJob(job);
}
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(ActionEven
{
String text1 = Id.getText();
String text2 = Customer.getText();
String text = Paid.getText();
Double.parseDouble(Materia
Double.parseDouble(LabourC
String actionCommand = e.getActionCommand();
if(e.getSource() instanceof JMenuItem)
if("AddJob".equals(actionC
addFJob();
}
public void addFJob()
{
Job job = new Job(Id.getText(), Customer.getText(), Paid.getText(), Double.parseDouble(Materia
collection.addFJob(job);
}
You need to call setText("");
on your text fields
on your text fields
public void addFJob()
{
Job job = new Job(Id.getText(), Customer.getText(), Paid.getText(), Double.parseDouble(Materia lCost.getT ext()), Double.parseDouble(LabourC ost.getTex t()));
collection.addFJob(job);
Id.setText("");
Customer.setText("");
Paid.setText("");
MaterialCost.setText("");
LabourCost.setText("");
}
{
Job job = new Job(Id.getText(), Customer.getText(), Paid.getText(), Double.parseDouble(Materia
collection.addFJob(job);
Id.setText("");
Customer.setText("");
Paid.setText("");
MaterialCost.setText("");
LabourCost.setText("");
}
ASKER
I've tried writing the code but it doesnt clear them.
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/co lumn
//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(t his);
p1.add(AddJob);
JButton FindJob = new JButton("Find Job");
FindJob.addActionListener( this);
p1.add(FindJob);
JButton Calulate = new JButton("Calulate");
AddJob.addActionListener(t his);
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.setHorizontalAlig nment(JTex tField.RIG HT);
Customer.addActionListener (this);
p2.add(Customer);
//creating a Paid Textfield
JTextField Paid;
Paid= new JTextField(10);
Paid.addActionListener(thi s);
Paid.setHorizontalAlignmen t(JTextFie ld.RIGHT);
p2.add(Paid);
//creating a Material Cost Textfield
JTextField MaterialCost;
MaterialCost= new JTextField(10);
MaterialCost.setHorizontal Alignment( JTextField .RIGHT);
MaterialCost.addActionList ener(this) ;
p2.add(MaterialCost);
//creating a LabourCost Textfield
JTextField LabourCost;
LabourCost= new JTextField(10);
LabourCost.setHorizontalAl ignment(JT extField.R IGHT);
LabourCost.addActionListen er(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.R IGHT);
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.addAction Listener(t his);
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(KeyEve nt.VK_E);
KeyStroke key = KeyStroke.getKeyStroke(Key Event.VK_E ,InputEven t.ALT_MASK );
exitApp.setAccelerator(key );
//making the frame
setTitle("Job Application");
setBounds(0,0,300,300);
setDefaultCloseOperation(J Frame.EXIT _ON_CLOSE) ;
setVisible(true);
}
public void actionPerformed(ActionEven t e)
{
String text1 = Id.getText();
String text2 = Customer.getText();
String text = Paid.getText();
Double.parseDouble(Materia lCost.getT ext());
Double.parseDouble(LabourC ost.getTex t());
String actionCommand = e.getActionCommand();
if(e.getSource() instanceof JMenuItem)
if("AddJob".equals(actionC ommand))
addFJob();
if(e.getSource() instanceof JMenuItem)
if("FindJob".equals(action Command))
findFJob();
}
public void addFJob()
{
FixedRateJob job = new FixedRateJob(Id.getText(), Customer.getText(), Paid.getText(), Double.parseDouble(Materia lCost.getT ext()), Double.parseDouble(LabourC ost.getTex t()));
collection.addFJob(job);
Id.setText("");
Customer.setText("");
Paid.setText("");
MaterialCost.setText("");
LabourCost.setText("");
}
public Job findFJob()
{
return collection.findFJob(Id.get Text());
}
public static void main(String args[])
{
String lookAndFeel =
"com.sun.java.swing.plaf.w indows.Win dowsLookAn dFeel";
try
{
UIManager.setLookAndFeel(l ookAndFeel );
}
catch (Exception e)
{
e.printStackTrace();
}
new JobApplication();
}
}
I've posted the code above if that helps... :)
thank you
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/co
//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(t
p1.add(AddJob);
JButton FindJob = new JButton("Find Job");
FindJob.addActionListener(
p1.add(FindJob);
JButton Calulate = new JButton("Calulate");
AddJob.addActionListener(t
p1.add(Calulate);
//Panel2 to hold text
//creating a ID Textfield
TextField Id;
Id = new JTextField(10);
Id.setHorizontalAlignment(
Id.addActionListener(this)
p2.add(Id);
//creating a Customer Textfield
JTextField Customer;
Customer= new JTextField(10);
Customer.setHorizontalAlig
Customer.addActionListener
p2.add(Customer);
//creating a Paid Textfield
JTextField Paid;
Paid= new JTextField(10);
Paid.addActionListener(thi
Paid.setHorizontalAlignmen
p2.add(Paid);
//creating a Material Cost Textfield
JTextField MaterialCost;
MaterialCost= new JTextField(10);
MaterialCost.setHorizontal
MaterialCost.addActionList
p2.add(MaterialCost);
//creating a LabourCost Textfield
JTextField LabourCost;
LabourCost= new JTextField(10);
LabourCost.setHorizontalAl
LabourCost.addActionListen
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.R
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
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.addAction
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(
app.addSeparator();
//Adds short cuts
exitApp.setMnemonic(KeyEve
KeyStroke key = KeyStroke.getKeyStroke(Key
exitApp.setAccelerator(key
//making the frame
setTitle("Job Application");
setBounds(0,0,300,300);
setDefaultCloseOperation(J
setVisible(true);
}
public void actionPerformed(ActionEven
{
String text1 = Id.getText();
String text2 = Customer.getText();
String text = Paid.getText();
Double.parseDouble(Materia
Double.parseDouble(LabourC
String actionCommand = e.getActionCommand();
if(e.getSource() instanceof JMenuItem)
if("AddJob".equals(actionC
addFJob();
if(e.getSource() instanceof JMenuItem)
if("FindJob".equals(action
findFJob();
}
public void addFJob()
{
FixedRateJob job = new FixedRateJob(Id.getText(),
collection.addFJob(job);
Id.setText("");
Customer.setText("");
Paid.setText("");
MaterialCost.setText("");
LabourCost.setText("");
}
public Job findFJob()
{
return collection.findFJob(Id.get
}
public static void main(String args[])
{
String lookAndFeel =
"com.sun.java.swing.plaf.w
try
{
UIManager.setLookAndFeel(l
}
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
JTextField Id;
JTextField Customer;
JTextField Paid;
JTextField MaterialCost;
JTextField LabourCost;
They are shadowing the member variables
ASKER
I've changed what u suggested but its still not refreshing
ASKER
Do i need to add mouselisteners in to the program. If so how do I go about doing it?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you it works now
I will split the points between you and objects as you both deserve them :)
I will split the points between you and objects as you both deserve them :)
OK - glad i could help
:-)
no worries :)