Home
Reviews
Swing Extreme Testing
This tutorial is part of a Swing Extreme Testing tutorial series. Read it from the beginning and learn yourself.
Reviews
Swing Extreme TestingSwing Extreme Testing - Getting the Text of a Text Field
Author: Packt Publishing Published on: 10th Jul 2008
Getting the Text of a Text Field
The method is getText(), and there is a variant to retrieve just the selected text:
Ads
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
Ads
Swing Extreme Testing
- Swing Extreme Testing
- Swing Extreme Testing - Outline of the Unit Test
- Swing Extreme Testing - Getting the Text of a Text Field
- Swing Extreme Testing - Unit Test Infrastructure
- Swing Extreme Testing - The ShowerThread Class
- Swing Extreme Testing - The init() Method
- Swing Extreme Testing - The Constructor Test
- Swing Extreme Testing - The wasCancelled() Test
- Swing Extreme Testing - The name() Test
- Swing Extreme Testing - The Data Validation Test







