Advertisement
Guest User

TextField and Button Example in Java

a guest
Jul 2nd, 2013
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.06 KB | None | 0 0
  1. import java.awt.FlowLayout;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4.  
  5. import javax.swing.JButton;
  6. import javax.swing.JFrame;
  7. import javax.swing.JOptionPane;
  8. import javax.swing.JPanel;
  9. import javax.swing.JTextField;
  10.  
  11. public class TextFieldAndButtonTutorial extends JFrame implements
  12.         ActionListener {
  13.  
  14.     /*
  15.      * Creating instances of text fields, buttons and a panel that we will use
  16.      * in this tutorial
  17.      */
  18.     JTextField txtField;
  19.     JButton btn1, btn2, btn3;
  20.     JPanel pnl;
  21.  
  22.     /*
  23.      * In this constructor we will initialize all the UI components and them to
  24.      * the Panel. One new thing that we will be doing is the Event Handling in
  25.      * this tutorial. Events are basically actions that we want the program to
  26.      * perform when we click on a button. For this tutorial we will cover just
  27.      * one type of event listener which is used for clicking events. Its is
  28.      * called "ActionListener".
  29.      */
  30.     public TextFieldAndButtonTutorial() {
  31.         /*
  32.          * This super calls the constructor of JFrame class that we extended and
  33.          * arguments passed in it set that as the title of the window.
  34.          */
  35.         super("A simple TextField and Button tutorial");
  36.         /*
  37.          * Setting the Layout as simple FlowLayout
  38.          */
  39.         setLayout(new FlowLayout());
  40.  
  41.         /*
  42.          * Initializing all the components
  43.          */
  44.         pnl = new JPanel();
  45.         /*
  46.          * We are initializing the TextField and passing an argument of 15 in
  47.          * the JTextField constructor to set the length of the TextField.
  48.          */
  49.         txtField = new JTextField(15);
  50.         /*
  51.          * Here JButtons are being initialized. The text passed as the argument
  52.          * in the constructor will be the title that we will be see on buttons.
  53.          */
  54.         btn1 = new JButton("Enable Text Field");
  55.         btn2 = new JButton("Disable Text Field");
  56.         btn3 = new JButton("Submit");
  57.  
  58.         /*
  59.          * adding all the components in the JPanel object and adding the JPanel
  60.          * object in the Frame
  61.          */
  62.         pnl.add(txtField);
  63.         pnl.add(btn1);
  64.         pnl.add(btn2);
  65.         pnl.add(btn3);
  66.         add(pnl);
  67.  
  68.         /*
  69.          * Now this one is new. What this code do is it registers the button
  70.          * components to the click events. But what we want to perform on these
  71.          * events will be done in a seperate method of ActionListener interface
  72.          * that we implemented in this class.
  73.          */
  74.         btn1.addActionListener(this);
  75.         btn2.addActionListener(this);
  76.         btn3.addActionListener(this);
  77.         /*
  78.          * In the above methods "this" is passed as an argument because the
  79.          * method of the ActionListener interface is in the same class. There
  80.          * are several methods to perform this functionality using multiple
  81.          * classes. Those will be discussed in the next 2-3 tutorials before
  82.          * discussing other event listeners.
  83.          */
  84.     }
  85.  
  86.     /*
  87.      * This is the method of the ActionListener interface. It will be called
  88.      * when we click on a button that is registered to the ActionListener
  89.      * events. i.e. btn1.addActionListener(this);
  90.      */
  91.     @Override
  92.     public void actionPerformed(ActionEvent e) {
  93.         /*
  94.          * What we are doing here is we are recogonizing the button clicked by
  95.          * the ActionEvent class's method getSource(). What this getSource()
  96.          * method do is recogonize which button was clicked and then we can
  97.          * perform the desired operation by recogonizing the compoment object
  98.          */
  99.         if (e.getSource() == btn1) {
  100.             txtField.setEnabled(true);
  101.         } else if (e.getSource() == btn2) {
  102.             txtField.setEnabled(false);
  103.         } else if (e.getSource() == btn3) {
  104.             String message = txtField.getText();
  105.  
  106.             if (message.equals("")) {
  107.                 JOptionPane.showMessageDialog(null, "Please enter something. You have left the field empty");
  108.             } else {
  109.                 /*
  110.                  * This is a way to show a message dialog box.
  111.                  * More of it will be discussed in a later tutorial
  112.                  */
  113.                 JOptionPane.showMessageDialog(null, "You entered: " + message);
  114.             }
  115.         }
  116.     }
  117.  
  118.     public static void main(String[] args) {
  119.         TextFieldAndButtonTutorial obj = new TextFieldAndButtonTutorial();
  120.         obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  121.         obj.setSize(700, 200);
  122.         obj.setResizable(false);
  123.         obj.setVisible(true);
  124.  
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement