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 ).