Search code examples
javaswingjtextfieldnetbeans-7

How to take the value of a JTextField after being modified by the user [java]


I have an empty JTextField that the user should modify with an int number. However, even once the JTextFiled has been filled with an integer, when I go to get the String from that JTextField, the string results "" (-> empty). How could I save the new value of the Field? what should I do? Here's the problem:

//Class where JTextField is initialized: 

 javax.swing.JTextField tMax = new javax.swing.JTextField();
 tMax.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tMaxKeyTyped(evt);
            }
        });
private void tMaxKeyTyped(java.awt.event.KeyEvent evt) {                                   
// TODO add your handling code here:
    char c = evt.getKeyChar();
    if(!(Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE) || c == KeyEvent.VK_DELETE)) {
        getToolkit().beep();
        evt.consume();
    }
}
public String getTMax() {
    String tMaxString = tMax.getText();
    return tMaxString;
}


//Class where tMax should be used

    //code
   private void runBtnActionPerformed(java.awt.event.ActionEvent evt) {
   Panel class1 = new Panel();
   String tMaxString = class1.getTMax();
   System.out.println(tMaxString);

When I run the program and I click the Run Botton an Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "".

What can I do for saving the modified JTextField? Thank you so much.


Solution

  • At last I managed this issue by using a DocumentListener. Here's the solution:

    package AppPackage;
    
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    import javax.swing.event.DocumentEvent;
    import javax.swing.event.DocumentListener;
    
            public class Test {
                JFrame frame = new JFrame("Test");
                JPanel panel = new JPanel();
                JTextField option = new JTextField("HI", 10);
                static String optionString;
    
                public Test() {
                   option.getDocument().addDocumentListener(new DocumentListener() {
    
                        @Override
                        public void insertUpdate(DocumentEvent e) {
                            warn();
                        }
    
                        @Override
                        public void removeUpdate(DocumentEvent e) {
                            warn();
                        }
    
                        @Override
                        public void changedUpdate(DocumentEvent e) {
                            warn();
                        }
    
                        public void warn() {
                            optionString = option.getText();
                            System.out.println(optionString);
                        }
                    });
                    panel.add(option);
                    frame.add(panel);
                    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    frame.pack();
                    frame.setVisible(true);
                }
    
                  public static void main(String[] args) {
                        SwingUtilities.invokeLater(new Runnable() {
                             public void run() {
                                 new Test();
                             }
                        });
                  }
          }
    

    Hope this will be useful to someone.