How to refresh a JTextField before continuing execution?

R

Ramon F Herrera

Hello:

In several of my Dialogs, I have some phone number, file path, etc. in
a JTextField. Then there is an button that starts some long running
class. My problem is that I would like to convert the {phone number,
SSN, file path} to its canonical form and have the user see it
*before* the long running class starts.

For example, I have a dialog that contains two filepaths with the DOS
backslashes (as returned by JFileChooser), and I would prefer them to
be shown with forward slashes ASAP. Currently, the replacement takes
places after the long running 'ProcessTIFF' instance ends.

final JButton matchAllFilesButton = new JButton();
matchAllFilesButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
String srcStr = sourceDirTextField.getText().replace('\\',
'/');
String dstStr = destinDirTextField.getText().replace('\\',
'/');
sourceDirTextField.setText(srcStr);
destinDirTextField.setText(dstStr);
// would like to insert some sort of sleep() or 'redraw' here
new ProcessTIFF(sourceDirTextField.getText(),
destinDirTextField.getText());
}
});

I would like to provide a more expedient visual feedback to my users.

TIA,

-Ramon
 
K

Knute Johnson

Ramon said:
Hello:

In several of my Dialogs, I have some phone number, file path, etc. in
a JTextField. Then there is an button that starts some long running
class. My problem is that I would like to convert the {phone number,
SSN, file path} to its canonical form and have the user see it
*before* the long running class starts.

For example, I have a dialog that contains two filepaths with the DOS
backslashes (as returned by JFileChooser), and I would prefer them to
be shown with forward slashes ASAP. Currently, the replacement takes
places after the long running 'ProcessTIFF' instance ends.

final JButton matchAllFilesButton = new JButton();
matchAllFilesButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
String srcStr = sourceDirTextField.getText().replace('\\',
'/');
String dstStr = destinDirTextField.getText().replace('\\',
'/');
sourceDirTextField.setText(srcStr);
destinDirTextField.setText(dstStr);
// would like to insert some sort of sleep() or 'redraw' here
new ProcessTIFF(sourceDirTextField.getText(),
destinDirTextField.getText());
}
});

I would like to provide a more expedient visual feedback to my users.

TIA,

-Ramon

Just move your long running task away from the EDT. Put it in a simple
Runnable and start it.

Runnable r = new Runnable() {
public void run() {
// long running task goes here
}
};
new Thread(r).start();

You can leave this code in the actionPerformd() method and it should
work just fine. If you need to prevent any GUI actions from happening
while that task is running, disable the button or whatever that starts
it until your long running task is done.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top