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

1928 у кіно

Захаров Федір Захарович

Ель Греко