godcomplex
Programmer
Hello All,
I am sorry ahead of time if this question has already been answered somewhere in the forum, but I couldn't find it. I am simply trying to find a method to clear the tex field of all its text (ie set it back to its original state). When I try to use the method setText( String value ), and set value = "", I get some wierd results. Here's the code, pretty simple stuff:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;
public class GUITest
{
public static void main( String args[] )
{
JFrame frame = new TestFrame();
frame.show();
}
}
class TestFrame extends JFrame implements ActionListener
{
private JFormattedTextField ssnText;
private JFormattedTextField dateText;
private JButton button;
public TestFrame()
{
setTitle( "GUI Test" );
setSize( 300, 200 );
addWindowListener( new WindowAdapter()
{
public void WindowClosingEvent( WindowEvent e )
{
System.exit( 0 );
}
} );
button = new JButton( "Clear" );
button.addActionListener( this );
try
{
ssnText = new JFormattedTextField( new MaskFormatter( "###-##-####" ) );
dateText = new JFormattedTextField( new MaskFormatter( "##/##/## ##:##" ) );
} catch( Exception e ){}
getContentPane().setLayout( new FlowLayout() );
getContentPane().add( ssnText );
getContentPane().add( dateText );
getContentPane().add( button );
}
public void actionPerformed( ActionEvent event )
{
System.out.println( "SSN = \"" + ssnText.getText() + "\"" );
System.out.println( "DATE = \"" + dateText.getText() + "\"" );
ssnText.setText( "" );
dateText.setText( "" );
}
}
If anyone could give me a hand with this I would appreciate it.
-gc
I am sorry ahead of time if this question has already been answered somewhere in the forum, but I couldn't find it. I am simply trying to find a method to clear the tex field of all its text (ie set it back to its original state). When I try to use the method setText( String value ), and set value = "", I get some wierd results. Here's the code, pretty simple stuff:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;
public class GUITest
{
public static void main( String args[] )
{
JFrame frame = new TestFrame();
frame.show();
}
}
class TestFrame extends JFrame implements ActionListener
{
private JFormattedTextField ssnText;
private JFormattedTextField dateText;
private JButton button;
public TestFrame()
{
setTitle( "GUI Test" );
setSize( 300, 200 );
addWindowListener( new WindowAdapter()
{
public void WindowClosingEvent( WindowEvent e )
{
System.exit( 0 );
}
} );
button = new JButton( "Clear" );
button.addActionListener( this );
try
{
ssnText = new JFormattedTextField( new MaskFormatter( "###-##-####" ) );
dateText = new JFormattedTextField( new MaskFormatter( "##/##/## ##:##" ) );
} catch( Exception e ){}
getContentPane().setLayout( new FlowLayout() );
getContentPane().add( ssnText );
getContentPane().add( dateText );
getContentPane().add( button );
}
public void actionPerformed( ActionEvent event )
{
System.out.println( "SSN = \"" + ssnText.getText() + "\"" );
System.out.println( "DATE = \"" + dateText.getText() + "\"" );
ssnText.setText( "" );
dateText.setText( "" );
}
}
If anyone could give me a hand with this I would appreciate it.
-gc