The class JTextField is a component which allows the editing of a single line of text.
Class Declaration
Following is the declaration for javax.swing.JTextField class −
public class JTextField extends JTextComponent implements SwingConstants
public class JTextField extends JTextComponent implements SwingConstants
Field
Following are the fields for javax.swing.JList class −
- static String notifyAction − Name of the action to send notification that the contents of the field have been accepted.
Class Constructors
Constructor & Description | |
---|---|
1 |
JTextField()
Constructs a new TextField.
|
2 |
JTextField(Document doc, String text, int columns)
Constructs a new JTextField that uses the given text storage model and the given number of columns.
|
3 |
JTextField(int columns)
Constructs a new empty TextField with the specified number of columns.
|
4 |
JTextField(String text)
Constructs a new TextField initialized with the specified text.
|
5 |
JTextField(String text, int columns)
Constructs a new TextField initialized with the specified text and columns.
|
Class Methods
Method & Description | |
---|---|
1 |
protected void actionPropertyChanged(Action action, String propertyName)
Updates the textfield's state in response to property changes in associated action.
|
2 |
void addActionListener(ActionListener l)
Adds the specified action listener to receive action events from this textfield.
|
3 |
protected void configurePropertiesFromAction(Action a)
Sets the properties on this textfield to match those in the specified Action.
|
4 |
protected PropertyChangeListener createActionPropertyChangeListener(Action a)
Creates and returns a PropertyChangeListener that is responsible for listening for changes from the specified Action and updating the appropriate properties.
|
5 |
protected Document createDefaultModel()
Creates the default implementation of the model to be used at construction if one isn't explicitly given.
|
6 |
protected void fireActionPerformed()
Notifies all listeners that have registered interest for notification on this event type.
|
7 |
AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this JTextField.
|
8 |
Action getAction()
Returns the currently set Action for this ActionEvent source, or null if no Action is set.
|
9 |
ActionListener[] getActionListeners()
Returns an array of all the ActionListeners added to this JTextField with addActionListener().
|
10 |
Action[] getActions()
Fetches the command list for the editor.
|
11 |
int getColumns()
Returns the number of columns in this TextField.
|
12 |
protected int getColumnWidth()
Returns the column width.
|
13 |
int getHorizontalAlignment()
Returns the horizontal alignment of the text.
|
14 |
BoundedRangeModel getHorizontalVisibility()
Gets the visibility of the text field.
|
15 |
Dimension getPreferredSize()
Returns the preferred size Dimensions needed for this TextField.
|
16 |
int getScrollOffset()
Gets the scroll offset, in pixels.
|
17 |
String getUIClassID()
Gets the class ID for a UI.
|
18 |
boolean isValidateRoot()
Calls to revalidate that come from within the textfield itself will be handled by validating the textfield, unless the textfield is contained within a JViewport, in which case this returns false.
|
19 |
protected String paramString()
Returns a string representation of this JTextField.
|
20 |
void postActionEvent()
Processes action events occurring on this textfield by dispatching them to any registered ActionListener objects.
|
21 |
void removeActionListener(ActionListener l)
Removes the specified action listener so that it no longer receives action events from this textfield.
|
22 |
void scrollRectToVisible(Rectangle r)
Scrolls the field left or right.
|
23 |
void setAction(Action a)
Sets the Action for the ActionEvent source.
|
24 |
void setActionCommand(String command)
Sets the command string used for action events.
|
25 |
void setColumns(int columns)
Sets the number of columns in this TextField, and then invalidate the layout.
|
26 |
void setDocument(Document doc)
Associates the editor with a text document.
|
27 |
void setFont(Font f)
Sets the current font.
|
28 |
void setHorizontalAlignment(int alignment)
Sets the horizontal alignment of the text.
|
29 |
void setScrollOffset(int scrollOffset)
Sets the scroll offset, in pixels.
|
30 |
void setEditable(boolean editable)
Sets the textfield to be editable or not
|
Methods Inherited
This class inherits methods from the following classes −
- javax.swing.text.JTextComponent
- javax.swing.JComponent
- java.awt.Container
- java.awt.Component
- java.lang.Object
JTextField Example
Create the following Java program using any editor of your choice in say D:/ > SWING > com > sjavaspot > gui >
JTextFieldDemo.java
// A program to demonstrate the use of JTextFields's
//Import Statements
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JTextFieldDemo extends JFrame {
//Class Declarations
JTextField jtfText1, jtfUneditableText;
String disp = "";
TextHandler handler = null;
//Constructor
public JTextFieldDemo() {
super("TextField Test Demo");
Container container = getContentPane();
container.setLayout(new FlowLayout());
jtfText1 = new JTextField(10);
jtfUneditableText = new JTextField("Uneditable text field", 20);
jtfUneditableText.setEditable(false);
container.add(jtfText1);
container.add(jtfUneditableText);
handler = new TextHandler();
jtfText1.addActionListener(handler);
jtfUneditableText.addActionListener(handler);
setSize(325, 100);
setVisible(true);
}
//Inner Class TextHandler
private class TextHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jtfText1) {
disp = "text1 : " + e.getActionCommand();
} else if (e.getSource() == jtfUneditableText) {
disp = "text3 : " + e.getActionCommand();
}
JOptionPane.showMessageDialog(null, disp);
}
}
//Main Program that starts Execution
public static void main(String args[]) {
JTextFieldDemo test = new JTextFieldDemo();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}// End of class JTextFieldDemo
Output
SwingControlDemo.java
package com.sjavaspot.gui; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SwingControlDemo { private JFrame mainFrame; private JLabel headerLabel; private JLabel statusLabel; private JPanel controlPanel; public SwingControlDemo(){ prepareGUI(); } public static void main(String[] args){ SwingControlDemo swingControlDemo = new SwingControlDemo(); swingControlDemo.showTextFieldDemo(); } private void prepareGUI(){ mainFrame = new JFrame("Java Swing Examples"); mainFrame.setSize(400,400); mainFrame.setLayout(new GridLayout(3, 1)); mainFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent){ System.exit(0); } }); headerLabel = new JLabel("", JLabel.CENTER); statusLabel = new JLabel("",JLabel.CENTER); statusLabel.setSize(350,100); controlPanel = new JPanel(); controlPanel.setLayout(new FlowLayout()); mainFrame.add(headerLabel); mainFrame.add(controlPanel); mainFrame.add(statusLabel); mainFrame.setVisible(true); } private void showTextFieldDemo(){ headerLabel.setText("Control in action: JTextField"); JLabel namelabel= new JLabel("User ID: ", JLabel.RIGHT); JLabel passwordLabel = new JLabel("Password: ", JLabel.CENTER); final JTextField userText = new JTextField(6); final JPasswordField passwordText = new JPasswordField(6); JButton loginButton = new JButton("Login"); loginButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String data = "Username " + userText.getText(); data += ", Password: " + new String(passwordText.getPassword()); statusLabel.setText(data); } }); controlPanel.add(namelabel); controlPanel.add(userText); controlPanel.add(passwordLabel); controlPanel.add(passwordText); controlPanel.add(loginButton); mainFrame.setVisible(true); } }
Compile the program using the command prompt. Go to D:/ > SWING and type the following command.
D:\SWING>javac com\sjavaspot\gui\SwingControlDemo.java
If no error occurs, it means the compilation is successful. Run the program using the following command.
D:\SWING>java com.sjavaspot.gui.SwingControlDemo
Verify the following output.
No comments:
Post a Comment