InputVerifier for JTextArea problem

Hello,
I am having a problem with using an InputVerifier to check a max character limit on a JTextArea. It seems that occasionally after the verify of the JTextArea fails, I lose the next character I type into it! Here is the code, followed by a description of how to reproduce the problem. Note this code is copied from the Java 1.4.2 API documentation of the InputVerifier class, with the first JTextField changed to a JTextArea and the verify method modified:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class TestJTextArea  extends JFrame {
    public TestJTextArea () {
       JTextArea ta = new JTextArea ("");
       ta.setPreferredSize(new Dimension(100, 50));
       getContentPane().add (ta, BorderLayout.NORTH);
       ta.setInputVerifier(new PassVerifier());
       JTextField tf2 = new JTextField ("TextField2");
       getContentPane().add (tf2, BorderLayout.SOUTH);
       WindowListener l = new WindowAdapter() {
           public void windowClosing(WindowEvent e) {
               System.exit(0);
       addWindowListener(l);
    class PassVerifier extends InputVerifier {
        public boolean verify(JComponent input) {
            JTextArea jText = (JTextArea)input;
            boolean retValue = jText.getText().length() < 5;
            if (!retValue) {
                System.out.println("MAX CHARS");
            return retValue;
    public static void main(String[] args) {
        Frame f = new TestJTextArea ();
       f.pack();
       f.setVisible(true);
}A way to reproduce the problem is type in the TextArea: "123455", then Ctrl+Tab and the verify will fail. Then backspace twice to delete the "55" and Ctrl+Tab to move to the TextField. Then Tab again to go back to the TextArea and type any charachter over than 5. About 80% of the time, the character that you type will be lost and not appear in the TextArea. It doesn't always happen, but if you try it a few times it will come up. Also, occasionally the backspace keystroke is also lost in the TextArea. Note this only happens when I use Tab. I am not able to reproduce it by using the mouse to change focus.
I have searched for a bug report on this, but have found nothing. Am I doing something wrong? I am simply using the sample code from the InputVerifier JavaDoc, but with a JTextArea... This is on a WinXP Pro machine with Java Std. Ed. 1.4.2.
Thank you for your help.
Angel

JTextField jtf = (JTextField)comboBox.getEditor().getEditorComponent();
jtf.setInputVerifier(new YourInputVerifier());

Similar Messages

  • Which listener should be used for JTextArea

    Hi,
    I need to add a listener for JTextArea particualy when
    the text in the jtextarea is changed. I don't think
    mouse listener is enough, since the user can use tab to
    get to the jtextarea and edit the text. In this case,
    which listener should I use. Sample code would be
    helpful.
    In addition, I have a problem with the jtextarea that
    when user uses tab to go to different gui components,
    such as jtextfield, jcombobox, jtextarea, jlist, etc, the
    tab stays in the jtextarea and keeps appending to it. It does
    not go to next component, such as jlist. How can I make it
    work?
    Thanks in advance,
    Pin

    It doesn't work. Are you using 1.4 or 1.3? I am using
    1.4.
    In the 1.4 API, it says that isManagingFocus is
    "Deprecated".
    Here is what I have:
    JTextArea descrptArea = new JTextArea() {
    public boolean isManagingFocus() {
    return false;
    descrptArea.setRows(3);
    descrptArea.setLineWrap(true);
    descrptArea.addKeyListener(new KeyAdapter() {
    public void keyTyped(KeyEvent e) {
    // DO SOMETHING
    I found the following code which says it will do it.
    However, I got the exeception:
    java.lang.ClassCastException:
    n: javax.swing.KeyStroke
    at java.util.TreeMap.compare(TreeMap.java:1081)
    at java.util.TreeMap.put(TreeMap.java:459)
    at java.util.TreeSet.add(TreeSet.java:205)
    Set forwardTraversalKeys = new TreeSet();
    forwardTraversalKeys.add(KeyStroke.getKeyStroke('\t'));
    forwardTraversalKeys.add(KeyStroke.getKeyStroke(KeyEven
    .VK_TAB,
    InputEvent.CTRL_MASK));
    textArea.setFocusTraversalKeys
    (KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
    forwardTraversalKeys);
    Set backwardTraversalKeys = new TreeSet();
    backwardTraversalKeys.add(KeyStroke.getKeyStroke(KeyEve
    t.VK_TAB,
    InputEvent.SHIFT_MASK));
    backwardTraversalKeys.add(KeyStroke.getKeyStroke(KeyEve
    t.VK_TAB,
    InputEvent.SHIFT_MASK |InputEvent.CTRL_MASK));
    textArea.setFocusTraversalKeys(
    KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
    backwardTraversalKeys);
    Any idea???
    PinI'm not using 1.4 but the class cast exceptions look like they come from : Set forwardTraversalKeys=new TreeSet() etc
    try : TreeSet forwardTraversalKeys=new TreeSet()
    you cant cast from a Set to a TreeSet because a TreeSet is a Set but a Set is NOT a TreeSet.
    hope this helps.

  • Customize "Tab" key for JTextArea to focus next component.

    Hi,
    I am trying to change the "TAB" key behaviour for JTextArea, by using CustomAction configured via InputMap() and ActionMap(). When the user presses the Tab key, the focus should go the next component instead of tabbing in the same JTextArea component. Here is the code for the CustomAction().
        public static class CustomTabAction extends AbstractAction {
            private JComponent comp;
            public CustomTabAction (JComponent comp) {
                this.comp = comp;
                this.comp.getInputMap().put(KeyStroke.getKeyStroke("TAB"), "TABPressed");
                this.comp.getActionMap().put("TABPressed", this);
            public void actionPerformed(ActionEvent evt) {
                Object source = evt.getSource();
                if (source instanceof Component) {
                    FocusManager.getCurrentKeyboardFocusManager().
                            focusNextComponent((Component) source);
        }This works for most of the cases in my applicaiton. The problem is that it doesn't work with JTable which has a custom cell editor (JTextArea). In JTextArea field of JTable, if the Tab is pressed, nothing happens and the cursor remains in the custom JTextArea exactly at the same place, without even tabbing spaces. Here is the CustomCellEditor code.
        public class DescColCellEditor extends AbstractCellEditor implements TableCellEditor {
    //prepare the component.
            JComponent comp = new JTextArea();
            public Component getTableCellEditorComponent(JTable table, Object value,
                    boolean isSelected, int rowIndex, int vColIndex) {
                // Configure Tab key to focus to next component
                CustomActions.setCustomAction("CustomTabAction", comp);
                // Configure the component with the specified value
                ((JTextArea)comp).setText((String)value);
                // Return the configured component
                return comp;
            // This method is called when editing is completed.
            // It must return the new value to be stored in the cell.
            public Object getCellEditorValue() {
                return ((JTextArea)comp).getText();
        }regards,
    nirvan

    >
    textArea.getInputMap().remove(....);but that won't work because the binding is actually defined in the parent InputMap. So I think you need to use code like:
    textArea.getInputMap().getParent().remove(...);But I'm not sure about this as I've never tried it.I tried removing the VK_TAB key from both the input map and parent input map as shown below. But I still have to press "TAB" twice in order to get out of the JTextArea column in JTable.
                comp.getInputMap().getParent().remove(
                            KeyStroke.getKeyStroke(KeyEvent.VK_TAB,0));
                comp.getInputMap().remove(
                            KeyStroke.getKeyStroke(KeyEvent.VK_TAB,0));after coding this, I am using the setFocusTraversalKeys for adding only "TAB" key as ForwardTraversalKey as given below.
            Set newForwardKeys = new HashSet();
            newForwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));
            comp.setFocusTraversalKeys(
                        KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,newForwardKeys);
            Set newBackwardKeys = new HashSet();
            newBackwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, KeyEvent.SHIFT_DOWN_MASK));
            comp.setFocusTraversalKeys(
                        KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,newBackwardKeys);The only problem that remains now is that I have to press the "TAB" twice in order to get out of the JTextArea column
    regards,
    nirvan.

  • Override "crtl + tab" key behaviour with "tab" key for JtextArea .

    I am trying to override the "crtl + tab" key behaviour for JTextArea with "tab" key plus add my own action. I am doing the following.
    1. Setting Tab as forward traversal key for the JTextArea (the default traversal key from JTexArea is "crtl + Tab").
    2. Supplementing the "crtl + Tab" key behaviour with my custom behaviour.
    For the point 2 above, I need to get hold of the Action represented by the "crtl + Tab" key so that I could use that and then follow with my own custom action. But the problem is that there is no InputMap entry for "crtl + tab". I dont know how the "crtl + tab" key Action is mapped for JTextArea. I used the following code to search the InputMap.
                System.out.println("Searching Input Map");
                for (int i = 0; i < 3; i++) {
                    InputMap iMap = comp.getInputMap(i);
                    if (iMap != null) {
                        KeyStroke [] ks = iMap.allKeys();
                        if (ks  != null) {
                            for (int j = 0;j < ks.length ;j++) {
                                System.out.println("Key Stroke: " + ks[j]);
                System.out.println("Searching Parent Input Map");
                for (int i = 0; i < 3; i++) {
                    InputMap iMap = comp.getInputMap(i).getParent();
                    if (iMap != null) {
                        KeyStroke [] ks = iMap.allKeys();
                        if (ks  != null) {
                            for (int j = 0;j < ks.length ;j++) {
                                System.out.println("Key Stroke: " + ks[j]);
                }In short, I need to get the Action associated with the "crtl + tab" for JTextArea.
    regards,
    nirvan.

    There is no Action for Ctrl+TAB. Its a focus traversal key.

  • Line limit for JTextArea - Urgent

    Hi All,
    I would like to have 80 character limit for each line displayed in JTextArea. The linewrap along with wordwrap have been enabled for JTextArea but that solves a part of the problem. The no. of characters that can be entered per line depends on the width of the component and the font size. For this component there is no upper limit for the total no. charaters that can be entered. Basically, it supports text editing feature(cut, copy, paste). One way it could have been done by fixing the width of component so that it could accomodate only 80 characters, but again the factors like screen resolution, font's advance width keeps changing. I tried with overriding insertString() of PlainDocument class, but didn't know how to wrap the characters after 80 position to next line if text is inserted in the middle of a line. At the same time the whole text needs a reformatiing. Same behavior is desirable while doing cut/paste operations.
    A hint or sample code will be highly appreciated as it has been a long time I am looking to solve this problem.
    Thanks
    Ritwick.

    You are correct it is not easy to override the default behaviour of the JTextArea. So my suggestion is to use a monospaced font. The code would then be:
    JTextArea.textArea = new JTextArea( "some text", 10, 80 );
    textArea.setFont( new Font("monospaced", Font.PLAIN, 12) );
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);
    JScrollPane scrollPane = new JScrollPane( textArea );
    scrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );If using a monospaced font is not possible, then please explain why wrapping at exactly 80 characters is so important, maybe there is a different approach to take.

  • I've only had my iphone 5s for a week. I keep getting an error message of "Server has stopped responding."  I need the server to work. Does anyone know if there is a "fix" for the problem? Other wise, I probably best return for a refund and get a Samsung.

    I've only had my iphone 5s for a week. I keep getting an error message of "Server has stopped responding."  I need the server to work. Does anyone know if there is a "fix" for the problem? Other wise, I probably best return for a refund and get a Samsung.  Thanks

    sandyzotz wrote:
    Other wise, I probably best return for a refund and get a Samsung.
    Unlikely.  Based on the complete lack of detail of the issue provided it is entirely possible the same issue would occur.
    Unless and until the user provides some actual details of the problem, there is nothing the indicate that the issue is with the iPhone.

  • HT204266 My iPad (version 1, IOS 5.1) has quit connecting with the store. I am unable to update or buy any app. I did a reboot and a reset with deleting the data. I can not find anything in support for this problem. Any help will be appreciated.

    My iPad (version 1, IOS 5.1) has quit connecting with the store. I am unable to update or buy any app. I did a reboot and a reset with deleting the data. I can not find anything in support for this problem. Any help will be appreciated.

    My iPad (version 1, IOS 5.1) has quit connecting with the store. I am unable to update or buy any app. I did a reboot and a reset with deleting the data. I can not find anything in support for this problem. Any help will be appreciated.

  • I've searched to no avail for this problem. Similar posts but none that tell me what to do. I can't add or delete any bookmarks on my iPad 2 running the newest iOS. I know how it's suppose to work, it just isn't working!

    I've searched to no avail for this problem. Similar posts but none that tell me what to do. I can't add or delete any bookmarks on my iPad 2 running the newest iOS. I know how it's suppose to work, it just isn't working!
    It started after the major update to iOS 7.
    I can't believe that this is so hard to do. It's just not letting me. I can add a bookmark to the home screen just fine, just not in a bookmarks folder anywhere I try.
    I've used Apple products since 2001 and have always loved how intuitive they are. But the Safari browser since iOS 7 has been the worst I've experienced. At least right in the beginning after that update.
    I'd really appreciate any help that doesn't just tell me how it's suppose to work...I know that.
    My iPad 4 is not affected with the problem and works as it should.

    To delete, tap "Edit" (tap to enlarge image)

  • Can someonoe please assist me in my Adobe Photoshop CS6 Extended.  I keep getting a 'Couldn't Complete Your Request because Dynamic Link is not Available?" Is there a way of getting a software patch for this problem?  I thought you didn't need extra softw

    Can someonee please assist me in my Adobe Photoshop CS6 Extended.  I keep getting a 'Couldn't Complete Your Request because Dynamic Link is not Available?" Is there a way of getting a software patch for this problem?  I thought you didn't need extra software.  My software other than that problem works fine.

    Couldn't complete what request?   Way more information please.
    What operating system?

  • TA24002 My 500 GB can't verify nor repair. I have photoshop work that I need to recover. I would like to know which erase option would be the best solution for this problem.

    My 500 GB can't verify nor repair. I have photoshop work that I need to recover. I would like to know what option would be the best solution for this problem?

    You appear to have two issues: 1) a hard drive that is not working properly and 2) files you wish to recover.
    Re 1) you need to answer Kappy's questions.
    Re 2) does the drive load and can you see your photo files? If so can you copy them to another drive?
    Do you not have a backup of the photo files?

  • I  have no service on my iphone 3gs. Is there a fix for this problem.

    I have no service  or no 3G Network on my iphone 3GS. Is there a fix from Apple for this problem.

    Sometimes this happens to me, I know I am old school as well with an iPhone 3g hey what can I say I got a great deal on it. If I do a reboot, hold the power button and home button down for 10 seconds, and then hold the power button down until the apple logo appears it usually fixes this issue.

  • I have problems in the initiation of the Encore process when opening presents the following error message : "Encore CS6 Cannot Run in Non-Royalty Serialized".... What is the best solution for this problem ?

    Help Me.
    What is the best solution for this problem ?

    Encore is activated when you activate Premiere Pro... so, as Stan asked, how did you install P-Pro?
    Ask for serial number http://forums.adobe.com/thread/1234635 has a FAQ link
    -and a fix for Encore http://forums.adobe.com/thread/1421765?tstart=0 in reply #7
    -plus more Encore http://helpx.adobe.com/encore/kb/cant-write-image-fie-larger1.html

  • I can't get any contact info for installation problem- I just bought a Mac Air and my CS5 is asking for a reinstall. Mac Air has no DVD slots. HELP I al VERY FRUSTRATED THAT ADOBE HAS NO WAY TO GET HELP

    I can't get any contact info for installation problem- I just bought a Mac Air and my CS5 is asking for a reinstall. Mac Air has no DVD slots. HELP I al VERY FRUSTRATED THAT ADOBE HAS NO WAY TO GET HELP

    You are right, Adobe does not support CS5 because it is no longer sold.
    Simply download the trial version of CS5 from this Adobe web site and input your serial number.
    Adobe - Photoshop : For Macintosh

  • I bought my iphone 5 in Houston Texas May 15 2013 IMEI Nr. 013428009645399.The problem is that in the Greece the country which I live the 4G is not working.If you have any solution for this problem pls. let me know.My email is philcoueth@yahoo.gr Thank yo

    I bought my iphone 5 in Houston on May 15 2013.
    IMEI 013428009645399.The problem I have is that in the country
    which I live GREECE the 4G is
    not working.Please if you have any solution for this
    problem let me know.My email is [email protected]
    Thanking you in advance
    Philip Couridis

    iPhones purchased in the US are NOT guaranteed to work with 4G bands outside of North America.
    For what crazy reason did you purchase an iPhone in the US if you live in Greece?  If your phone needs servicing, it will have to be brought back to the US.  You cannot get that phone serviced in Greece.

  • Auto creation of notification for vendor problems

    Hi, pls advise how to generate notificatin for vendor problems during goods receipt. can it be possible to generate automatically when there is problem for the vendor for the goods which are subjected to GR. Pls advise.

    Hi Yadav,
    pls advise how to generate notificatin for vendor problems during goods receipt.
    While doing GR( which is stores activity) why you want to generate Notification, once GR is made the inspection lot is generated, then while doing RR if some thing is not as per specs then you can raise notification automatically.
    can it be possible to generate automatically when there is problem for the vendor for the goods which are subjected to GR. Pls advise.
    You will be able to generate auto  notification only after you do GR, while doing RR. This authorization you can given to stores but  again unless you inspect the material how you will know that there is problem in material and you need to raise notification.
    for auto notification while RR, you have to assign notification type Q2/ N2 at that inspection type at customizing.
    Also you should tick 'Defect recording' tab in  control indicator of  MIC's, this will  automatically  pop up the Window for  defect recording and  thereby notification.
    Best Regards,
    Shekhar

Maybe you are looking for

  • ITunes not working on my Windows 7 computer

    Getting message MSVCR80.dll is missing reinstall iTunes.  Went to iTunes and seemed to be sent to download latest update and tried.  Failed.  Got message: Apple Mode Devise failed to start verify sufficient privileges.  Have no idea what is going on

  • License management in BOBJ4.0

    We are installing BOBJ4 on our POC sandbox. We notice on SAP Market Place there is a button: Obtain an SAP BusinessObjects temporary license key. The instruction downloaded with the temp license is pretty complicated. Just curious, we accepted the li

  • Install on PowerMac without DVD drive

    I have an older PowerMac G4 here with a CD drive only, but all of my Tiger disks (upgrade and retail install) are on DVD's. Since I have a software title that will not work on Panther, I have to get this machine installed with Tiger.. So, short of bu

  • I hit "tabs on top",and now can't change them back..what a mistake. Opera

    First,don't like to be forced to be updated..second,you used to be able to change the tabs position..I hit,"tabs on top"..in the bookmarks tab,thinking I could change back..WHAT A MISTAKE..Opera is a cool browser,that needs to be revisited..great sli

  • Material - UoM Pair issue in SRM Portal - shopping cart

    Hello Experts, When we try to add Pair type material in shopping cart in SRM Sandbox Portal (Material no: 11100000000111) , it is throwing error as " Incomplete items in catalog. Only complete items were transferred" in checkout screen and item is no