jtextfield

“append” text for JTextField

北城余情 提交于 2019-12-06 09:18:25
I'm trying to make a small application where U input ID (with help of JButton -from 0 to 9 - ) and then pass the number that have been pressed to putText-Method and then display it in JTextField-the problem is that every time I press new number the one I pressed before disapears:s . Could anyone help me with that pls? public class IdPanel extends JPanel { private JTextField idField; private JLabel idLabel; public IdPanel() { setLayout(new GridBagLayout()); setPreferredSize(new Dimension(500, 70)); idField = new JTextField(20); idField.setFont(new Font("Serif", Font.PLAIN, 20)); idLabel = new

Help on writing your own javax.swing.text.Document

寵の児 提交于 2019-12-06 08:24:08
问题 I'm writing a Java TextComponent where the underlying document has some structure. It is very short, basically one line. I need to be able to override the response to inserting or deleting characters in some parts of the document. My initial approach was to implement javax.swing.text.Document, but this seems to involve developing many associated classes (Element, EditorKit, View) and there don't seem to be many examples or tutorials on how to do this. There are plenty on using the pre-defined

How to create a KeyEvent

廉价感情. 提交于 2019-12-06 08:24:04
When i create the KeyListener , it requires the following fields: public void keyPressed(KeyEvent e) { } public void keyReleased(KeyEvent e) { } public void keyTyped(KeyEvent e) { } When i put System.out.println(e) into the keyPressed method, though, it returns this when i press the enter key: java.awt.event.KeyEvent[KEY_PRESSED,keyCode=10,keyText=?,keyChar=?,keyLocation=KEY_LOCATION_STANDARD,rawCode=0,primaryLevelUnicode=0,scancode=0] on javax.swing.JButton[,1,1,100x100,alignmentX=0.0,alignmentY=0.5,border=com.apple.laf.AquaButtonBorder$Dynamic@13b33a0e,flags=288,maximumSize=,minimumSize=

JTextField's setText method doesn't work from a KeyListener

邮差的信 提交于 2019-12-06 05:58:55
I'm puzzled as to why a JTextField doesn't seem to just "clear out" by using the setText("") method on it, when this is coming from a KeyListener. It works fine from an ActionListener, except that, most amazingly, if the KeyListener method tries to invoke the ActionListener method, with a dummy action event (created on the fly as a simple test), it still leaves the typed text in place. In other words, when you run it from the command line, if you type, for example, a "3" into the field, you will see the setText("test") method does not wipe out the 3, as I would expect and desire, but rather

setBorder on JTextField does not work ? or does it?

孤者浪人 提交于 2019-12-06 01:45:11
问题 The code is like this: JTextField txt = new JTextField(); txt.setBorder(BorderFactory.createMatteBorder(2,2,2,2,Color.red)); However the text field is ignoring my call to setBorder . No changes whatsoever. I were to replace it with a JLabel (for instance) JLabel txt = new JLabel(); txt.setBorder(BorderFactory.createMatteBorder(2,2,2,2,Color.red)); I would see the red border. Can anybody tell me why? Or even better explain to me how to add a border in the JTextField ? 回答1: Check out this

Make JTable cell editor value be selectable, but not editable?

霸气de小男生 提交于 2019-12-06 00:43:41
问题 I have tried to keep my JTable 's tight and secure, making only editable columns editable via isCellEditable() . However, my clients are insisting that they want to double click on a cell so they can copy its contents, even if it is read only. I could let the cell be editable and not do anything with any edits they could make in the setValueAt() (so it reverts back to original value when editor exits). But I don't want this application to feel so freeform. Is there an easy effective way to

Limiting Length of Input in JTextField is not working

…衆ロ難τιáo~ 提交于 2019-12-06 00:32:51
问题 I am trying to limit the max length of character a user can input in a textfield but It seems to be not working. Here is the code: text2 = new JTextField("Enter text here",8); Is there anything I am doing wrong? How can I make the limit to work properly? 回答1: The constructor new JTextField("Enter text here",8); sets the number of visible columns to 8 but doesn't restrict you from entering more. You could use a DocumentFilter to restrict the input length. 回答2: You current code is not setting

Filtering JList based on JTextField

烈酒焚心 提交于 2019-12-05 23:56:32
问题 I have a JTextField and a JList in my program. The JList contains the user's contacts. I'd like to filter the JList based on the text on the JTextField. For example, if I type in "Mike" it will only show contacts including "Mike". When the user clears the JTextField it would reset the filter. I know I could do this manually by having two arrays. One for the original contacts and one for the filtered ones. When the user changes the value of the JTextField I would go trought the original list,

Override VK_Tab Focus action

余生长醉 提交于 2019-12-05 23:30:47
Good Day! I am tying to add keyevent listener to jTextField so that if the user pressed the tab key, the caret position will go to the end of the text inside the jtextField, here is my code: private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) { if(evt.getKeyCode()==KeyEvent.VK_TAB){ evt.consume(); jTextField1.setCaretPosition(jTextField1.getText().length()); } } But it doesn't work. How can i make this happen? Hovercraft Full Of Eels One way is to: First of all, don't use a KeyListener since this is a Swing application, and you shouldn't use KeyListeners in Swing apps if it can be

How to make a Round Rectangle JTextField?

微笑、不失礼 提交于 2019-12-05 19:46:34
I want to make a round rectangle JTextField. I write a sub class of AbstractBorder to realize it.But I run into some problems. My requirement is: What I get is: My code is : import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Font; import java.awt.Graphics; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import