1

I'm working on a program which has 2 classes. The first one is just for the GUI of the program. And in the other is the code to create new files with the needed data.

I want to use the text, wich the user writes in a jTextField. But my problem is, that I'm not able to save the text in a String and than call it in the other class with the code to save the user inputs.

So in the Class NetworkComponentFrame is following code:

public class NetworkComponentFrame extends javax.swing.JFrame {
/* GUI Code */
public static void main(String args[]) {
String ID = jTextField1.getText();
/* other code */
}}

And in the Class NetworkComponent is at the moment the following code:

public class NetworkComponent {
public static void New(){
NetworkComponentFrame ID = new NetworkComponentFrame();
/*other code */}}

The names are just some placeholders. I hope this is enough information to help me solving the problem.

1
  • 1
    Perhaps make a public getter/setter for the string that can be called in the other class?
    – graviton
    Dec 17, 2013 at 14:13

3 Answers 3

0

You can pass value by creating a constructor NetworkComponent or static property of your NetworkComponentFrame class. e.g

public class NetworkComponentFrame extends javax.swing.JFrame {
/* GUI Code */
public String ID;
public static void main(String args[]) {
ID = jTextField1.getText();
/* other code */
}}

in your second class

public class NetworkComponent {
public static void New(){
s.o.p(NetworkComponentFrame.ID);
/*other code */}}
0

If i understand it correct, u may define String ID out of the main function but in NetworkComponentFrame class as public so u may reach it wherever u want after u create an object from your class.

Eg:

public class NetworkComponentFrame extends javax.swing.JFrame {
public String ID_text;
/* GUI Code */
public static void main(String args[]) {
ID_text = jTextField1.getText();
/* other code */
}}


public class NetworkComponent {
public static void New(){
NetworkComponentFrame ID = new NetworkComponentFrame();<
String test = ID.ID_text;
/*other code */}}

And something problematic with your example code. Are u sure u are calling public static void main function in a non static object?

Extended: U may also use getters and setters.

1
  • this helped me a lot but if i just do a System.out.println(ID_text); in the NetworkComponentFrame class. --> Output is right. But in the other class is the output "null". where's the problem?
    – Pulsar
    Dec 17, 2013 at 15:01
0

I think you want it the other way around. Instantiate the NetworkComponent in the GUI and have a setter method in the NetworkComponent and use the text from the text field to set the text in the NetworkComponent object.

public class NetworkComponent {
    String ID;

    public void setId(String text){
        ID = text;
    }
}

public class NetworkComponentFrame extends javax.swing.JFrame {
    NetworkComponent network = new NetWorkComponent();  <-- NetworkComponent object
    JButton button = new JButton();
    JTextField textField = new JTextField();

    public NetworkComponentFrame(){
        button.addActionListener(new ActionListener(){
            public actionPerformed(ActionEvent e){
                String text = textField.getText();
                network.setId(text);                   <-- set the ID here
            }
        });
    }
}
2
  • I know it sounds funny but the teacher said it, that I have to do it this way :/ but thanks anyway
    – Pulsar
    Dec 17, 2013 at 14:43
  • Tell your teacher that's bad design, Instantiating your View/Controller inside your Model. Should be the other way around. See what He/She says. Dec 17, 2013 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.