0

I have the following simple example;

public class TES extends JFrame {

private JPanel contentPane;
private JTextField textField;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                TES frame = new TES();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public TES() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 100, 100);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);

    textField = new JTextField();
    textField.setColumns(10);

    JButton btnNewButton = new JButton("New button");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {


            try {
                System.out.println("Before ");
                textField.setText("Before");
                Thread.sleep(4000);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            textField.setText("After");
        }
    });



    GroupLayout gl_contentPane = new GroupLayout(contentPane);
    gl_contentPane.setHorizontalGroup(
        gl_contentPane.createParallelGroup(Alignment.TRAILING)
            .addGroup(gl_contentPane.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnNewButton)
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(gl_contentPane.createSequentialGroup()
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
    );
    gl_contentPane.setVerticalGroup(
        gl_contentPane.createParallelGroup(Alignment.LEADING)
            .addGroup(gl_contentPane.createSequentialGroup()
                .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(ComponentPlacement.RELATED, 61, Short.MAX_VALUE)
                .addComponent(btnNewButton)
                .addContainerGap())
    );
    contentPane.setLayout(gl_contentPane);


}

}

The textField.settext("Before") line is not being executed.

            try {
                System.out.println("Before ");
                textField.setText("Before");
                Thread.sleep(4000);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            textField.setText("After");
        }

The System.out.println("Before ") line is executed though. I put in thread.sleep(4000) just to pause so I can see the "Before" and "After" texts in the JtextField. In my original code, I am running another class in that place;

                                    try {
                System.out.println("Before ");
                textField.setText("Before");
                Functions.dbSet();
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            textField.setText("After");
        }

SO the main problem is, textField.setText("Before"); line doesnt get executed.

user2034602
  • 51
  • 1
  • 1
  • 6

1 Answers1

0

Looks like you need to bone up on Java's "Event dispatch thread".

See here: Java Event-Dispatching Thread explanation

Or http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html

Community
  • 1
  • 1
John3136
  • 28,809
  • 4
  • 51
  • 69