JTextField setText() does nothing

Discussion in 'Java' started by jonasmin@gmail.com, Nov 24, 2007.

  1. Guest

    I have problem with setting the JTextField text. here you see some of
    my code:

    if (in1.getText().length() > 10){
    JOptionPane.showMessageDialog(null, "Error", "Error",
    JOptionPane.WARNING_MESSAGE);
    in1.setText("");
    }


    in1 - JTextField. But when i axecute the program it does nothing. It
    shows the WARNING_MESSAGE, but it does nothing with text. where can be
    the problem?
     
    , Nov 24, 2007
    #1
    1. Advertising

  2. wrote:
    > I have problem with setting the JTextField text. here you see some of
    > my code:
    >
    > if (in1.getText().length() > 10){
    > JOptionPane.showMessageDialog(null, "Error", "Error",
    > JOptionPane.WARNING_MESSAGE);
    > in1.setText("");
    > }
    >
    >
    > in1 - JTextField. But when i axecute the program it does nothing. It
    > shows the WARNING_MESSAGE, but it does nothing with text. where can be
    > the problem?


    JOptionPane.showMessageDialog is modal so the in1.setText() won't get
    executed until the dialog is closed.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class test {
    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    JFrame f = new JFrame("test");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JTextField tf = new JTextField("Hello World");
    f.add(tf,BorderLayout.CENTER);
    JButton b = new JButton("Erase The Text");
    b.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
    JOptionPane.showMessageDialog(
    null,"Error","Error",
    JOptionPane.WARNING_MESSAGE);
    tf.setText("");
    }
    });
    f.add(b,BorderLayout.SOUTH);
    f.pack();
    f.setVisible(true);
    }
    });
    }
    }

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Nov 24, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Yamin
    Replies:
    11
    Views:
    2,670
  2. Yamin
    Replies:
    0
    Views:
    437
    Yamin
    Jul 21, 2004
  3. Mickey Segal
    Replies:
    0
    Views:
    630
    Mickey Segal
    Sep 15, 2004
  4. Mickey Segal
    Replies:
    1
    Views:
    521
    Mickey Segal
    Sep 26, 2004
  5. Devin Jeanpierre
    Replies:
    2
    Views:
    467
    Devin Jeanpierre
    Feb 14, 2012
Loading...

Share This Page