Swing Tutorial - 03 - Creating a TextField and Button and learning Event Listeners

In this tutorial I have tried a new way to explain the code. I have commented the code heavily where ever required. You can get to know that particular method is doing just by reading the comment above it.
JButton: This is a class that is used to draw a button on the Panel. On click of this button we perform many different operations such some calculations, Database operations, start/stop an event etc etc.
These are one of the most used Swing components.

JTextField: Another important Swing component that we need in many purpose of entring any text information.

EventListeners: Event Listeners are a way to get a task done on some user click or any user event. It is a large topic so it will discussed with time as these tutorials are made further.



import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class TextFieldAndButtonTutorial extends JFrame implements
ActionListener {

/*
* Creating instances of text fields, buttons and a panel that we will use
* in this tutorial
*/
JTextField txtField;
JButton btn1, btn2, btn3;
JPanel pnl;

/*
* In this constructor we will initialize all the UI components and them to
* the Panel. One new thing that we will be doing is the Event Handling in
* this tutorial. Events are basically actions that we want the program to
* perform when we click on a button. For this tutorial we will cover just
* one type of event listener which is used for clicking events. Its is
* called "ActionListener".
*/
public TextFieldAndButtonTutorial() {
/*
* This super calls the constructor of JFrame class that we 
                   extended and
* arguments passed in it set that as the title of the window.
*/
super("A simple TextField and Button tutorial");
/*
* Setting the Layout as simple FlowLayout
*/
setLayout(new FlowLayout());

/*
* Initializing all the components
*/
pnl = new JPanel();
/*
* We are initializing the TextField and passing an argument of 15 
                   in
* the JTextField constructor to set the length of the TextField.
*/
txtField = new JTextField(15);
/*
* Here JButtons are being initialized. The text passed as the 
                   argument
* in the constructor will be the title that we will be see on 
                   buttons.
*/
btn1 = new JButton("Enable Text Field");
btn2 = new JButton("Disable Text Field");
btn3 = new JButton("Submit");

/*
* adding all the components in the JPanel object and adding the 
                   JPanel
* object in the Frame
*/
pnl.add(txtField);
pnl.add(btn1);
pnl.add(btn2);
pnl.add(btn3);
add(pnl);

/*
* Now this one is new. What this code do is it registers the   
                   button
* components to the click events. But what we want to perform on 
                   these
* events will be done in a seperate method of ActionListener 
                   interface
* that we implemented in this class.
*/
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
/*
* In the above methods "this" is passed as an argument because the
* method of the ActionListener interface is in the same class. 
                   There
* are several methods to perform this functionality using multiple
* classes. Those will be discussed in the next 2-3 tutorials 
                   before
* discussing other event listeners.
*/
}

/*
* This is the method of the ActionListener interface. It will be called
* when we click on a button that is registered to the ActionListener
* events. i.e. btn1.addActionListener(this);
*/
@Override
public void actionPerformed(ActionEvent e) {
/*
* What we are doing here is we are recogonizing the button clicked 
                   by
* the ActionEvent class's method getSource(). What this 
                   getSource()
* method do is recogonize which button was clicked and then we can
* perform the desired operation by recogonizing the compoment 
                   object
*/
if (e.getSource() == btn1) {
  /*
  * Enables the text field.
  */
txtField.setEnabled(true);
} else if (e.getSource() == btn2) {

  /*
  * Enables the text field.
  */

txtField.setEnabled(false);
} else if (e.getSource() == btn3) {
String message = txtField.getText();

if (message.equals("")) {
JOptionPane.showMessageDialog(null, "Please enter something. You have left the field empty");
} else {
/*
* This is a way to show a message dialog box.
* More of it will be discussed in a later tutorial
*/
JOptionPane.showMessageDialog(null, "You entered: " + message);
}
}
}

public static void main(String[] args) {
TextFieldAndButtonTutorial obj = new TextFieldAndButtonTutorial();
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
obj.setSize(700, 200);
obj.setResizable(false);
obj.setVisible(true);

}
}


Normal frame with text entered in the JTextField

On Click of the Disable TextField button it got dissabled

On submit we can see the result in a Dialog of the text written in the textfield.
It shows text irrespective of the textfield is enabled or disabled


For clear code here is the pastebin link of this tutorialhttp://pastebin.com/pe10Kj7b

Any question and queries ask in the comments section. 

Comments

Post a Comment

Popular posts from this blog

What happened to the blog ?

Why I love to be a programmer ?