JTextField numbers only but can set non-numbers by setTextRestricting JTextField input to IntegersJava JTextField with input hintWhy can't I draw an ellipse with this code?how to perform mouse on click action using threadsDrag and Drop nodes in JTreeJtextField with data validation and BeansbindingGridBagLayout not working correctlyJTextField not clearing after setText(“”)JLabel and JTextField setText is not updateJTextField setText() expensiveConverting String input to Integer from JTextField with Integer.parseInt but still receiving error messageTrying to only allow for numbers to be entered into JTextField and non-numbers be set as 0

How must one send away the mother bird?

How to express sadness?

Why do IPv6 unique local addresses have to have a /48 prefix?

Bob has never been a M before

Two-sided logarithm inequality

Can somebody explain Brexit in a few child-proof sentences?

Journal losing indexing services

Should I install hardwood flooring or cabinets first?

Is XSS in canonical link possible?

How do ground effect vehicles perform turns?

How should I respond when I lied about my education and the company finds out through background check?

We have a love-hate relationship

A social experiment. What is the worst that can happen?

Customize circled numbers

MAXDOP Settings for SQL Server 2014

Can a significant change in incentives void an employment contract?

Why has "pence" been used in this sentence, not "pences"?

Visiting the UK as unmarried couple

Why is Arduino resetting while driving motors?

Indicating multiple different modes of speech (fantasy language or telepathy)

Is there a word to describe the feeling of being transfixed out of horror?

How to align and center standalone amsmath equations?

Drawing ramified coverings with tikz

Proof of Lemma: Every nonzero integer can be written as a product of primes



JTextField numbers only but can set non-numbers by setText


Restricting JTextField input to IntegersJava JTextField with input hintWhy can't I draw an ellipse with this code?how to perform mouse on click action using threadsDrag and Drop nodes in JTreeJtextField with data validation and BeansbindingGridBagLayout not working correctlyJTextField not clearing after setText(“”)JLabel and JTextField setText is not updateJTextField setText() expensiveConverting String input to Integer from JTextField with Integer.parseInt but still receiving error messageTrying to only allow for numbers to be entered into JTextField and non-numbers be set as 0













0















I want to limit the JTextField to only enter numbers, but I can set the text to non-numbers via setText function.



Because I want to hint when there is no character in the JTextField.
This is the effect I want:When the JTextField does not get the focus, it has a hint text.



image



This is my code



import javax.swing.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

public class InputFieldFrame extends JFrame implements FocusListener
public static void main(String[] args)
new InputFieldFrame();


JTextField input = new JTextField(12);

public InputFieldFrame()
//input.setDocument(new NumberFilter()); // Remove the comment can just enter the number, but the hint function will fail.
//input.setText("Hint Text"); // This will not take effect
input.addFocusListener(this);
add(input);
setSize(new Dimension(200, 200));
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);


@Override
public void focusGained(FocusEvent e)
if (input.getText().equals("Hint"))
input.setText("");


@Override
public void focusLost(FocusEvent e)
if (input.getText().equals(""))
input.setText("Hint");


class NumberFilter extends PlainDocument
@Override
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException
if (str.charAt(0) >= '0' && str.charAt(0) <= '9')
System.out.println(str);
super.insertString(offs, str, a);













share|improve this question
























  • How about you implement an FocusListener which set/unsets the document?

    – XtremeBaumer
    Mar 7 at 9:01











  • i update the code that implement the hint function

    – Grapes
    Mar 7 at 9:13











  • Please refer to stackoverflow.com/questions/1738966/…

    – Miller Cy Chan
    Mar 7 at 9:21











  • @MillerCyChan Sorry, the way I set the hint is to learn from it, but this reference does not solve my problem (without introducing an external framework)

    – Grapes
    Mar 7 at 9:28






  • 1





    Try this, stackoverflow.com/questions/11093326/…

    – Tech Guy
    Mar 7 at 10:52















0















I want to limit the JTextField to only enter numbers, but I can set the text to non-numbers via setText function.



Because I want to hint when there is no character in the JTextField.
This is the effect I want:When the JTextField does not get the focus, it has a hint text.



image



This is my code



import javax.swing.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

public class InputFieldFrame extends JFrame implements FocusListener
public static void main(String[] args)
new InputFieldFrame();


JTextField input = new JTextField(12);

public InputFieldFrame()
//input.setDocument(new NumberFilter()); // Remove the comment can just enter the number, but the hint function will fail.
//input.setText("Hint Text"); // This will not take effect
input.addFocusListener(this);
add(input);
setSize(new Dimension(200, 200));
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);


@Override
public void focusGained(FocusEvent e)
if (input.getText().equals("Hint"))
input.setText("");


@Override
public void focusLost(FocusEvent e)
if (input.getText().equals(""))
input.setText("Hint");


class NumberFilter extends PlainDocument
@Override
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException
if (str.charAt(0) >= '0' && str.charAt(0) <= '9')
System.out.println(str);
super.insertString(offs, str, a);













share|improve this question
























  • How about you implement an FocusListener which set/unsets the document?

    – XtremeBaumer
    Mar 7 at 9:01











  • i update the code that implement the hint function

    – Grapes
    Mar 7 at 9:13











  • Please refer to stackoverflow.com/questions/1738966/…

    – Miller Cy Chan
    Mar 7 at 9:21











  • @MillerCyChan Sorry, the way I set the hint is to learn from it, but this reference does not solve my problem (without introducing an external framework)

    – Grapes
    Mar 7 at 9:28






  • 1





    Try this, stackoverflow.com/questions/11093326/…

    – Tech Guy
    Mar 7 at 10:52













0












0








0








I want to limit the JTextField to only enter numbers, but I can set the text to non-numbers via setText function.



Because I want to hint when there is no character in the JTextField.
This is the effect I want:When the JTextField does not get the focus, it has a hint text.



image



This is my code



import javax.swing.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

public class InputFieldFrame extends JFrame implements FocusListener
public static void main(String[] args)
new InputFieldFrame();


JTextField input = new JTextField(12);

public InputFieldFrame()
//input.setDocument(new NumberFilter()); // Remove the comment can just enter the number, but the hint function will fail.
//input.setText("Hint Text"); // This will not take effect
input.addFocusListener(this);
add(input);
setSize(new Dimension(200, 200));
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);


@Override
public void focusGained(FocusEvent e)
if (input.getText().equals("Hint"))
input.setText("");


@Override
public void focusLost(FocusEvent e)
if (input.getText().equals(""))
input.setText("Hint");


class NumberFilter extends PlainDocument
@Override
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException
if (str.charAt(0) >= '0' && str.charAt(0) <= '9')
System.out.println(str);
super.insertString(offs, str, a);













share|improve this question
















I want to limit the JTextField to only enter numbers, but I can set the text to non-numbers via setText function.



Because I want to hint when there is no character in the JTextField.
This is the effect I want:When the JTextField does not get the focus, it has a hint text.



image



This is my code



import javax.swing.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

public class InputFieldFrame extends JFrame implements FocusListener
public static void main(String[] args)
new InputFieldFrame();


JTextField input = new JTextField(12);

public InputFieldFrame()
//input.setDocument(new NumberFilter()); // Remove the comment can just enter the number, but the hint function will fail.
//input.setText("Hint Text"); // This will not take effect
input.addFocusListener(this);
add(input);
setSize(new Dimension(200, 200));
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);


@Override
public void focusGained(FocusEvent e)
if (input.getText().equals("Hint"))
input.setText("");


@Override
public void focusLost(FocusEvent e)
if (input.getText().equals(""))
input.setText("Hint");


class NumberFilter extends PlainDocument
@Override
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException
if (str.charAt(0) >= '0' && str.charAt(0) <= '9')
System.out.println(str);
super.insertString(offs, str, a);










java swing jtextfield






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 9:12







Grapes

















asked Mar 7 at 8:55









GrapesGrapes

175




175












  • How about you implement an FocusListener which set/unsets the document?

    – XtremeBaumer
    Mar 7 at 9:01











  • i update the code that implement the hint function

    – Grapes
    Mar 7 at 9:13











  • Please refer to stackoverflow.com/questions/1738966/…

    – Miller Cy Chan
    Mar 7 at 9:21











  • @MillerCyChan Sorry, the way I set the hint is to learn from it, but this reference does not solve my problem (without introducing an external framework)

    – Grapes
    Mar 7 at 9:28






  • 1





    Try this, stackoverflow.com/questions/11093326/…

    – Tech Guy
    Mar 7 at 10:52

















  • How about you implement an FocusListener which set/unsets the document?

    – XtremeBaumer
    Mar 7 at 9:01











  • i update the code that implement the hint function

    – Grapes
    Mar 7 at 9:13











  • Please refer to stackoverflow.com/questions/1738966/…

    – Miller Cy Chan
    Mar 7 at 9:21











  • @MillerCyChan Sorry, the way I set the hint is to learn from it, but this reference does not solve my problem (without introducing an external framework)

    – Grapes
    Mar 7 at 9:28






  • 1





    Try this, stackoverflow.com/questions/11093326/…

    – Tech Guy
    Mar 7 at 10:52
















How about you implement an FocusListener which set/unsets the document?

– XtremeBaumer
Mar 7 at 9:01





How about you implement an FocusListener which set/unsets the document?

– XtremeBaumer
Mar 7 at 9:01













i update the code that implement the hint function

– Grapes
Mar 7 at 9:13





i update the code that implement the hint function

– Grapes
Mar 7 at 9:13













Please refer to stackoverflow.com/questions/1738966/…

– Miller Cy Chan
Mar 7 at 9:21





Please refer to stackoverflow.com/questions/1738966/…

– Miller Cy Chan
Mar 7 at 9:21













@MillerCyChan Sorry, the way I set the hint is to learn from it, but this reference does not solve my problem (without introducing an external framework)

– Grapes
Mar 7 at 9:28





@MillerCyChan Sorry, the way I set the hint is to learn from it, but this reference does not solve my problem (without introducing an external framework)

– Grapes
Mar 7 at 9:28




1




1





Try this, stackoverflow.com/questions/11093326/…

– Tech Guy
Mar 7 at 10:52





Try this, stackoverflow.com/questions/11093326/…

– Tech Guy
Mar 7 at 10:52












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55039649%2fjtextfield-numbers-only-but-can-set-non-numbers-by-settext%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55039649%2fjtextfield-numbers-only-but-can-set-non-numbers-by-settext%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved