aspose file tools*
The moose likes Beginning Java and the fly likes creating a new object from a gui input Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "creating a new object from a gui input" Watch "creating a new object from a gui input" New topic
Author

creating a new object from a gui input

Al Dodd
Greenhorn

Joined: Feb 08, 2008
Posts: 7
Do you know how to create a object with a reference inputed from a textfield in a GUI ?

so you would have some code like :

shiftDay textField1.text = new shiftDay();

where shiftDay is a class and the textField1 refers to a text field on GUI? Obviously this doesn't work.


marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Welcome to JavaRanch!

I don't think it's possible.

I don't know why you are asking this, but my guess is that you are writing code in which you don't know how many variables there will be at runtime, so you are looking for a way to give them names at runtime. In a situation like that, you can use an array to store your references...

ShiftDay[] shiftDay = new ShiftDay[x];

...where x is determined at runtime.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Al Dodd
Greenhorn

Joined: Feb 08, 2008
Posts: 7
Thanks for the reply.

This is actually for my degree project and I am stuck. This is part of an app which stores a shift rota. So Shiftday object will have to be created, when all the staff positions are set, it is stored in a vector, If i want to reference it then to retrieve it and edit it how do I know what the object reference would be if I can't set it? Hiw would I create new shiftdays?

Thanks for the reply though. I think I may be able to change the design to do it the way you have suggested.

Al


marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Al Dodd:
...it is stored in a vector, If i want to reference it then to retrieve it and edit it how do I know what the object reference would be if I can't set it? ...

One option might be to assign a unique key (e.g., a date?) to each instance, then store the instances in a Map instead of a Vector. Then you could retrieve specific instances by their keys.

There are a lot of considerations in choosing a collection type. What made you decide on a Vector?
[ May 10, 2008: Message edited by: marc weber ]
Al Dodd
Greenhorn

Joined: Feb 08, 2008
Posts: 7
I suppose the truthful answer is laziness as I had used one in the course work. a Map would seem like a good idea.
I think I'll research it further.

Thanks very Much Marc.
Al Dodd
Greenhorn

Joined: Feb 08, 2008
Posts: 7
Okay tried using a LinkedHashMap

This is basically a program used to display a shift rota for a dispatch room.

This is the class for the shiftDay

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.LinkedHashMap;


public class shiftDay extends JFrame {

public shiftDay (Integer tempShiftDate) {

shiftDate = tempShiftDate;
}

public Integer shiftDate;
private String m1rOperator;
private String m1aOperator;
private String m2rOperator;
private String m2aOperator;
private String m1rShift;
private String m1aShift;
private String m2rShift;
private String m2aShift;
private String m1rRefTime;
private String m1aRefTime;
private String m2rRefTime;
private String m2aRefTime;

private String f1rOperator;
private String f1aOperator;
private String f2rOperator;
private String f2aOperator;
private String f1rShift;
private String f1aShift;
private String f2rShift;
private String f2aShift;
private String f1rRefTime;
private String f1aRefTime;
private String f2rRefTime;
private String f2aRefTime;

public void setShiftDate(Integer tempShiftDate){

shiftDate = tempShiftDate;
}
public Integer getShiftDate(Integer tempShiftDate){

return shiftDate;
}
public void setM1rOperator(String tempM1rOperator){

m1rOperator = tempM1rOperator;
}

public void setM1aOperator (String tempM1aOperator){

m1aOperator = tempM1aOperator;
}

public void setM2rOperator (String tempM2rOperator){

m2rOperator = tempM2rOperator;
}

public void setM2aOperator (String tempM2aOperator){

m2aOperator = tempM2aOperator;
}


public void setM1rShift(String tempM1rShift){

m1rShift = tempM1rShift;
}

public void setM1aShift(String tempM1aShift){

m1aShift = tempM1aShift;
}

public void setM2rShift(String tempM2rShift){

m2rShift = tempM2rShift;
}

public void setM2aShift(String tempM2aShift){

m2aShift = tempM2aShift;
}

public void setM1rRefTime(String tempM1rRefTime){

m1rRefTime = tempM1rRefTime;
}

public void setM1aRefTime(String tempM1aRefTime){

m1aRefTime = tempM1aRefTime;
}

public void setM2rRefTime(String tempM2rRefTime){

m2rRefTime = tempM2rRefTime;
}

public void setM2aRefTime(String tempM2aRefTime){

m2aRefTime = tempM2aRefTime;
}

public Integer getShiftDate(){

return shiftDate;
}


public String getM1rOperator(){

return m1rOperator;
}

public String getM1aOperator (){

return m1aOperator;
}

public String getM2rOperator (){

return m2rOperator;
}

public String getM2aOperator (){

return m2aOperator;
}


public String getM1rShift(){

return m1rShift;
}

public String getM1aShift(){

return m1aShift;
}


public String getM2rShift(){

return m2rShift;
}

public String getM2aShift(){

return m2aShift;
}

public String getM1rRefTime(){

return m1rRefTime;
}

public String getM1aRefTime(){

return m1aRefTime;
}

public String getM2rRefTime(){

return m2rRefTime;
}

public String getM2aRefTime(){

return m2aRefTime;
}


public void setF1rOperator(String tempf1rOperator){

f1rOperator = tempf1rOperator;
}

public void setF1aOperator (String tempf1aOperator){

f1aOperator = tempf1aOperator;
}

public void setF2rOperator (String tempf2rOperator){

f2rOperator = tempf2rOperator;
}

public void setF2aOperator (String tempf2aOperator){

f2aOperator = tempf2aOperator;
}


public void setF1rShift(String tempf1rShift){

f1rShift = tempf1rShift;
}

public void setF1aShift(String tempf1aShift){

f1aShift = tempf1aShift;
}

public void setF2rShift(String tempf2rShift){

f2rShift = tempf2rShift;
}

public void setF2aShift(String tempf2aShift){

f2aShift = tempf2aShift;
}

public void setF1rRefTime(String tempf1rRefTime){

f1rRefTime = tempf1rRefTime;
}

public void setF1aRefTime(String tempf1aRefTime){

f1aRefTime = tempf1aRefTime;
}

public void setF2rRefTime(String tempf2rRefTime){

f2rRefTime = tempf2rRefTime;
}

public void setF2aRefTime(String tempf2aRefTime){

f2aRefTime = tempf2aRefTime;
}




public String getF1rOperator(){

return f1rOperator;
}

public String getF1aOperator (){

return f1aOperator;
}

public String getF2rOperator (){

return f2rOperator;
}

public String getF2aOperator (){

return f2aOperator;
}


public String getF1rShift(){

return f1rShift;
}

public String getF1aShift(){

return f1aShift;
}


public String getF2rShift(){

return f2rShift;
}

public String getF2aShift(){

return f2aShift;
}

public String getF1rRefTime(){

return f1rRefTime;
}

public String getF1aRefTime(){

return f1aRefTime;
}

public String getF2rRefTime(){

return f2rRefTime;
}

public String getF2aRefTime(){

return f2aRefTime;
}

public void addComponent(Container container,Component c,int x,int y,int width,int height)
{
c.setBounds(x,y,width,height);
container.add(c);
}

public void displayDay(){

JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch (Exception ex)
{
System.out.println("Failed loading L&F: ");
System.out.println(ex);
}
JFrame frame = new JFrame("Shift Rota");
JLabel date = new JLabel ("Date: " + this.getShiftDate());
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JLabel jLabel4 = new JLabel();
JLabel jLabel9 = new JLabel();
JLabel jLabel10 = new JLabel();
JLabel jLabel11 = new JLabel();
JLabel jLabel13 = new JLabel();
JLabel jLabel14 = new JLabel();
JLabel jLabel15 = new JLabel();
JTextField jTextField1 = new JTextField();
JTextField jTextField2 = new JTextField();
JTextField jTextField3 = new JTextField();
JTextField jTextField4 = new JTextField();
JTextField jTextField6 = new JTextField();
JTextField jTextField7 = new JTextField();
JTextField jTextField8 = new JTextField();
JTextField jTextField9 = new JTextField();
JTextField jTextField10 = new JTextField();
JTextField jTextField11 = new JTextField();
JTextField jTextField12 = new JTextField();
JTextField jTextField13 = new JTextField();
JTextField jTextField14 = new JTextField();
JTextField jTextField15 = new JTextField();
JTextField jTextField16 = new JTextField();
JTextField jTextField17 = new JTextField();
JTextField jTextField18 = new JTextField();
JTextField jTextField19 = new JTextField();
JTextField jTextField20 = new JTextField();
JTextField jTextField21 = new JTextField();
JTextField jTextField22 = new JTextField ();
JTextField jTextField23 = new JTextField();
JTextField jTextField24 = new JTextField();
JTextField jTextField25 = new JTextField();
JPanel contentPane = new JPanel();
jLabel1.setText("M1R");
//
// jLabel2
//
jLabel2.setText("M1A");
//
// jLabel3
//
jLabel3.setText("M2R");
//
// jLabel4
//
jLabel4.setText("M2A");
//
// jLabel9
//
jLabel9.setText("Shift");
//
// jLabel10
//
jLabel10.setText("Refs Time");
//
// jLabel11
//
jLabel11.setText("F1R");
//
// jLabel13
//
jLabel13.setText("F1A");
//
// jLabel14
//
jLabel14.setText("F2R");
//
// jLabel15
//
jLabel15.setText("F2A");
//
// jTextField1
//
jTextField1.setText(this.getM1rOperator());
jTextField2.setText(this.getM1aOperator());
jTextField3.setText(this.getM2rOperator());
jTextField4.setText(this.getM2aOperator());
jTextField6.setText(this.getM1rShift());
jTextField7.setText(this.getM1aShift());


jTextField8.setText(this.getM2rShift());
jTextField9.setText(this.getM2aShift());
jTextField10.setText(this.getM1rRefTime());
jTextField11.setText(this.getM1aRefTime());
jTextField12.setText(this.getM2rRefTime());
jTextField13.setText(this.getM2aRefTime());
jTextField14.setText(this.getF1rOperator());
jTextField15.setText(this.getF1rShift());
jTextField16.setText(this.getF1rRefTime());
jTextField17.setText(this.getF1aOperator());
jTextField18.setText(this.getF1aShift());
jTextField19.setText(this.getF1aRefTime());
jTextField20.setText(this.getF2rOperator());
jTextField21.setText(this.getF2rShift());
jTextField22.setText(this.getF2rRefTime());
jTextField23.setText(this.getF2aOperator());
jTextField24.setText(this.getF2aShift());
jTextField25.setText(this.getF2aRefTime());

// set TextFields uneditable


jTextField1.setEditable (false);
jTextField2.setEditable (false);
jTextField3.setEditable (false);
jTextField4.setEditable (false);
jTextField6.setEditable (false);
jTextField7.setEditable (false);
jTextField8.setEditable (false);
jTextField9.setEditable (false);
jTextField10.setEditable (false);
jTextField11.setEditable (false);
jTextField12.setEditable (false);
jTextField13.setEditable (false);
jTextField14.setEditable (false);
jTextField15.setEditable (false);
jTextField16.setEditable (false);
jTextField17.setEditable (false);
jTextField18.setEditable (false);
jTextField19.setEditable (false);
jTextField20.setEditable (false);
jTextField21.setEditable (false);
jTextField22.setEditable (false);
jTextField23.setEditable (false);
jTextField24.setEditable (false);
jTextField25.setEditable (false);


contentPane.setLayout(null);
addComponent(contentPane, jLabel1, 8,14,60,18);
addComponent(contentPane, jLabel2, 8,69,60,18);
addComponent(contentPane, jLabel3, 8,120,60,18);
addComponent(contentPane, jLabel4, 8,173,60,18);
addComponent(contentPane, jLabel9, 150,14,60,18);
addComponent(contentPane, jLabel10, 284,14,60,18);
addComponent(contentPane, jLabel11, 9,227,60,18);
addComponent(contentPane, jLabel13, 9,289,60,18);
addComponent(contentPane, jLabel14, 8,341,60,18);
addComponent(contentPane, jLabel15, 10,393,60,18);
addComponent(contentPane, jTextField1, 7,39,100,22);
addComponent(contentPane, jTextField2, 5,91,100,22);
addComponent(contentPane, jTextField3, 5,144,100,22);
addComponent(contentPane, jTextField4, 7,195,100,22);
addComponent(contentPane, jTextField6, 150,39,100,22);
addComponent(contentPane, jTextField7, 149,90,100,22);
addComponent(contentPane, jTextField8, 148,144,100,22);
addComponent(contentPane, jTextField9, 147,196,100,22);
addComponent(contentPane, jTextField10, 283,41,100,22);
addComponent(contentPane, jTextField11, 281,90,100,22);
addComponent(contentPane, jTextField12, 284,144,100,22);
addComponent(contentPane, jTextField13, 282,194,100,22);
addComponent(contentPane, jTextField14, 7,251,100,22);
addComponent(contentPane, jTextField15, 149,251,100,22);
addComponent(contentPane, jTextField16, 282,249,100,22);
addComponent(contentPane, jTextField17, 5,311,100,22);
addComponent(contentPane, jTextField18, 148,311,100,22);
addComponent(contentPane, jTextField19, 282,309,100,22);
addComponent(contentPane, jTextField20, 7,365,100,22);
addComponent(contentPane, jTextField21, 148,367,100,22);
addComponent(contentPane, jTextField22, 285,366,100,22);
addComponent(contentPane, jTextField23, 9,421,100,22);
addComponent(contentPane, jTextField24, 147,422,100,22);
addComponent(contentPane, jTextField25, 283,420,100,22);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(contentPane);
frame.pack();
frame.setSize(800,600);
frame.setVisible(true);
}

}



And then this is class used to store the shiftDays in

import java.util.LinkedHashMap;
import java.util.Map;

public class ShiftRota1{

public ShiftRota1 () {
Map <Integer, shiftDay>shiftRota1 = new LinkedHashMap<Integer, shiftDay>();
}

public void removeShiftDay ( Integer key) {

shiftRota1.remove (key); // uses Map method to remove the shift day

}

public void addDay (Integer key, shiftDay value) {

shiftRota1.put( key, value); // uses Map method to add the shift day
}

public shiftDay getDay ( Integer key) {
return shiftRota1.get(key);
// uses Map method to find shiftDay
}


}

And then this is the class I used to test it.



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.LinkedHashMap;
import java.util.Map;

class ThirdInstance
{
public static void main ( String [] args)
{
shiftDay Shiftday1 = new shiftDay (200308);

ShiftRota1 shiftRotaA = new ShiftRota1();
System.out.println(Shiftday1.getShiftDate());


Shiftday1.setM1rOperator ("20185");
Shiftday1.setM1aOperator ("22974");
Shiftday1.setM2aOperator ("24185");
Shiftday1.setM2rOperator ("60093");

Shiftday1.setF1rOperator ("21167");
Shiftday1.setF1aOperator ("29994");
Shiftday1.setF2aOperator ("28885");
Shiftday1.setF2rOperator ("60993");

Shiftday1.setM1rShift ("0700-1700");
Shiftday1.setM1aShift ("0800-1800");
Shiftday1.setM2aShift ("0700-1700");
Shiftday1.setM2rShift ("0700-1700");

Shiftday1.setF1rShift ("0700-1700");
Shiftday1.setF1aShift ("0700-1700");
Shiftday1.setF2aShift ("0800-1800");
Shiftday1.setF2rShift ("0700-1700");

Shiftday1.setM1rRefTime ("1000");
Shiftday1.setM1aRefTime ("1100");
Shiftday1.setM2aRefTime ("1200");
Shiftday1.setM2rRefTime ("1300");

Shiftday1.setF1rRefTime ("1200");
Shiftday1.setF1aRefTime ("1300");
Shiftday1.setF2aRefTime ("1400");
Shiftday1.setF2rRefTime ("1200");



System.out.println("M1R is " + Shiftday1.getM1rOperator()+ " " + Shiftday1.getM1rShift() + " " + Shiftday1.getM1rRefTime());
System.out.println("M2R is " + Shiftday1.getM2rOperator()+ " " + Shiftday1.getM2rShift() + " " + Shiftday1.getM2rRefTime());
System.out.println("M1a is " + Shiftday1.getM1aOperator()+ " " + Shiftday1.getM1aShift() + " " + Shiftday1.getM1aRefTime());
System.out.println("M2a is " + Shiftday1.getM2aOperator()+ " " + Shiftday1.getM2rShift() + " " + Shiftday1.getM2rRefTime());

System.out.println("f1R is " + Shiftday1.getF1rOperator()+ " " + Shiftday1.getF1rShift() + " " + Shiftday1.getF1rRefTime());
System.out.println("f2R is " + Shiftday1.getF2rOperator()+ " " + Shiftday1.getF2rShift() + " " + Shiftday1.getF2rRefTime());
System.out.println("f1a is " + Shiftday1.getF1aOperator()+ " " + Shiftday1.getF1aShift() + " " + Shiftday1.getF1aRefTime());
System.out.println("f2a is " + Shiftday1.getF2aOperator()+ " " + Shiftday1.getF2aShift() + " " + Shiftday1.getF2aRefTime());

shiftRotaA.addDay( Shiftday1.getShiftDate(),Shiftday1);




shiftDay ShiftDay2 = shiftRotaA.getDay(200308);

ShiftDay2.displayDay();


}
}


All compile ok but when I run it I get the following error

Exception in thread "main" java.lang.NullPointerException
at ShiftRota1.addDay(ShiftRota1.java:18)
at ThirdInstance.main(ThirdInstance.java:60)

As far as I can tell every object is referenced, so where am I going wrong.



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
Welcome to JavaRanch.

Please use the CODE button below the message window when posting code; it makes it much easier to read.

Somehow you have managed to get an object reference which references null and tried to use it.

Look at the ShiftRota1 constructor. You have declared the field inside the constructor. Never do that. Declare it outside any methods, and instantiate it inside the constructor. Otherwise it becomes a local variable to the constructor and it shadows a field declaration and you get NullPointerExceptions. In fact when I copied and pasted your code, I got two compiler errors caused by that shadowing, so you have not posted the code which you are having problems with. Please use ctrl-C-ctrl-V to PostRealCode.

But I think a shadowing declaration may be the cause of your problem.
Al Dodd
Greenhorn

Joined: Feb 08, 2008
Posts: 7
Thanks for that !

Saw the error you pointed out , changed it and got the output I wanted!

Thanks for coming to my rescue!

Java Ranch is cool!

Cheers Campbell.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: creating a new object from a gui input