jtextfield

How do I align text within a JTextField?

喜欢而已 提交于 2020-01-03 13:15:15
问题 I have a JTextField with rather big bounds, the text however does not behave like I want it to. _________________ | | | | |text | | | |_________________| how would i do it so my text aligns like this _________________ |text | | | | | | | |_________________| Edit: Using JTextArea fixed my problem. Thank you. 回答1: JTextArea aligns to the top. Or use a JLabel : JLabel myLabel = new JLabel("my text"); and the call: myLabel.setHorizontalAlignment(SwingConstants.LEFT); myLabel.setVerticalAlignment

JTextField Specific format checking

风流意气都作罢 提交于 2020-01-03 05:56:08
问题 I want to check if the input entered to my JTextField1 equal to shown sample picture below, how to do that? I can only check if numbers entered by putting below code in to try block and catch NumberFormatException try { taxratio = new BigDecimal(jTextField1.getText()); } } catch (NumberFormatException nfe) { System.out.println("Error" + nfe.getMessage()); } 回答1: Here are two options: A JTextField with an InputVerifier . The text field will not yield focus unless its contents are of the form

Override VK_Tab Focus action

假装没事ソ 提交于 2020-01-02 07:04:54
问题 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? 回答1: One way is to: First of all, don't use a KeyListener since this is

Korean characters are displayed as empty boxes on JTextField

拟墨画扇 提交于 2020-01-02 06:13:42
问题 JTextfield does not show korean charaters properly. It shows empty boxes in instead of characters. Here is the screenshot of my application. 回答1: Thats because of the fonts that you used. According to this oracle document Korean letters is not supported by Lucida font. Note that of the writing systems that are generally fully supported by the JRE, the Lucida fonts do not support Chinese (Simplified), Chinese (Traditional), Japanese, and Korean. So if you use the fonts that support the korean

How to insert a 'String' in a 'JTextField' to a specific position?

北慕城南 提交于 2020-01-02 04:57:34
问题 I want to insert a String in a JTextField . Something like- myTextField.insert(text, position); How can it be done? Is there any simple way to do this? 回答1: You may do this- jTextField.setText("This swing."); jTextField.getDocument().insertString(5, "is", null); And jTextField will show- This is swing. 来源: https://stackoverflow.com/questions/11493508/how-to-insert-a-string-in-a-jtextfield-to-a-specific-position

How to clear all textfield of jframe using loop?

[亡魂溺海] 提交于 2020-01-01 14:57:46
问题 I'm developing Java application using NetBeans. I have 5 JTextFields and 2 JTextArea in JFrame . I want to clear them at once using a loop. How can it be done? 回答1: Iterate over all of the components and set the text of all JTextField and JTextArea objects to an empty String: //Note: "this" should be the Container that directly contains your components //(most likely a JPanel). //This won't work if you call getComponents on the top-level frame. for (Component C : this.getComponents()) { if (C

How can I add padding to a jtextfield

六眼飞鱼酱① 提交于 2020-01-01 07:34:05
问题 How can I add some padding to a jtextfield? I've tried tf.setMargin(new Insets(5,5,5,5)); which doesn't have any effect. 回答1: The problem you are having is that the UI is setting its own border on the text field, overriding the margin you set. You can see a warning to this effect in the javadoc of setMargin() . The solution is to let the UI set a border, then squeeze in another border of your own: field.setBorder(BorderFactory.createCompoundBorder( field.getBorder(), BorderFactory

Set Cursor On A JTextField

橙三吉。 提交于 2019-12-30 06:57:31
问题 I am making a small application in Java that uses a JTextField. Now, I want, as soon as I run the app, the cursor to be put automatically in that so that the user doesn't have to click on it and then write the text. I have tried pretty much everything that I found on the net: setCaretPosition(0), grabFocus(), requestFocusInWindow() or requestFocus() but none of them worked! I am desperate, can you please help me solve this? Thanks a lot 回答1: By default focus will go to the first component on

How to get JTextField name in which is Document placed?

狂风中的少年 提交于 2019-12-29 08:04:28
问题 Is there something like event.getSource for DocumentListener too? Im trying to change color of just one JTextField in which is text changing. Here is my DocumentListener: DocumentListener posluchac = new DocumentListener() { public void changedUpdate(DocumentEvent e) { warn(e); } public void removeUpdate(DocumentEvent e) { warn(e); } public void insertUpdate(DocumentEvent e) { warn(e); } public void warn(DocumentEvent e) { txtName.setBackground(Color.WHITE); txtSurname.setBackground(Color

Set hilighting color in jTextField

↘锁芯ラ 提交于 2019-12-29 01:27:59
问题 I am developing a Java Swing application. This app allows user use basic command line. For example user can add event with simply type add "Something to be added." .I wanna highlight add when user type this command. Can any one implement a jTextField. 回答1: An idea would be to use a StyleDocument and DocumentFilter This is actually based on this question, but I wasn't sure if I could attempt to use a JTextField import java.awt.Color; import java.awt.EventQueue; import java.awt.GridBagLayout;