Getting the Text of a Text Field
The method is getText(), and there is a variant to retrieve just the selected text:
Sample Code
//... from UI
/**
* Safely read the text of the given text component.
*/
public static String getText( JTextComponent textComponent ) {
return getTextImpl( textComponent, true )
}
/**
* Safely read the selected text of the given text component.
*/
public static String getSelectedText(
JTextComponent textComponent ) {
return getTextImpl( textComponent, false )
}
private static String getTextImpl(
final JTextComponent textComponent,
final boolean allText ) {
final String[] resultHolder = new String[]{null}
runInEventThread( new Runnable() {
public void run() {
resultHolder[0] = allText ? textComponent.getText() :
textComponent.getSelectedText()
}
} )
return resultHolder[0]
}
Copyright exforsys.com
Frame Disposal
In a lot of our unit tests, we will want to dispose of any dialogs or frames that are still showing at the end of a test. This method is brutal but effective:
Sample Code
//... from UI
public static void disposeOfAllFrames() {
Runnable runnable = new Runnable() {
public void run() {
Frame[] allFrames = Frame.getFrames()
for (Frame allFrame : allFrames) {
allFrame.dispose()
}
}
}
runInEventThread( runnable )
}
Copyright exforsys.com