Halt123
asked on
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/co lumn
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(t his);
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.setHorizontalAlig nment(JTex tField.RIG HT);
Customer.addActionListener (this);
p2.add(Customer);
//creating a Paid Textfield
Paid= new JTextField(10);
Paid.addActionListener(thi s);
Paid.setHorizontalAlignmen t(JTextFie ld.RIGHT);
p2.add(Paid);
//creating a Material Cost Textfield
MaterialCost= new JTextField(10);
MaterialCost.setHorizontal Alignment( JTextField .RIGHT);
MaterialCost.addActionList ener(this) ;
p2.add(MaterialCost);
//creating a LabourCost Textfield
LabourCost= new JTextField(10);
LabourCost.setHorizontalAl ignment(JT extField.R IGHT);
LabourCost.addActionListen er(this);
p2.add(LabourCost);
TotalPayment= new JTextField(10);
TotalPayment.setHorizontal Alignment( JTextField .RIGHT);
TotalPayment.addActionList ener(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.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);
JLabel myLabel7;
myLabel7 = new JLabel("Total Payment",SwingConstants.RI GHT);
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(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();
try
{
Double.parseDouble(Materia lCost.getT ext());
}
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(LabourC ost.getTex t());
}
catch (NumberFormatException nfe)
{
if(LabourCost.getText().eq uals(""))
{
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(actionC ommand))
addFJob();
}
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("");
}
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
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/co
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
AddJob.addActionListener(t
p1.add(AddJob);
//Panel2 to hold text
//creating a ID Textfield
Id = new JTextField(10);
Id.setHorizontalAlignment(
Id.addActionListener(this)
p2.add(Id);
//creating a Customer Textfield
Customer= new JTextField(10);
Customer.setHorizontalAlig
Customer.addActionListener
p2.add(Customer);
//creating a Paid Textfield
Paid= new JTextField(10);
Paid.addActionListener(thi
Paid.setHorizontalAlignmen
p2.add(Paid);
//creating a Material Cost Textfield
MaterialCost= new JTextField(10);
MaterialCost.setHorizontal
MaterialCost.addActionList
p2.add(MaterialCost);
//creating a LabourCost Textfield
LabourCost= new JTextField(10);
LabourCost.setHorizontalAl
LabourCost.addActionListen
p2.add(LabourCost);
TotalPayment= new JTextField(10);
TotalPayment.setHorizontal
TotalPayment.addActionList
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.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);
JLabel myLabel7;
myLabel7 = new JLabel("Total Payment",SwingConstants.RI
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(J
setVisible(true);
public void actionPerformed(ActionEven
{
String text1 = Id.getText();
String text2 = Customer.getText();
String text = Paid.getText();
try
{
Double.parseDouble(Materia
}
catch (NumberFormatException nfe)
{
if(MaterialCost.getText().
{
System.out.println("ERROR:
}
else
{
System.out.println("ERROR:
}
}
try
{
Double.parseDouble(LabourC
}
catch (NumberFormatException nfe)
{
if(LabourCost.getText().eq
{
System.out.println("ERROR:
}
else
{
System.out.println("ERROR:
}
}
String actionCommand = e.getActionCommand();
if(e.getSource() instanceof JButton)
{
if("AddJob".equals(actionC
addFJob();
}
public void addFJob()
{
FixedRateJob job = new FixedRateJob(Id.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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
And add the code to actually display the text in your action ;listener
ASKER
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);
JTextArea displayArea;
displayArea = new JTextArea(15, 11);
displayArea.setText("");
displayArea.append("New Job:\n");
p4.add(displayArea);
Id = new JTextField(10);
Id.setHorizontalAlignment(
Id.addActionListener(this)
displayArea.append("ID: "+AddJob.getID()+"\n");
p2.add(Id);
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok ill take a look thank you
> JTextArea displayArea;
make that member variable so you can access it in your action listener
make that member variable so you can access it in your action listener
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
A JTable would seem a good way of doing this and would give you a better display
ASKER
Thank you for all your comments.
no worries :)
ASKER
Id = new JTextField(10);
Id.setHorizontalAlignment(
Id.addActionListener(this)
displayArea.setText("");
displayArea.append("New Job:\n");
displayArea.append("ID: "+newjob.getID()+"\n");
displayArea.append("Custom
p2.add(Id);
But im not sure what to call newJob..