How to get values from text fields and use them in another class?
I have a GUI that allows a user to enter their personal information. I want to get all that information and use it in another class to create an instance of that person. I keep getting
non-static method getValues(String, String...) cannot be referenced from a static context
even though none of my methods are static.
I have tried to use individual getters for each text field, but that gave the same error. I also tried to get each value through one method and then call it in the other class but that doesn't work either.
In this class I am trying to get the values from the GUI:
public class PatientStrategy implements IAccountStrategy {
@Override
public void createAccount(String accountType, String firstname, String lastname,
String address, String postcode, String sex, Integer age){
PMSGUI.getValues(accountType, firstname, lastname, address, postcode, sex, age);
}
And this is the method I'm using in the GUI itself to get the values from the text fields:
public class PMSGUI extends javax.swing.JFrame {
public void getValues(String accountType, String firstname, String lastname, String address,
String postcode, String sex, Integer age) {
accountType = cboAccountType.getSelectedItem().toString();
firstname = txtFirstName.getText();
lastname = txtLastName.getText();
address = txtAddress.getText();
postcode = txtPostcode.getText();
sex = cboSex.getSelectedItem().toString();
age = Integer.parseInt(txtAge.getText());
}
I am using an observer and strategy pattern and so I will be taking the values from the GUI and will be creating a new instance of each person as an observer.
I'm new to programming in Java and so I know I'm probably going about this all wrong. Any help is appreciated!
java swing
|
show 3 more comments
I have a GUI that allows a user to enter their personal information. I want to get all that information and use it in another class to create an instance of that person. I keep getting
non-static method getValues(String, String...) cannot be referenced from a static context
even though none of my methods are static.
I have tried to use individual getters for each text field, but that gave the same error. I also tried to get each value through one method and then call it in the other class but that doesn't work either.
In this class I am trying to get the values from the GUI:
public class PatientStrategy implements IAccountStrategy {
@Override
public void createAccount(String accountType, String firstname, String lastname,
String address, String postcode, String sex, Integer age){
PMSGUI.getValues(accountType, firstname, lastname, address, postcode, sex, age);
}
And this is the method I'm using in the GUI itself to get the values from the text fields:
public class PMSGUI extends javax.swing.JFrame {
public void getValues(String accountType, String firstname, String lastname, String address,
String postcode, String sex, Integer age) {
accountType = cboAccountType.getSelectedItem().toString();
firstname = txtFirstName.getText();
lastname = txtLastName.getText();
address = txtAddress.getText();
postcode = txtPostcode.getText();
sex = cboSex.getSelectedItem().toString();
age = Integer.parseInt(txtAge.getText());
}
I am using an observer and strategy pattern and so I will be taking the values from the GUI and will be creating a new instance of each person as an observer.
I'm new to programming in Java and so I know I'm probably going about this all wrong. Any help is appreciated!
java swing
UsingPMSGUI.getValues
is exactly where you are trying to access a non-static method in a static context. To access a method, you have to create an instance and use the method with that instance.
– RealSkeptic
Jan 1 at 17:25
Thanks for the reply. What do you mean by create an instance of the method exactly?
– Top Lit
Jan 1 at 17:29
Create an instance of the class, not of the method. Usenew PMSGUI()
somewhere. I'm sure you already have an instance or the GUI would not be shown.
– RealSkeptic
Jan 1 at 17:32
Oh I see. Would creating a new instance of the GUI reset all the values in the text field then, or does it create an instance while keeping the inputted values? Sorry if I'm looking at this the wrong way.
– Top Lit
Jan 1 at 17:35
You are supposed to use the same instance that you are using already, otherwise you'll have two GUIs appear.
– RealSkeptic
Jan 1 at 17:41
|
show 3 more comments
I have a GUI that allows a user to enter their personal information. I want to get all that information and use it in another class to create an instance of that person. I keep getting
non-static method getValues(String, String...) cannot be referenced from a static context
even though none of my methods are static.
I have tried to use individual getters for each text field, but that gave the same error. I also tried to get each value through one method and then call it in the other class but that doesn't work either.
In this class I am trying to get the values from the GUI:
public class PatientStrategy implements IAccountStrategy {
@Override
public void createAccount(String accountType, String firstname, String lastname,
String address, String postcode, String sex, Integer age){
PMSGUI.getValues(accountType, firstname, lastname, address, postcode, sex, age);
}
And this is the method I'm using in the GUI itself to get the values from the text fields:
public class PMSGUI extends javax.swing.JFrame {
public void getValues(String accountType, String firstname, String lastname, String address,
String postcode, String sex, Integer age) {
accountType = cboAccountType.getSelectedItem().toString();
firstname = txtFirstName.getText();
lastname = txtLastName.getText();
address = txtAddress.getText();
postcode = txtPostcode.getText();
sex = cboSex.getSelectedItem().toString();
age = Integer.parseInt(txtAge.getText());
}
I am using an observer and strategy pattern and so I will be taking the values from the GUI and will be creating a new instance of each person as an observer.
I'm new to programming in Java and so I know I'm probably going about this all wrong. Any help is appreciated!
java swing
I have a GUI that allows a user to enter their personal information. I want to get all that information and use it in another class to create an instance of that person. I keep getting
non-static method getValues(String, String...) cannot be referenced from a static context
even though none of my methods are static.
I have tried to use individual getters for each text field, but that gave the same error. I also tried to get each value through one method and then call it in the other class but that doesn't work either.
In this class I am trying to get the values from the GUI:
public class PatientStrategy implements IAccountStrategy {
@Override
public void createAccount(String accountType, String firstname, String lastname,
String address, String postcode, String sex, Integer age){
PMSGUI.getValues(accountType, firstname, lastname, address, postcode, sex, age);
}
And this is the method I'm using in the GUI itself to get the values from the text fields:
public class PMSGUI extends javax.swing.JFrame {
public void getValues(String accountType, String firstname, String lastname, String address,
String postcode, String sex, Integer age) {
accountType = cboAccountType.getSelectedItem().toString();
firstname = txtFirstName.getText();
lastname = txtLastName.getText();
address = txtAddress.getText();
postcode = txtPostcode.getText();
sex = cboSex.getSelectedItem().toString();
age = Integer.parseInt(txtAge.getText());
}
I am using an observer and strategy pattern and so I will be taking the values from the GUI and will be creating a new instance of each person as an observer.
I'm new to programming in Java and so I know I'm probably going about this all wrong. Any help is appreciated!
java swing
java swing
edited Jan 1 at 19:18


Kод it
2,67342240
2,67342240
asked Jan 1 at 17:18


Top LitTop Lit
327
327
UsingPMSGUI.getValues
is exactly where you are trying to access a non-static method in a static context. To access a method, you have to create an instance and use the method with that instance.
– RealSkeptic
Jan 1 at 17:25
Thanks for the reply. What do you mean by create an instance of the method exactly?
– Top Lit
Jan 1 at 17:29
Create an instance of the class, not of the method. Usenew PMSGUI()
somewhere. I'm sure you already have an instance or the GUI would not be shown.
– RealSkeptic
Jan 1 at 17:32
Oh I see. Would creating a new instance of the GUI reset all the values in the text field then, or does it create an instance while keeping the inputted values? Sorry if I'm looking at this the wrong way.
– Top Lit
Jan 1 at 17:35
You are supposed to use the same instance that you are using already, otherwise you'll have two GUIs appear.
– RealSkeptic
Jan 1 at 17:41
|
show 3 more comments
UsingPMSGUI.getValues
is exactly where you are trying to access a non-static method in a static context. To access a method, you have to create an instance and use the method with that instance.
– RealSkeptic
Jan 1 at 17:25
Thanks for the reply. What do you mean by create an instance of the method exactly?
– Top Lit
Jan 1 at 17:29
Create an instance of the class, not of the method. Usenew PMSGUI()
somewhere. I'm sure you already have an instance or the GUI would not be shown.
– RealSkeptic
Jan 1 at 17:32
Oh I see. Would creating a new instance of the GUI reset all the values in the text field then, or does it create an instance while keeping the inputted values? Sorry if I'm looking at this the wrong way.
– Top Lit
Jan 1 at 17:35
You are supposed to use the same instance that you are using already, otherwise you'll have two GUIs appear.
– RealSkeptic
Jan 1 at 17:41
Using
PMSGUI.getValues
is exactly where you are trying to access a non-static method in a static context. To access a method, you have to create an instance and use the method with that instance.– RealSkeptic
Jan 1 at 17:25
Using
PMSGUI.getValues
is exactly where you are trying to access a non-static method in a static context. To access a method, you have to create an instance and use the method with that instance.– RealSkeptic
Jan 1 at 17:25
Thanks for the reply. What do you mean by create an instance of the method exactly?
– Top Lit
Jan 1 at 17:29
Thanks for the reply. What do you mean by create an instance of the method exactly?
– Top Lit
Jan 1 at 17:29
Create an instance of the class, not of the method. Use
new PMSGUI()
somewhere. I'm sure you already have an instance or the GUI would not be shown.– RealSkeptic
Jan 1 at 17:32
Create an instance of the class, not of the method. Use
new PMSGUI()
somewhere. I'm sure you already have an instance or the GUI would not be shown.– RealSkeptic
Jan 1 at 17:32
Oh I see. Would creating a new instance of the GUI reset all the values in the text field then, or does it create an instance while keeping the inputted values? Sorry if I'm looking at this the wrong way.
– Top Lit
Jan 1 at 17:35
Oh I see. Would creating a new instance of the GUI reset all the values in the text field then, or does it create an instance while keeping the inputted values? Sorry if I'm looking at this the wrong way.
– Top Lit
Jan 1 at 17:35
You are supposed to use the same instance that you are using already, otherwise you'll have two GUIs appear.
– RealSkeptic
Jan 1 at 17:41
You are supposed to use the same instance that you are using already, otherwise you'll have two GUIs appear.
– RealSkeptic
Jan 1 at 17:41
|
show 3 more comments
1 Answer
1
active
oldest
votes
The error message tells you that getValues()
, a non-static method, needs a non-static context. This context is provided by any instance of the defining class. An instantiation is a construction from a "static" class into a "dynamic" object (this object may only exist, while your program is running). Since one class can have many such objects, created from one class, each of them is called an instance.
Now if there should be at most one such window at a time, then you can apply the Singleton pattern to your GUI class. Then you would be able to access all non-static methods through the provided static instance of the class.
public class PMSGUI extends javax.swing.JFrame {
private static final PMSGUI instance = new PMSGUI();
private PMSGUI() {} // Restrict instantiation
public static PMSGUI getInstance() {
return instance;
}
// Add your custom methods further down here
}
Now to reference it in your calling method, use
PMSGUI.getInstance().getValues(accountType, firstname, lastname, address, postcode, sex, age);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997428%2fhow-to-get-values-from-text-fields-and-use-them-in-another-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The error message tells you that getValues()
, a non-static method, needs a non-static context. This context is provided by any instance of the defining class. An instantiation is a construction from a "static" class into a "dynamic" object (this object may only exist, while your program is running). Since one class can have many such objects, created from one class, each of them is called an instance.
Now if there should be at most one such window at a time, then you can apply the Singleton pattern to your GUI class. Then you would be able to access all non-static methods through the provided static instance of the class.
public class PMSGUI extends javax.swing.JFrame {
private static final PMSGUI instance = new PMSGUI();
private PMSGUI() {} // Restrict instantiation
public static PMSGUI getInstance() {
return instance;
}
// Add your custom methods further down here
}
Now to reference it in your calling method, use
PMSGUI.getInstance().getValues(accountType, firstname, lastname, address, postcode, sex, age);
add a comment |
The error message tells you that getValues()
, a non-static method, needs a non-static context. This context is provided by any instance of the defining class. An instantiation is a construction from a "static" class into a "dynamic" object (this object may only exist, while your program is running). Since one class can have many such objects, created from one class, each of them is called an instance.
Now if there should be at most one such window at a time, then you can apply the Singleton pattern to your GUI class. Then you would be able to access all non-static methods through the provided static instance of the class.
public class PMSGUI extends javax.swing.JFrame {
private static final PMSGUI instance = new PMSGUI();
private PMSGUI() {} // Restrict instantiation
public static PMSGUI getInstance() {
return instance;
}
// Add your custom methods further down here
}
Now to reference it in your calling method, use
PMSGUI.getInstance().getValues(accountType, firstname, lastname, address, postcode, sex, age);
add a comment |
The error message tells you that getValues()
, a non-static method, needs a non-static context. This context is provided by any instance of the defining class. An instantiation is a construction from a "static" class into a "dynamic" object (this object may only exist, while your program is running). Since one class can have many such objects, created from one class, each of them is called an instance.
Now if there should be at most one such window at a time, then you can apply the Singleton pattern to your GUI class. Then you would be able to access all non-static methods through the provided static instance of the class.
public class PMSGUI extends javax.swing.JFrame {
private static final PMSGUI instance = new PMSGUI();
private PMSGUI() {} // Restrict instantiation
public static PMSGUI getInstance() {
return instance;
}
// Add your custom methods further down here
}
Now to reference it in your calling method, use
PMSGUI.getInstance().getValues(accountType, firstname, lastname, address, postcode, sex, age);
The error message tells you that getValues()
, a non-static method, needs a non-static context. This context is provided by any instance of the defining class. An instantiation is a construction from a "static" class into a "dynamic" object (this object may only exist, while your program is running). Since one class can have many such objects, created from one class, each of them is called an instance.
Now if there should be at most one such window at a time, then you can apply the Singleton pattern to your GUI class. Then you would be able to access all non-static methods through the provided static instance of the class.
public class PMSGUI extends javax.swing.JFrame {
private static final PMSGUI instance = new PMSGUI();
private PMSGUI() {} // Restrict instantiation
public static PMSGUI getInstance() {
return instance;
}
// Add your custom methods further down here
}
Now to reference it in your calling method, use
PMSGUI.getInstance().getValues(accountType, firstname, lastname, address, postcode, sex, age);
answered Jan 1 at 19:44


Kод itKод it
2,67342240
2,67342240
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997428%2fhow-to-get-values-from-text-fields-and-use-them-in-another-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Using
PMSGUI.getValues
is exactly where you are trying to access a non-static method in a static context. To access a method, you have to create an instance and use the method with that instance.– RealSkeptic
Jan 1 at 17:25
Thanks for the reply. What do you mean by create an instance of the method exactly?
– Top Lit
Jan 1 at 17:29
Create an instance of the class, not of the method. Use
new PMSGUI()
somewhere. I'm sure you already have an instance or the GUI would not be shown.– RealSkeptic
Jan 1 at 17:32
Oh I see. Would creating a new instance of the GUI reset all the values in the text field then, or does it create an instance while keeping the inputted values? Sorry if I'm looking at this the wrong way.
– Top Lit
Jan 1 at 17:35
You are supposed to use the same instance that you are using already, otherwise you'll have two GUIs appear.
– RealSkeptic
Jan 1 at 17:41