Welcome to Kodlogs Q&A, where you can ask programming questions and receive answers from other members of the community. For programmers, by programmers Kodlogs is an open community for anyone that does coding. We help you get answers to your toughest coding questions, share knowledge with your coworkers in private, and find your next IT dream job.
0 votes
11 views
What is a JTextField?

What is the difference between JTextField and JTextArea?

What is the use of getText () in Java?
in Java Programming by (2.5k points)
edited by

8 Answers

0 votes

Java Swing | JTextField

by (8.5k points)
0 votes

Source for javax.swing.JTextField

   1: /* JTextField.java --
   2:    Copyright (C) 2002, 2004, 2005, 2006  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
Examples:
private void myMethod () {
JTextField j =
  • imagenew JTextField()
  • imageString text;new JTextField(text)
  • imagenew JTextField(size)
by (8.5k points)
0 votes

Example of jtextField :

JTextField textField = new JTextField();
String userInput = textField.getText();
JLabel label = new JLabel(userInput);
by (8.5k points)
0 votes

JTextField Defination:

JTextField is a lightweight part that permits the altering of a solitary line of content. 

The object of a JTextField class is a content part that permits the altering of a solitary line content. 

Following is the revelation for javax.swing.JTextField class − open class JTextField 

broadens JTextComponent 

executes SwingConstant 

JTextFieldis a central Swing's segment that permits clients altering a solitary line of content. 

javax.swing.JTextField has two employments. 

Info. The client can enter one line of content (a String) 

Yield. To show one line of content. 

It is a lightweight part which enables clients to alter single line of content. JFormattedTextField, JPasswordField are inferred classes of JTextField. JTextField has a perfect segment java.awt.TextField. JTextField has abilities not accessible in java.awt.TextField.

by (8.5k points)
0 votes

Modifier and Type Field and Description

static String notifyAction

Fields inherited from class javax.swing.text.JTextComponent

DEFAULT_KEYMAP, FOCUS_ACCELERATOR_KEY

Fields inherited from class javax.swing.JComponent

accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW

Fields inherited from class java.awt.Component

BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT

Fields inherited from interface javax.swing.SwingConstants

BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST

Fields inherited from interface java.awt.image.ImageObserver

ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH

by (8.5k points)
0 votes

javax.swing.text.JTextComponent methods:

addCaretListener, addInputMethodListener, addKeymap, copy, cut, fireCaretUpdate, getCaret, getCaretColor, getCaretListeners, getCaretPosition, getDisabledTextColor, getDocument, getDragEnabled, getDropLocation, getDropMode, getFocusAccelerator, getHighlighter, getInputMethodRequests, getKeymap, getKeymap, getMargin, getNavigationFilter, getPreferredScrollableViewportSize, getPrintable, getScrollableBlockIncrement, getScrollableTracksViewportHeight, getScrollableTracksViewportWidth, getScrollableUnitIncrement, getSelectedText, getSelectedTextColor, getSelectionColor, getSelectionEnd, getSelectionStart, getText, getText, getToolTipText, getUI, isEditable, loadKeymap, modelToView, moveCaretPosition, paste, print, print, print, processInputMethodEvent, read, removeCaretListener, removeKeymap, removeNotify, replaceSelection, restoreComposedText, saveComposedText, select, selectAll, setCaret, setCaretColor, setCaretPosition, setComponentOrientation, setDisabledTextColor, setDragEnabled, setDropMode, setEditable, setFocusAccelerator, setHighlighter, setKeymap, setMargin, setNavigationFilter, setSelectedTextColor, setSelectionColor, setSelectionEnd, setSelectionStart, setText, setUI, updateUI, viewToModel, write

by (8.5k points)
0 votes

Method or Constructor

Purpose

JTextField()

JTextField(String)

JTextField(String, int)

JTextField(int)

Makes a content field. Whenever present, the int contention determines the ideal width in sections. The String contention contains the field's underlying content.

void setText(String)

String getText()
<!--[if !supportLineBreakNewLine]-->

<!--[endif]-->

Sets or obtains the text displayed by the text field.

 

by (8.5k points)
0 votes

Some helpful example and links:

  1. import javax.swing.*;  
  2. class TextFieldExample  
  3. {  
  4. public static void main(String args[])  
  5.     {  
  6.     JFrame f= new JFrame("TextField ");  
  7.     JTextField t1,t2;  
  8.     t1=new JTextField("JTextField.");  
  9.     t1.setBounds(50,100200,30);  
  10.     t2=new JTextField("JTextField");  
  11.     t2.setBounds(50,150200,30);  
  12.     f.add(t1); f.add(t2);  
  13.     f.setSize(400,400);  
  14.     f.setLayout(null);  
  15.     f.setVisible(true);  
  16.     }  
  17.     }  

setActionCommand(java.lang.String)JPasswordFieldaddActionListener(java.awt.event.ActionListener)
See How to Use Formatted Text Fields.
by (8.5k points)
...