jtextfield

JTextField listener when text changes that modifies textField's text

时光总嘲笑我的痴心妄想 提交于 2019-12-22 10:26:36
问题 Any idea how I can add a text listener on to a JTextField, that triggers when the text changes and as an action modifies the JTextField's text. I have tried using addInputMethodListener which seems to be appropriate but it doesn't seem to work. I have also tried textField.getDocument().addDocumentListener() but this throws java.lang.IllegalStateException: Attempt to mutate in notification when I try to modify textField's text. 回答1: DocumentListeners do not permit the modification of the

Simple way to cancel user input on esc key pressed?

我怕爱的太早我们不能终老 提交于 2019-12-22 08:59:33
问题 Is there a simple way to cancel the user input in a JTextField when key Esc is pressed ? I mean something different that a key listener and a data backup. Thanks 回答1: Add a KeyListener to your JTextField: JTextField field = new JTextField.addKeyListener(yourKeyListener); where yourKeyListener could be: public class YourKeyListener implements KeyListener{ void keyPressed(KeyEvent e){ Component source = e.getSource(); if (source instanceof JTextField && e.getId() == KeyEvent.VK_ESCAPE ){

Click on icon in a JTextField and clear its content

旧巷老猫 提交于 2019-12-22 08:42:10
问题 I am trying to create a JTextField with an image and a hint. The function of the textfield is a search field to search some books. Now, I like to go a little bit further. I would like to give the image a function. For example, if I click on the image the text in the textfield should be cleared. To achieve this implementation I created a new class and extended it with JTextField. This is the code: public class JSearchTextField extends JTextField implements FocusListener { /** * */ private

Document filter is not working in Java?

大兔子大兔子 提交于 2019-12-21 16:55:20
问题 In text field more than 10 characters na, it has to show an error. For that i used document filter: JTextField field = (JTextField) txtFld; AbstractDocument document = (AbstractDocument) field.getDocument(); document.setDocumentFilter(new DocumentSizeAndUppercaseFilter(10)); So this is my document filter coding. I registered the textfield through document filter. But nothing has happening here. How to use document filter? DocumentSizeAndUppercaseFilter class which has the error msg. 回答1:

JTextArea and JTextField internal padding on text

血红的双手。 提交于 2019-12-21 09:36:11
问题 I would like to increase the spacing / padding / insets for the JTextField and JTextArea. Effectively increase the spacing between the two red lines in the image below: 回答1: You could also try using an EmptyBorder to put in gaps between the two components. If you already have a Border, you can use that in conjunction with an EmptyBorder to create a CompoundBorder. In the code fragment below, a new CompoundBorder is created that has a TitledBorder and an EmptyBorder which enforces a padding of

Disabling 'paste' in a jTextfield

非 Y 不嫁゛ 提交于 2019-12-21 05:07:41
问题 I have an app that is written in Swing, awt. I want to prevent users from pasting values into the textfields. is there any way to do this without using action listeners? 回答1: You can just call setTransferHandler with a null parameter like this: textComponent.setTransferHandler(null); This will disable all copy/paste actions on the field. 回答2: The best way is to remove action associated with CTRL+V keystroke in components ActionMap. 回答3: The simplest way it to say: textComponent.setEditable

Swing: taking variables from two private jTextField?ActionPerformed classes

让人想犯罪 __ 提交于 2019-12-20 07:48:20
问题 I'm in NetBeans 8.2 and I've constructed a nice jFrame GUI for a Binary-to-Decimal converter, I think I have the code in my two text fields correct but I can't figure out how to access the variables for my Button to perform operations on. I have 3 method operations called parseBinary, isBinary, and illegal that I want to use to perform operations on my two text fields. In NetBeans when you code a button the method is locked to private so that's my issue. I will post all my of code and

change the size of JTextField on KeyPress

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-20 06:43:46
问题 I have to extend the size of JTextField on KeyPressed event as user enter the text in textfield.please give me some idea how to achieve this? thanks in advance 回答1: Okay, jumping for it :-) Let's assume the question is How to adjust a JTextField's width to always fit its content width? Usual collaborators a LayoutManager which sizes its children at their pref size, f.i. FlowLayout JTextField reports its pref size fitted to content auto-magical resize expected Quick example: JTextField field =

Transparency for JTextField not working

眉间皱痕 提交于 2019-12-20 06:37:37
问题 I'm working on a log in server & my JTextFields aren't transparent when I set Opaque to false . My code: //username JTextField jUsername = new JTextField(10); jUsername.setBounds(520, 284, 190, 25); jUsername.setOpaque(false); jUsername.setBorder(null); getContentPane().add(jUsername); //password JTextField jPassword = new JTextField(15); jPassword.setBounds(520, 374, 190, 25); jPassword.setOpaque(false); jPassword.setBorder(null); //jPassword.setBackground(new Color(Color.TRANSLUCENT));

How to exit a method in ActionListener

戏子无情 提交于 2019-12-20 06:28:10
问题 I have an ActionListener connected to a JTextField and want to type something so that it will exit the method the ActionListener is in. Code: main() { Security(x,x,x); } public void Security(JTextArea out, JTextField in) { in.setText(""); in.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { if (in.getText().contains("exitsys")) { out.append("Security:Security System Deactivated\n"); return; } in.setText(""); } }); out.append("Security:Security System