So, I started studying java about a week ago, I'm running into a few issues with a little program I'm building to train with swing and oop/java in general.
The program (so far) has a MainClass and a Window class. The MainClass creates an instance of the Window class, which creates a JFrame and saves the user input in a field .
At this point, MainClass prints the output, which I get through getters methods.
The problem is that I still think in a procedural way: MainClass prints null, because it doesn't wait for the istance of window to get user input.
How can I fix it, thus getting main to wait for the istance of window to accept user input, before printing?
Nb. The Jframe stuff works, the window appears, it's just that MainClass doesn't wait for it to do what it's supposed to. I could (I think?) use some sleep command to wait but it seems utterly wrong.
here's the code of MainClass.java
import java.util.Arrays;
public class MainClass {
private char[] password;
private String pin;
public static void main(String[] args) {
Window w = new Window();
System.out.println(w.getPin() + Arrays.toString(w.getPassword()) + '1');
}
}
and Window.java
import java.awt.*;
import javax.swing.*;
import java.awt.Window.Type;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import net.miginfocom.swing.MigLayout;
public class Window extends JFrame{
private JTextField textField_1;
private JButton btnNewButton;
private JPanel panel;
private JPasswordField passwordField;
private char[] password = new char[10];
private String pin;
public Window() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setSize(370, 150);
this.setForeground(new Color(192, 192, 192));
this.setTitle("Access Password Manager");
this.setResizable(false);
panel = new JPanel();
getContentPane().add(panel, BorderLayout.CENTER);
panel.setLayout(new MigLayout("", "[70.00][132.00,grow][44.00][67.00,grow][61.00][]", "[19.00][34.00][]"));
JLabel lblNewLabel = new JLabel("Password");
panel.add(lblNewLabel, "cell 0 1,alignx trailing,aligny center");
passwordField = new JPasswordField();
passwordField.setColumns(13);
panel.add(passwordField, "cell 1 1,alignx center");
JLabel lblNewLabel_1 = new JLabel("Key");
panel.add(lblNewLabel_1, "cell 2 1,alignx center,aligny center");
textField_1 = new JTextField();
panel.add(textField_1, "cell 3 1,alignx left,aligny center");
textField_1.setColumns(4);
btnNewButton = new JButton("Log In");
ListenForButton listener = new ListenForButton();
btnNewButton.addActionListener(listener);
panel.add(btnNewButton, "cell 4 1");
this.setVisible(true);
}
private class ListenForButton implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btnNewButton){
if (passwordField.getPassword().length < 10){
password = passwordField.getPassword().clone();
}
pin = textField_1.getText();
}
}
}
public char[] getPassword(){
return password;
}
public String getPin(){
return pin;
}
}
EDIT:
It's not just about printing, which I know I could do directly into Window.class. I'm sorry if I explained myself poorly. Please consider the println as a "I need to access and work on those fields once window has saved them form the input".