Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

SetText(); not working on JTextField

843789Nov 14 2009 — edited Nov 15 2009
Hi Guys,

I am trying to set the string to a JTextField with the filepath that the JFileChooser collects from the user. My testing shows that the filepath is collected correctly, and in further investigating I believe that I have a concurrency problem.
So I have tried to put the setText() method away from the EDT and it's still not working. Can you please have a look and let me know how I can get the setText() method actually setting the text?

Thank you, and let me know if need to look at the other JDialogs, JFrames, JFileChooser classes etc.
package carsuite;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.SwingUtilities;

public class Import extends javax.swing.JDialog implements ActionListener {
    OpenDialog open;
    String location = null;

    /** Creates new form Import */
    public Import(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    protected void setup(){
        this.jButton1.addActionListener(this);
        jButton1.setActionCommand("BROWSE");
        this.btnCancel.addActionListener(this);
        btnCancel.setActionCommand("CANCEL");
    }
                      
    private void initComponents() {
        //removed many lines of code in this post only, to make room
        pack();
    }
    private void doSetLocation(){
        File file;
        file = open.getSelectedFile();
        location = file.getPath();
        txtLocation.updateUI();

        new Thread(new Runnable() {
            public void run() {
                try {
                    writeText();
                }
                catch(Exception e) {

                }
            }
        }).start();
    }

    public void writeText() {
	    if (SwingUtilities.isEventDispatchThread()) {
	    }
        else{
	    SwingUtilities.invokeLater(new Runnable() {
   	        public void run() {
                    System.out.print("it is here");    //<--------------this prints, but setText() doesn't work
	 	    txtLocation.setText(location);
                }
            });
        }
    }

    public void actionPerformed(ActionEvent e){
        String cmd = e.getActionCommand();
        if("BROWSE".equals(cmd)){
            open = new OpenDialog();
            open.setVisible(true);
            this.setVisible(false);
            if(open.setup()){
                this.setVisible(true);
                doSetLocation();
            }
        }
        else if("CANCEL".equals(cmd)){
            this.setVisible(false);
        }
    }
    // Variables declaration - do not modify                     
.......
    // End of variables declaration                   
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 13 2009
Added on Nov 14 2009
7 comments
2,882 views