SHARE
TWEET

Untitled

a guest Mar 24th, 2019 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package TextField;
  2.     import java.awt.FlowLayout;
  3.     import java.awt.event.ActionListener;
  4.     import java.awt.event.ActionEvent;
  5.     import javax.swing.JFrame;
  6.     import javax.swing.JTextField;
  7.     import javax.swing.JPasswordField;
  8.     import javax.swing.JOptionPane;
  9.  
  10. public class TextFieldFrame extends JFrame{
  11.    
  12.     private JTextField textField1;
  13.     private JTextField textField2;
  14.     private JTextField textField3;
  15.     private JPasswordField passwordField;
  16.  public TextFieldFrame(){
  17.         super("Testing JTextField and JPasswordField");
  18.         setLayout(new FlowLayout() );
  19.         textField1 = new JTextField(10);
  20.         add ( textField1 );
  21.         textField2 = new JTextField( "Enter text here" );
  22.         add( textField2 );
  23.         textField3 = new JTextField( "Uneditable text field", 21);
  24.         textField3.setEditable( false );
  25.         add ( textField3 ); passwordField = new JPasswordField( "Hiden text" );
  26.         add( passwordField );
  27.         TextFieldHandler handler = new TextFieldHandler();
  28.         textField1.addActionListener( handler );
  29.         textField2.addActionListener( handler );
  30.         textField3.addActionListener( handler );
  31.         passwordField.addActionListener( handler );
  32.     }
  33.     private class TextFieldHandler implements ActionListener{
  34.         @Override
  35.         public void actionPerformed( ActionEvent event)
  36.         {
  37.             String string = "";
  38.             if (event.getSource() == textField1)
  39.                 string = String.format("textField1: %s",
  40.                     event.getActionCommand());
  41.             else if (event.getSource() == textField2)
  42.                 string = String.format("textField2: %s",
  43.                         event.getActionCommand());
  44.             else if (event.getSource() == textField3)
  45.                 string = String.format("textField3: %s",
  46.                         event.getActionCommand());
  47.             else if (event.getSource() == passwordField)
  48.                 string = String.format("passwordField: %s",
  49.                         event.getActionCommand());
  50.             JOptionPane.showMessageDialog(null, string);
  51.         }
  52.     }
  53. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top