0

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.

B Whitehead
  • 49
  • 2
  • 8
  • 1
    There is no JTextArea in that code that you've posted??? – Hovercraft Full Of Eels Nov 20 '16 at 23:52
  • I meant JTextField, sorry about that – B Whitehead Nov 20 '16 at 23:54
  • Also you never want to use a `while (true)` loop as you're doing in an event-driven program as this goes against the entire paradigm of event-driven programming and can grind your GUI to a halt. Instead you want your program to **notify** you of changes, by using a listener, here a DocumentListener. Look at the duplicate used to close this question for more on this as well as the tutorial which can easily be found via Google. – Hovercraft Full Of Eels Nov 20 '16 at 23:55
  • Take a look at https://docs.oracle.com/javase/7/docs/api/javax/swing/JTextField.html#addActionListener(java.awt.event.ActionListener). – Stephen L. Nov 20 '16 at 23:57
  • [www.google.com/#q=documentlistener+tutorial](https://www.google.com/#q=documentlistener+tutorial) – Hovercraft Full Of Eels Nov 20 '16 at 23:58
  • textField.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { System.out.println(textField.getText()); } } – Stephen L. Nov 21 '16 at 00:00
  • Or add an ActionListener to the JTextField as @Stephen notes in his link. The DocumentListener will notify you of any changes to the text in the JTextField while the ActionListenter will notify you only when enter has been pressed. – Hovercraft Full Of Eels Nov 21 '16 at 00:00

0 Answers0