Get focus to the start of JFormattedTextField

I create my JFormattedTextField like this:
       try{
             MaskFormatter formatter = new MaskFormatter( "######-####" );
             JFormattedTextField idNr = new JFormattedTextField(formatter);
        catch(ParseException idNr){System.out.println(idNr);}Its working fine but the only problem is that when a user selects the JFormattedTextField to fil it out the focus is gained in the midle instead of the start where it should be.... Like normally a TextField would do.Can anyone help me with this?

This is a small subclass that only works with the DefaultFormatter constructor. Basically, you need to add a FocusListener and MouseListener. When the TextField gains focus, set a flag. When the user clicks in the TextField with the mouse the first time, the flag will be set (focus event comes first). The mouseClicked method will be called. If the flag is set, set the caret position to the beginning.
If the user clicks in the text field after the field already has focus, move the caret to the mouse position as normal.
Chad
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.JFormattedTextField;
import javax.swing.text.DefaultFormatter;
public class JMaskField extends JFormattedTextField
implements FocusListener, MouseListener
     private boolean justGotFocus;
     public JMaskField(DefaultFormatter mask)
          super(mask);
          addFocusListener(this);
          addMouseListener(this);
          justGotFocus = false;
     public void focusLost(FocusEvent event)
          setBackground(Color.white);
     public void focusGained(FocusEvent event)
          setBackground(Color.green);
          justGotFocus = true;
     public void mouseClicked(MouseEvent event)
          if (justGotFocus == true)
               justGotFocus = false;
               setCaretPosition(0);
     public void mouseEntered(MouseEvent e) {}
     public void mouseExited(MouseEvent e) {}
     public void mousePressed(MouseEvent e) {}
     public void mouseReleased(MouseEvent e) {}

Similar Messages

  • How do I get rid of the start-up window?

    I've just installed QuickTime, but when it started I was presented with a window full of advertising. I cannot find anything on the menu allowing me to disable that gross display. How can I get rid of this start-up headache? Thank you for any helpful advice you can render.

    That does not work, actually. I've ben using QT PRo for ages, and ever since version 0, it NEVER saved or remembered my preferences. Once I set my preferences up, it does remember it for the current windows session. But once I restart my machine everything is back to square one. It's been like that for years, actually. This is very annoying sometimes. Makes you wonder if apple "engineers" know how to write code?
    Windows XP Pro SP3 (long time ago SP1, SP2 currently SP3)
    But years ago I used WinXP Home as well
    QT Pro preferences were never saved or remembered.

  • Mac mini gets stuck in the start-up screen! What do I do? Won't boot up.

    Well, I talked my friend into getting a Mac mini, and now it freezes up as the OSX boots up. There appears to be a disc of some sort in the drive, so I can't even put the start-up disc in. What can I do?

    Hold the mouse button down as the machine boots, that should kick the disk out of the CDROm. The next step is to just try to reboot again if the system does not boot after kicking out the CD. If you put the OS disc in and hold down the "c " key that should allow you to boot from the OS disc, then search for the utilities folder and repair the disk.

  • My MacBook is getting stuck on the start up screen (apple icon, progress wheel). What can I do?

    I've tried the normal troubleshooting stuff (turn off and restart, remove battery and restart, SMC reboot) but it keeps getting stuck on the initial start-up screen.

    Boot from your OS installer disk and use disk utility to repair the disk.

  • What is the shortcut to get back to the start page

    after browsing for a while I want to return to the start page but at the moment there seems to be no way to do this except by continuously and repeatedly going back through the pages I have been visiting one by one whereas in every other browser there is a home button

    It depends on wich version of Firefox you are using.
    In Firefox 20 "Home" button is an icon, located in top right corner under the minimalize and close windows buttons.

  • When changing "browser.newtab.url" is there any way to prevent the address bar from getting focus?

    When I change "browser.newtab.url" to about:home and open a new tab, focus is set to the address bar not the search box on the firefox home page. The same thing happens if i change the url to google.com or another search engine. Is there any way to prevent this from happening.

    Custom new Tab has a small bug if the start page is exactly the same as the new tab page. Then the focus of the start page is on the address bar. Just leave the "www." out in the startpage and use "www." in the new tab page then it works perfectly. e.g.
    Startpage: google.com
    new Tab page : www.google.com
    Now the focus of the start page is on the search box.

  • Getting Error In the Routine - While writing Code for the Cross Reference.

    Hi,
    Getting Error In the Start Routine - While writing Code for the Cross Reference from the Text table ( /BIC/TZMDES with Fields /BIC/ZMDES(Key),TXTSH ) Getting Error as [ E:Field "ZMDES" unknown ].
    Transformation : IOBJ ZPRJ3(Source) -> IOBJ ZPRJC ( Target ).
    The Source  Fields are: 0logsys(Key),zprj3(Key),ZDOM3.
    The Target Fields are : 0logsys(Key),zprjc(Key),ZDOM3, UID.
    Here i am trying to Update the target Field UID by Comparing the Source Field [ zprj3(Key)] with the Text table ( /BIC/TZMDES ) and update the UID.
    The Code is as below:
    Global Declarations in the Start Routine:
    Types: begin of itabtype,
            ZMDES type /BIC/TZMDES-/BIC/ZMDES,
            TXT type /BIC/TZMDES-TXTSH,
             end of itabtype.
    data : itab type standard table of itabtype
    with key ZMDES,
    wa_itab like line of itab.
    Routine Code :
    select * from /BIC/TZMDES into corresponding fields of table itab for
    all entries in SOURCE_PACKAGE
    where ZMDES = SOURCE_PACKAGE-/BIC/ZPRJ3.
    READ TABLE itab INTO wa_itab
    WITH KEY ZMDES = SOURCE_PACKAGE-/BIC/ZPRJ3
    BINARY SEARCH.
    IF SY-SUBRC = 0.
    RESULT = wa_itab.
    CLEAR wa_itab.
    The tys_SC_1 structure is :
    BEGIN OF tys_SC_1,
         InfoObject: 0LOGSYS.
            LOGSYS           TYPE RSDLOGSYS,
         InfoObject: ZPRJ3.
            /BIC/ZPRJ3           TYPE /BIC/OIZPRJ3,
         InfoObject: ZDOM3.
            /BIC/ZDOM3           TYPE /BIC/OIZDOM3,
         Field: RECORD.
            RECORD           TYPE RSARECORD,
          END   OF tys_SC_1.
        TYPES:
          tyt_SC_1        TYPE STANDARD TABLE OF tys_SC_1
                            WITH NON-UNIQUE DEFAULT KEY.
    Please suggest with your valuable inputs.
    Thanks in Advance

    I have split the code in two.. one for start routine.. other for field routine.. hope this helps
    Types: begin of itabtype,
    ZMDES type /BIC/TZMDES-/BIC/ZMDES,
    TXT type /BIC/TZMDES-TXTSH,
    end of itabtype.
    data : itab type standard table of itabtype
    with key ZMDES,
    wa_itab like line of itab.
    Start routine
    select * from /BIC/TZMDES into corresponding fields of table itab for
    all entries in SOURCE_PACKAGE
    where ZMDES = SOURCE_PACKAGE-/BIC/ZPRJ3.
    Sort itab.
    field routine
    CLEAR wa_itab.
    READ TABLE itab INTO wa_itab
    WITH KEY ZMDES = SOURCE_FIELD-/BIC/ZPRJ3
    BINARY SEARCH.
    IF SY-SUBRC = 0.
    RESULT = wa_itab-<field name>

  • Two clicks for getting focus in h:inputtext ?

    Hello.
    I have a JSF page whose three first components are *<h:inputText ...*
    When I press Tab for get focus on the next inputText, the cursor pass to the next field and disappear. I need click mouse on the field for getting focus again. Why?
    Thank you.

    Then I don't know. It's certainly a matter of the webbrowser/client environment. All I can suggest is to test in different browsers/environments and to doublecheck all the generated HTML/JS output.

  • Display full autosuggest list when field gets focus

    I've searched through the the Spry topics but did not find a
    solution to my question.
    How does one make the Spry autosuggest drop down to
    automatically display with an unfiltered list when the associated
    text field gets focus? The best I can tell this option isn't a
    normal part of the Spry autosuggest widget and would require a
    custom tweak.
    I'd also like to put in a suggestion that this ability be a
    built in option for the autosuggest widget.

    AFAIK, there isn't a way to do that in a PDF form.

  • My Final Cut Pro X won't load past the start-up screen....Why??

    I have been using FCPX for about 3 months now and I have had no problems. Out of NOWHERE today when I tried to open up FCPX it loads everything except for one project and it gets stuck. The start-up screen simply says "reading project 1". I have no idea what to do. I use FCPX everyday for editing videos and now I have nothing. I need someone's help.

    Not too many fans of FCPX here. Wrong forum. You'll have better luck here. https://discussions.apple.com/community/professional_applications/final_cut_pro_ x

  • IPad frozen on the starting screen!

    As I did a full reset to my iPad, It won't start. If I run it, it gets stuck on the starting screen and all I could see is the *"Eaten Apple"*! Gr.... I just bought it a weak ago. Now I dunno what to do with it?

    Welcome to the discussions. This Apple doc may help:
    _iPad: Basic troubleshooting_

  • How to Get the focus of the textbox.

    Hi all,
    I have a problem in getting the focus of the textbox, The scenario is in this way,
    I have 3 textboxes in a window, each one should should get enabled one after the other only (i.e) Textbox2 should be enabled only after Textbox1 is filled and pressed the TAB button and Textbox3 is enabled only after Textbox2 is filled and pressed the TAB button. Textbox2 is also enabled when the cursor is in Textbox3 and SHIFT+TAB is pressed,
    I tried this with the Focus Listener but i cant able to get the cursor in the textbox. Please help in this, how to do this.
    Thanks in Advance.

    844031 wrote:
    Hi all,
    I have a problem in getting the focus of the textbox, The scenario is in this way,
    I have 3 textboxes in a window, each one should should get enabled one after the other only (i.e) Textbox2 should be enabled only after Textbox1 is filled and pressed the TAB button and Textbox3 is enabled only after Textbox2 is filled and pressed the TAB button. Textbox2 is also enabled when the cursor is in Textbox3 and SHIFT+TAB is pressed,
    I tried this with the Focus Listener but i cant able to get the cursor in the textbox. Please help in this, how to do this.
    Thanks in Advance.Hello user,
    If you are talking about 'TextBox' of javafx then please start a new thread on Javafx Category here:JavaFX 1.x and JavaFX Script [ARCHIVE]
    Thanks.,
    narayan

  • Automatically focus JInternalFrame when starting the application...

    Hi,
    I wonder why my JInternalFrame is not selected (focused) when I start the application - in NetBeans GUI editor I have set the following properties to make my JInternalFrame automatically selected when the app starts:
    o enabled
    o selected
    o visible
    Can anyone tell me how to make my JInternalFrame focused?
    Thanks
    - Stephan

    Thanks but unfortunately that didn't solve my problem...
    I even tried the following code excerpt...
    try {
        jInternalFrame1.setSelected(true);
    } catch (java.beans.PropertyVetoException e1) {
        e1.printStackTrace();
    }... but it didn't work - my jInternalFrame1 is not focused on startup. Is this a bug in Java 1.4?
    On the other hand I noticed that the example application (InternalFrameDemo) doesn't focus the first internal frame on startup, too - when creating another internal frame from within the application that frame gets focused...
    http://java.sun.com/docs/books/tutorialJWS/uiswing/components/ex6/InternalFrameDemo.jnlp
    Thanks in advance
    - Stephan

  • How to stop preview window from automatically getting focus when you move up/down the message list.

    Since updating (my TB was a few versions old) to 31.2, the preview window automatically gets focus when I move up/down the message list. I'm assuming this is a new "feature". Is there a way to shut this off and leave the focus on the message list so I can navigate up/down like before?

    You are referring to the 'Message Pane' which displays selected opened emails below the list of emails. It is not a 'preview'.
    When you select an email by either using mouse to click on an email in the list or using the arrow keys to select emails up and down the list, you are selecting an email to open and read. That is why it is being opened in the Message Pane so that you can read it.
    If you select an email in the list and then cllck anywhere else including in the Message Pane, then you will have moved focus away from the list and the strong highlight on email in list is reduced to paler highlight.
    When you click on an email in the list, the email becomes highlighted, at this point you can use eg: arrow keys to move up and down the list.
    If you are performing single left click on email in list and then do nothing else and the strong highlight auto changes to paler highlight.
    Check you do not have a conflict with an addon by starting Thunderbird in Safe Mode.
    Help > select 'Restart with addons disabled'

  • My mail app quits a few seconds after launching. I get a blank screen and then it goes back to the starter screen

    THe mail app on my ipad 2 quits right after launching. This happened as I was forwarding an email with a long attachment. Now I get a blank screen with just the icons, no mail mailboxes etc. after a few seconds that blank screen disappears and we go back to the starter screen. I tried rebooting, relaunching but no luck so far. Can somebody help me? Thanks
    Hans V

    Have you tried removing the email account from the app through settings?  I would try removing it, rebooting, opening the email app to see if it still crashes,  & then adding it back your email account. 
    If you're leary about causing more issues while you're not home, you could always try using a third party mail app (there are many), accessing your email through it...not a fix for your problem but a way to deal with the situation until you can get it worked out.
    Resetting all settings on the iPad (not deleting content) is a good way to clear many errors as it can remove leftover cache files & files that have become corrupt.
    Sorry I couldn't be of more help.  Good luck & let us know if you get it working.

Maybe you are looking for

  • Spry submenu z-index problem in IE

    My sub menus in IE are going behind the page content on my wordpress blog page. I'm not sure if this is a spry or wordpress problem, but I had this problem on a few other (non wordpress) pages and fixed it by adding "z-index:10000;" under "#MenuBar.M

  • Cannot resolve symbol - class FileInputStream

    I am getting the above error when I compile this following method; I get it in the first line of code. Anyone know why? private String readAndProcessData(FileInputStream stream)       InputStreamReader iStrReader = new InputStreamReader(stream);     

  • Problem with the method in JSP

    <%@ method = "doPost" %> ******* if i remove this statement i am getting output but what is wrong with this statement ******** <HTML> <HEAD><TITLE>Hello</TITLE></HEAD> <BODY> <H1> <% if (request.getParameter("name") == null) { %> Hello World <% } els

  • DataTable var in request scope?

    Hi all, The description for the 'var' attribute of a h:dataTable tag reads: "Name of a request-scope attribute under which the model data for the row ... will be exposed." So I thought I would be able to access the object set as 'var' in normal JSP e

  • Messages taking 2 hours to process .

    Hi our system is getting very slow with below warning every second The adapter failed to transmit message going to send port "SendPort" with URL  "". It will be retransmitted after the retry interval specified for this Send Port. Details:"System.Tran