0

I would like to export a variable which is entered into the jtextfield to main class from other class. Code of other class:

public void initComponents(){
    this.getContentPane().add(panel, BorderLayout.NORTH);
    panel.add(button);
    panel.add(text);

    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            back(e);
        }
    }); 

}
private void back(ActionEvent e){
    this.setVisible(false);
    new Main().setVisible(true);
}

public String getName1(){
    return text.getText();
} 

And the main class code:

public void initComponents(){
    this.getContentPane().add(panel);
    panel.add(go);
    panel.add(show);

    go.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            goAction(e);
        }
    });

    show.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Konto k = new Konto();
            System.out.println(k.getName1());
        }
    }); 
}
JPanel panel = new JPanel();
JButton go = new JButton("new user");
JButton show = new JButton("show");

private void goAction(ActionEvent e){
    this.setVisible(false);
    new Konto().setVisible(true);
}

User manual: At the first press a button "new user", there's opening new frame, enter a nickname e.g. Martin. Then press a button "back" and the main frame is opening. Then press a button "show" and in console should appear a nickname "Martin", but there appear null ( no one char ).

Kris
  • 13
  • 4
  • Don't understand the question. "*I would like to export a variable*" which variable? – Michael May 12 '20 at 14:59
  • @Michael A String which is entered to JTextField – Kris May 12 '20 at 15:01
  • @Michael There's already a getName1() method. Based on the "user manual" description OP doesn't understand how to call that method from a different class. A warning to OP: if you don't have a grasp of the basics, you are in for a rough time. I strongly suggest you dial back your expectations and study the fundamentals before proceeding further. – MarsAtomic May 12 '20 at 15:01
  • @MarsAtomic I'm checking now, give me a moment. – Kris May 12 '20 at 15:02
  • @MarsAtomic yea, that's it. Thanks for help :) – Kris May 12 '20 at 15:03

0 Answers0