FAQ Database Discussion Community


Converting from String to Int

java,int,type-conversion,jtextfield,joptionpane
I'm trying to validate my program by entering a value through a JTextField on a JDialog and if it's less than a value..., else do ... I keep running into a problem on the line: int intDiagInput = Integer.parseInt(dialogInput) JOptionPane.showInputDialog(dialogPaneInput, "Age verification required, please enter year of birth: yyyy"); dialogInput...

How to disable automatic focus on textField

java,focus,jtextfield
When I was creating a bunch of JTextFields I saw that first one is selected. I want to deselect it, because I have focus listener and it's running automatically. Any clues? SSCCE: JTextField tf = new JTextField("hello"); tf.setForeground(Color.decode("0x8C8C8C")); // for nice comment inside the text field textFieldKwotaWplacona.addFocusListener(new FocusListener() { @Override...

Importing text into a JTable is not displaying

java,swing,jtable,jtextfield
I have created a program where I can input data in JTextField and on hitting save button I use a JFileChooser to save the data in a .txt file where each JTextField is in a new line. I also created a button that pops up a JFileChooser to browse for...

try/catch block not working in actionlistener

java,user-interface,try-catch,actionlistener,jtextfield
I am working on 2 programs for school, one to take contact info and save it to a text file and the other is to read from the text file. All works perfectly but I have to add a try/catch block the program that takes input so that it would...

Java Swing - How to get the value of text in a TextField from another class

java,swing,scope,jtextfield
Okay, beginner Java coder here. I'm trying to make a multi-purpose math utility application with Java Swing. One of the things I want it to do is be able to solve basic logarithms. I have the logic all down, but I'm having trouble with the outputting itself. I have a...

Putting text from JTextField to JLabel

java,swing,jlabel,jtextfield
I 'm working on code that takes a string from a JTextField and puts it on a JLabel. But my problem is always the string shown is missing the last character. private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) { String ch=jTextField1.getText(); jLabel1.setText(ch); } ...

How to properly add JTextField components in the array?

java,arrays,swing,jtextfield
I have a panel that contains different components (JLabel, JButton, JTextComponent, etc). I want to get the list of JTextField in this way: ArrayList<JTextField> arrayTf = new ArrayList(); Component[] arrayComponent = this.getComponents(); for (Component c : arrayComponent ) { if (c instanceof JTextField ) { arrayTf.add(c); } } But I'm...

How can I create a generic setter for JTextField?

java,swing,jtextfield
I am able to expose a private JTextField by doing this: public void setTextField(String value) { someTF.setText(value); } It would be a lot of work if I have a lot of JTextFields. I tried doing this but failed. No error it's just not setting the right value on specified JTextField....

Remove InputVerifier from JTextField

java,swing,jtextfield,inputverifier
I'm really surprised this is not in the documentation or at least google. I have a class that is likely to need to remove the verifier or replace it with another one. In particular, these methods are defined in the interface: /** * Add the verifier */ public void bind();...

How to keep Focus on JTextField while clicking input number buttons?

java,swing,jtextfield,jtextarea
I am new to GUI stuff and am having trouble with the following problem. I have 3 JTextFields, credit card number, expiration date, and security number. I am able to input information into the fields. I also implemented the focus listener for each button. If I click it, it says...

How to call `setText` on multiple JTextfields?

java,swing,jtextfield,boilerplate
I want to figure out a way to set the text for multiple JTextfields in just a few lines (preferable one line) of code rather than a new line for every textfield. I am looking for a way to invoke the setText method on multiple JTextfield values. My code works...

(Java Swing) JTextField updates “too late”

java,swing,jtextfield,keylistener
This is a code sample from a feet<->meters converter. My problem: It doesn't update the current input, for example: If the text in the JTextField is "50", the textField.getText()only gives the String "5", so the double result would be 5.0. As a consequence, a "5" in the JTextField gives an...

JTextField - how to determine which values can be typed

java,swing,jtextfield
I developing some calculator, so in the JTextField above can have only some characters. What is the best way to make that happened? Let's say that I have this char[] values = 0,1,2,3,4,5,6,7,8,9,+,-,*,/,(,),., which those are the values which the user can type....

Clear current FocusOwner (jTextfield)

java,swing,jtextfield
Developing an application in swing, just a little query :- I want to clear the current focus owner textfield using a button. It is possible to determine whether a textfield is the current focus owner or not using isFocusOwner() but how to clear the textfield which is currently on...

passing multiple JTextFields to a boolean method

java,methods,jtextfield
Method FourOfAKind is supposed to test 4 JTextFields and check if they all equal each other. Each JTextField excepts one number. When I press the ButtonListener the boolean method FourOfAKind doesn't respond, even if it returns false, when I enter numbers 5555 ex. import java.awt.*; import java.awt.event.*; import javax.swing.*; /**...

How to make sure that JTextField contains only letters?

java,regex,string,swing,jtextfield
I want to take only letters as a input in my name field. I have already tried using the matches method but unfortunately something is wrong and the exception is being thrown. Is there any other method of checking the same? import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import...

In Java, how can I avoid blank jtextfield input?

java,null,jtextfield
Due to a gui with a lot of buttons and labels, I'm using netbeans. When I test-run it in netbeans, if I leave the jtextfield blank and submit it, I get: Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1 I was looking at the jbutton I am using to submit the data...

Clearing text on mouse click from multiple JTextFields in Java

java,swing,focus,jtextfield,mouselistener
I'm doing some error checking in two JTextFields. I'll label them jtext1 and jtext2. Right now my GUI looks like this: Enter number 1: [ ] // jtext1 Enter number 2: [ ] // jtext2 Convert [ answer ] I want to be able to check for multiple conditions and...

How to check for empty text field?

java,swing,jtextfield,empty-string
I'm building a tool to generate a script of commands for integration purposes for some equipment using Java Swing. So basically the tool takes the user inputs in some text fields and then build the needed commands with the user variable. At the end of my script I'm displaying the...

Overlapping in JDialog w/ GridBagLayout when setting gridwidth

java,jtextfield,gridbaglayout
I am trying to create a JDialog that looks like this: but where the JTextField spans two columns so that it almost reaches the "Find Next" button. However, when I try to set the gridwidth of the JTextField to 2, I get this: Here is my code: JDialog dialog =...

JTextField with regex applied won't accept value until AFTER the Document is applied

java,regex,swing,numbers,jtextfield
I have a JTextField that I have overridden the Document for, so that I can prevent the user from entering some characters. The way this works is my extension of Document receives a Regex in its constructor, then checks anything the user types against the Regex: public class ValidDocument extends...

Customized JTextfield - Only Integer with Limitation

java,swing,jtextfield
I have a class which only allows integers with limited amount. The problem is, class is doing its work but when I use multiple objects, it only takes the last objects limitation number and applies to others. I also couldn't get rid of static warnings. Code is ; public class...

Prevent breaking of JTextField / JDialog with repeated transferFocus()

java,swing,jtextfield,jdialog
I have a very strange scenario which unfortunately I cannot prevent from occurring in my Swing application. When it occurs however, it has major consequences for me. Perhaps somebody could help! The basic setup is as follows: Linux environment. Multiple JTextFields in a JFrame. JTextFields push through transferFocus() when the...

Maximum value on JTextField

java,swing,limit,jtextfield
I want to have a JTextField in which you can only type numbers (integers and decimals), and you can only type numbers below 12345 and above 0. How would I do this? What I have right now: JTextField tf = new JTextField(); final PlainDocument doc = new PlainDocument(); doc.setDocumentFilter(new DocumentFilter()...

Calculate Dice Roll from Text Field

java,random,eval,jtextfield,dice
QUESTION: How can I read the string "d6+2-d4" so that each d# will randomly generate a number within the parameter of the dice roll? CLARIFIER: I want to read a string and have it so when a d# appears, it will randomly generate a number such as to simulate a...

How in Java I can create a field that can accept only digits and once dot?

java,regex,swing,jtextfield,documentfilter
I look all over the net and all the things I tried didn't work.... I found that I can use some JTextFormatterField but it didn't work. Then I found that I can use DocumentFilter with regex and that what I did: JTextField jFormattedTextFieldMoneyToConvert = new JTextField(); ((AbstractDocument) jFormattedTextFieldMoneyToConvert.getDocument()).setDocumentFilter(new DocumentFilter(){ Pattern...

Checking Text Backspace Issue

java,swing,jbutton,jtextfield,documentfilter
I made it where a button is disabled and the only way to enable it is to type text into a field. Here is my code: import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.text.AbstractDocument; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.DocumentFilter; class checkText extends DocumentFilter { public void replace(DocumentFilter.FilterBypass...

How can I map an action for option (Mac keyboard) up for a JTextField?

java,osx,swing,awt,jtextfield
Looks like option up can be caught only with AWT (i.e., adding key listener). Is there a way to do it without having to mess around with AWT?

Using an itemListener with JCheckBox to show/hide JTextField

java,jtextfield,jcheckbox,itemlistener
I'm trying to create an application that allows a user to choose insurance options in JCheckBoxes. For each option that is selected, the name and price are supposed to appear in a text field. My problem is that even when I select it, it doesn't display the Name & Price....

Java set JTextField position

java,swing,user-interface,jframe,jtextfield
how can I position a JTextField? I have searched through many examples but unfortunately none worked for me. Can someone help me? I want to position it using absolute coordinates. Code: public static class TextDemo extends JPanel implements ActionListener { protected JTextField textField; static public String text; public TextDemo() {...

Android: Why TextField is not showing?

java,android,textview,jtextfield
TextView t = new TextView(this); t.setX(0); t.setY(0); t.setText("Tap"); t.setTextColor(Color.GREEN); t.setTextSize(25); t.setLayoutParams(new FrameLayout.LayoutParams(100, 100)); setContentView(t); I'am in ActionBarActivity, that why I'am using "this" as context, can't understand why the textField isn't showing, is here something wrong?...

Jtextfield doesn't show up on launch. but after resize, yes

jtextfield
Well i need a little help. My intention of this code is to make a display of a calculator with buttons and a textfield. Im thinking of using a borderlayout where it will have a panel up north where the textfield will be and a panel south where buttons will...

Resetting the background color of jtextfield that has been changed

java,jtextfield,background-color
I have a set of jtextfields. Now, there are two different background colors i've used, the null and white. I also have a highlighter which changes the clicked textfield (given that it is enabled) to green when focused. My problem is when the focus is lost, I cant seem to...

How to stop this java.lang.ArrayIndexOutOfBoundsException?

java,event-handling,jtextfield
I'm trying validate my jtextfeild to enter only money value. which include only numeric and a full-stop. ex-17652.50 So I tried this method. But while it is executing I got this java.lang.ArrayIndexOutOfBoundsException: 1 Here is the method. private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) { try { char c = evt.getKeyChar(); String mny[]...

Change jTextField from another class

java,swing,class,jtextfield
I have two different classes which creates two Panel's A & B. B is initiated after a button is click on A. I want to change the a jTextField in B, when a button is clicked on A. Code of A private final B myB = new B(); public static...

How to get text from JTextField to JLabel that is in another JFrame?

java,swing,oop,jlabel,jtextfield
I am making an application in Netbeans and I would like to get the text from the jTextField and set it to a jLabel that is in another jFrame (not the same!) I did this: jLabel.setText(jTextField1.getText()); But it doesn't work. And what event method should I use? actionPerformed?...

Adding a JTextField into a JOptionPane: ShowOptionDialog

java,dialog,jtextfield,joptionpane
I was wondering if it is possible to add a JTextField to a ShowOptionDialog box. int optionChosen = JOptionPane.showOptionDialog(finishPayInput, dialogPanel, "The Title", JOptionPane.NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options , options[0]); When I run the program, the dialog box shows, but the JTextField doesn't display....

Update a JTextArea dynamically depending on users JTextField values

java,swing,jtextfield,jtextarea
I have two JTextField which take numbers from users, like this: nbMuscle = new JTextField(); nbMuscle.setText("2"); and this: nbFuyard = new JTextField(); nbFuyard.setText("1"); my JTextArea() takes make an addition of both JTextField's values, like this: nbPersonnages = new JTextArea(); int nombMusc = Integer.valueOf(nbMuscle.getText()); int nombFuy = Integer.valueOf(nbFuyard.getText()); int nbTotal =...

how to retrieve data from multiple JTextField

jtextfield
we are creating a project in netbeans which needs to read data from 3 JTextFields in order for us to use it in equations to solve a problem here the part we already finished: package combustionofgaseousfuels; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CombustionOfGaseousFuels extends JFrame { private JTextField...

calling a method to restart if a condition is met without using a loop

java,user-interface,oop,jtextfield,jtextarea
I am working on a payroll program for school. I only two weeks into working with GUIs so my skills have a lot of room for growth. Everything works fine except for this method. I am trying to get the method to clear the JTextAreas if the number entered is...

Swing remove JTexField and JLabel

java,swing,jtable,jtextfield,jcheckbox
I have a JTable 2 columns, column name and check-box. If the user clicks the check box next to the name of the column that creates a new field for data entry. I wish the unclick check box input field has disappeared. How can I do that? My code to...

why am i appending twice in jtextfield?

java,swing,jtextfield,keylistener
I have looked over this code and i don't know what is wrong. I keep getting incorrect outputs when i enter a command (any input). Please look at the bottom part of my code. public class gui { private final static javax.swing.JFrame frame = new javax.swing.JFrame(); private final static javax.swing.JPanel...

tow jtextfiled Addition to another Jtextfilde immeditly without action button

jtextfield
I create Jframe with 3 Jtextfield 1-first value 2-second value 3-result I need make operation between first value and second value to result without any button just once I insert number in the second value the result show immediately ...

Add user input to JFrame [closed]

java,debugging,jframe,user,jtextfield
How can I make a JFrame have an option for user input, that can also detect a certain input and do something afterwards? Is it something along the lines of JTextField?

Java getText of looped multiple JTextFields

java,for-loop,jtextfield,user-input,getter
My first question here. Already got a lot of help but now I don't know how to do. My code: package view; import javax.swing.*; public class OptionPlayerNames { JPanel playerPanel = new JPanel(); JTextField playerNames = new JTextField(); public OptionPlayerNames() { for (int i = 0; i < 8; i++)...

JTextField Outputting

java,swing,jtextfield
How do you output a number in the JTextField? I need to add age when inputs and display it back to him. age = new JTextField(); age.setBounds(10, 133, 108, 20); contentPane.add(age); age.setColumns(10); textArea = new JTextField(); textArea.setBounds(10, 198, 298, 20); contentPane.add(textArea); JButton btnDisplayWelcome = new JButton("Submit"); btnDisplayWelcome.addActionListener(new ActionListener() { public...

Java - JTextField fills all frame

java,swing,jframe,jtextfield
I am trying to achieve a very simple task: To make a JTextField that doesn't fill all screen. Currently I see this possible only with setMaximumSize, is there any other way? Here's my code: JFrame window = new JFrame(); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); window.setSize(screenSize.width / 2, screenSize.height / 2);...

JTextField Everything looks ok in Eclipse but won't compile

java,swing,awt,jtextfield
Can someone please tell me what's wrong with this code? I need it to pop up a frame and fill the frame with certain field a namer field and an intake field and also display a button at the bottom of the grid. Any help would be welcomed, Thanks!! import...

JTextField from separate class

java,jtextfield
Can anyone tell me why I keep getting a Null pointer exception for this. I have a JTextField in one class with a set and get method and in my second class i want to assign the JTextField value to a string variable. What am i missing here? Heres the...

declaring a variable textfield

java,jtextfield
I am trying to declare a JTextField with a variable name For a fixed JTextField i would simply declare it after my public class as private JTextField HH1; but with a variable text field i am trying to create them within int count = 1 HH + count++ = new...

Non-Static Variable Cannot be Referenced From a Static Context: New to GUIs

java,user-interface,variables,static,jtextfield
Whenever I compile this, I get an error reading "non-static variable french cannot be referenced from a static context". I have just started working with GUIs, but I cannot find an answer to this. I'm sure it's something simple, but I cannot figure it out! Any help would be very...

NumberFormatException when trying to convert JTextField to Integer

java,user-interface,object,constructor,jtextfield
I've tried many things and I still can't seem to fix this error no matter what so I'm going to ask my own question. I keep believing that the error is at the ActionEvent where I try to convert the JTextfield into an int but it might be something else?...

Formatting JTextField to accept three digits at most, but anything up to 1-3 digits can be typed

java,swing,jtextfield,jformattedtextfield
I have the following JFormattedTextField try { MaskFormatter CabinNoTextF = new MaskFormatter("###"); CabinNoTextF.setPlaceholderCharacter(' '); CabinNoTextField = new JFormattedTextField(CabinNoTextF); centerPanelCabin.add(CabinNoTextField); addCabinF.add(centerPanelCabin); addCabinF.setVisible(true); } catch (Exception ex) { } CabinNoTextField is formatted to only allow three digits to be inputted within the text field. However, I want it so that the user...

Cannot set JTextField but can Output Variable

java,user-interface,netbeans,jtextfield
I made a similar question before, but this one is more peculiar. Simply put, I have two classes, in the first class (DefineEquationQuadratic) I am passing a value to SelectSolutionQuadratic. In SelectSolutionQuadratic I am trying to set a TextField to display this value, but it does not work. However, what...

how do I filter what I entered in the JTextField and display the filtered result?

java,string,jframe,jtextfield,indexoutofboundsexception
package javaisnotbannana; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import javax.swing.*; public class Javaisnotbannana{ public static void main(String[] args) { window(); } //////////////////////////////////////////////////// public static void window() { JFrame window= new JFrame(); JPanel jp = new JPanel(); JLabel jl = new JLabel(); JTextField jt = new JTextField(30); JButton jb = new...

Take different actions depending on jTextField value

java,if-statement,jtextfield
I have many more cases, but I'm wondering if there is a simpler way to do this. If the user enters 1 the program will convert from inches to cm, if the user enters 2, the program will convert from cm to feet, etc. if (jTextField1.getText() == 1) { InchesToCm();...

getText for JTextField input after action event

java,swing,actionlistener,jtextfield
I am a complete newb to Java and I don't quite understand why I am not able to getText() from the JTextFields I have created. I want to be able to create a new User with the email and password that has been input, but I am getting a nullPointerException...

How to enter restricted characters in input field

jquery,html,validation,jtextfield
I am using jQuery method to block certain character in input field like #$%^&*() Now,How do I input these character in input field (how to hack this input field). $('#textinput').keypress(function (e) { var regex = new RegExp("^[[email protected]]+$"); var str = String.fromCharCode(!e.charCode ? e.which : e.charCode); if (regex.test(str)) { return true;...

How to check if there is a space between two characters?

java,string,swing,char,jtextfield
I'm new to Java programming and I'm now developing a simple application. Now I'm focused on finding a solution on this problem: checking if a String contained in a JTextField contains spaces between each of its characters, like: "1 + 2 - 3 + 7" (which has spaces between every...

Formatting of components in Swing

java,swing,jbutton,jtextfield
I have just started programming in Swing. I am making a Fitness application using Swing. I have 2 questions: Whenever I click 'Calculate Scales' it should display the results only once, but it is displaying as many times as I click. How can I solve this? How can I set...

Toogle Caps Lock On Tooltip on JTextFiled

java,swing,tooltip,jtextfield
I want to warn a user when caps lock is on the way it is done on Windows when login. I have a JTextField where this should be shown. I have searched forums and found out that the check if the key is on or off we use Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK). However,...

How to pass JTextField from JFrame into another JFrame?

java,swing,jtextfield
I have two JFrame (JFrame1 and JFrame2) with two JTextField1 and JTextField2. My question is when I write "Hello world " on JTextField2 from Jframe2 and then click on OK button, I see "Hello world " on JTextField1 on Jframe1 class. How can I do this? I'm sorry if this...

Change Listener for a JTextfield

java,swing,user-interface,listener,jtextfield
I made a program that dynamically gets data from a Panel but my code needs the user to hit the enter button for the data to be updated. Is there a change listener or other listeners that can update the data from a Jtextfield whenever it is updated? Thanks!

Java: Add Place Holder on JTextField

java,swing,jtextfield,prompt
Guys is there a way or method in which we can add placeholder in j text field.I want to add placeholder "Enter Your Number" in field but how can i do this.I check all methods but didn't working. Code: public class Loop extends JFrame{ private JTextField t1; public L(){ getContentPane().setLayout(null);...