• Register
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.

Most popular tags

java program php python javascript c# android r c spring-boot arrays html mysql entity-framework sql-server matlab redirect code program numpy spring asp loop net class quirks-mode internet-explorer-9 internet-explorer machine-learning winforms visual-studio-2010 webpack python-3 python-2 aspnet dataframe pandas jdbc ios eclipse sum loops css cmd tcp math security jquery sockets sql 7 number firebase-authentication aws-organizations amazon-web-services php-7 string-formatting cplusplus visual-studio-2015 net-core net-mvc msbuild extension-methods foreign-keys sql-server-2005 windows-services react-redux reactjs inputstream facebook-graph-api for-loop entity-framework-4 reportingservices-2005 reporting-services mips linear-regression scikit-learn anova deep-learning keras block-device melt reshape2 floating-point webpack-dev-server javascrip 0-lollipop android-5 statsmodels avx indentation-error eclipseide javafx-2 php-not-recognized laravel command-line unit-testing tinyurl atom-editor android-emulator android-studio android-sdk-tools ionic2 cordova jupyter-notebook foobar2k tcplistener net-2 net-4 ole-db-provider windows-10 vagrantfile ggplot2 glmnet jvm-arguments global-variables orm virtualenv atom xls oledb redux webclient prediction headless hyperlink outlook pipeline iis perfect-square objective-c carthage xcode8 compiler-errors numpy-ndarray standard-deviation xampp apple mdf destructor vagrant gettime gmail ioexception heuristics milliseconds reporting cpu npm modx-revolution goldsky modx prevnext javascript-dictionary stack-smashing device-monitor radio-button android-actionbaractivity android-activity android-fragments java-long unqualified-id ora-12154 javc c++ java nullpointerexception runtime-error drjava awt-eventqueue dsx math-pow ajquery nosuchelementexception appcompatactivity jtextfield jpanel inputmismatchexception deque jupyter javafx lvalue junit tensorflow ibm factorial servlet apache boot virtualbox indentation jvm margins 2147483647 xcode firebase plot deployment processor automation socketexception pointers concatenation oracle devices color recursion sequence search date caching expected response directory csv algorithms release collections facebook float figure url expression microsoft sorting sort datetime httpwebrequest cryptography json ajax exe dictionary required windows string variable size dll system exception files x int function random import code 2
0 votes
14 views
What is a JTextField?

What is the difference between JTextField and JTextArea?

What is the use of getText () in Java?
by (1.5k points)  
reshown by

8 Answers

0 votes

Java Swing | JTextField

by (8.9k 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.9k points)  
0 votes

Example of jtextField :

JTextField textField = new JTextField();
String userInput = textField.getText();
JLabel label = new JLabel(userInput);
by (8.9k 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.9k 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.9k 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.9k 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.9k 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.9k points)  
...