I am trying to print the value of my JTextField every time that I edit it. im pretty new to Swing so I don't know everything about it. I just want to print it to the console at the moment
package searchdatabase.main;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Main {
public static String CURRENT_STRING = null;
//Swing Variables Declare
public static JFrame frame;
public static JPanel panel;
public static JTextField inputField;
public static void main(String[] args) {
createDisplay("Search Database", 400, 400);
getCurrentString(60);
}
public static void createDisplay(String title, int width, int height) {
//Create JFrame
frame = new JFrame(title);
frame.setSize(new Dimension(width, height));
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create JPanel
panel = new JPanel();
//Create JTextField
inputField = new JTextField();
inputField.setPreferredSize(new Dimension(width / 2, height / 22));
//Adds everything
panel.add(inputField);
frame.add(panel);
//Sets JFrame Visible After adding everything
frame.setVisible(true);
}
public static void getCurrentString(int max_time) {
int time = 0;
String DEF_STRING = null;
System.out.println("Method Running");
System.out.println("_______________");
while(true) {
time++;
if(time == max_time) {
time = 0;
}
if(CURRENT_STRING == DEF_STRING) {
//Don't do anything
} else if(CURRENT_STRING != DEF_STRING) {
CURRENT_STRING = inputField.getText();
System.out.println(CURRENT_STRING);
}
}
}
}
I think I narrowed it down to something wrong with the getCurrentString method, mainly in the while loop.