TextField Focus

Hello there,
I am new to AS3 and trying to figure out how to set a focus to a textfield on the stage. I found out that if I use
stage.focus = tf; then the tf get the focus and it does, when I type letters they go directly to this textfield.
Problems are,
I don't have a blinking cursor in the textfield.
I added event listener to catch up when the user press the  ENTER  key but it catches the ENTER key ONLY after I click the textfield with the mouse and having a blinking cursor in that textfield.
Any idea ?
regards,

Are you seeing this behavior in the testing environment or when you put the flash into an html page?
In the testing environment you might want to go to the Control menu and select Disable keyboard shortcuts. That will give you a better representation of how things will actually work when they are standing on their own.
Also there is some kind of issue around setting the focus. The swf object has to have already received a click before it can set the blinking I-beam into an input text field. It is a "security" type of thing. To prevent a malicious swf from looking like some kind of interactive password field and grabbing someone's password or the like.
Depending upon what you are doing you can have a "Start" button or something to make sure that the user has clicked the flash before flash can be able to set the focus.

Similar Messages

  • Setting textfield focus in ActionScript 3.0

    I am using an event handler to listen for MOUSE_UP on stage_mc to let the user create text and images on the screen. I have buttons above the stage that set a variable, then the variable determines which action occurs with the mouse event.
    I would like the user to be able to enter text, and can create an input text field on the stage, but the user then has to click inside the text field to begin typing. Because of the event handler, this 2nd MOUSE_UP repositions the text field, although it IS successful in setting the focus.
    Ideally, I would like for the user to be able to create the input text field by clicking on the stage and then begin typing without having to click a 2nd time. It seems simple enough, but I still don't understand OOP enough to decipher the help pages available within the program.
    Does anyone have any suggestions? Here is my code (some of the included variable are set in previous lines):
    //Setting up the Text Field parameters
        var outputText:TextField = new TextField();
        var textFormat:TextFormat = new TextFormat();
    // Enabling the input tools
    function startDraw(event:MouseEvent):void {
        if (currentTool.name == "lineTool_mc") {
                newLine.graphics.moveTo(event.localX, event.localY);
                stage_mc.addEventListener(MouseEvent.MOUSE_MOVE, drawLine);  
        } else if (currentTool.name == "textTool_mc") {          
            textFormat.color = 0x336699;
            textFormat.size = 48;
            textFormat.font = "Times";
            outputText.defaultTextFormat = textFormat;
            outputText.selectable = true;
            outputText.x = event.localX;
            outputText.y = event.localY;
            outputText.background = false;
            outputText.border = true;
            outputText.autoSize = TextFieldAutoSize.LEFT;
            outputText.type = TextFieldType.INPUT;
            designContainer.addChild(outputText);
        } else if (currentTool.name == "brushTool_mc") { ...

    on mouseup or click, if the mouse is down, you can use:
    stage.focus=outputText;
    if the mouse isn't down, you can don't need a listener to trigger the above.

  • Input textfield focus problem

    Hello
    I'm developing a series of learning aids and the final screen requires the user to input some text.
    I have it working correctly but now need to make it a little more user friendly.
    At the moment the only way for the user to input the text is to click in each box. There are a total of 6 input text boxes.
    I would like to direct the user to a particular input text and then have them be able to use the tab key or the enter key or mouse clicks to proceed.
    As the first step I added the stage.focus = inputStarName1. See code below. As soon as I added the stage.focus = inputStarName1 line I get the dredded error 1009 when the code invokes TypeNames.
    What do I need to do to get rid of this error?
    Any help will be greatly appreciated.
    package
        import flash.display.*;
        import flash.text.*;
        import flash.events.*;
        public class TypeNames extends MovieClip
            public function TypeNames()
                addEventListener(Event.ADDED_TO_STAGE, addedHandler);
                addEventListener(Event.REMOVED_FROM_STAGE, removedHandler);
                backBtn.buttonMode = true;
                resetBtn.buttonMode = true;
                scoreBtn.buttonMode = true;
                backBtn.addEventListener(MouseEvent.CLICK, back);
                resetBtn.addEventListener(MouseEvent.CLICK, reset);
                scoreBtn.addEventListener(MouseEvent.CLICK, mark);
                scoreBtn.alpha = 1;
                feedback.alpha = 0;
                correct1.alpha = 0;
                correct2.alpha = 0;
                correct3.alpha = 0;
                correct4.alpha = 0;
                correct5.alpha = 0;
                correct6.alpha = 0;
                var score:uint = 0;// sets score variable to zero
                // Defines Input text format
                var inputFormat:TextFormat = new TextFormat();
                inputFormat.font = "Arial Bold";
                inputFormat.size = 14;
                inputFormat.align = "center";
                inputFormat.color = 0xffff00;
                //Defines and adds text input boxes for star names
                var inputStarName1:TextField = new TextField();
                inputStarName1.type = "input";
                inputStarName1.defaultTextFormat = inputFormat;
                inputStarName1.x = 12;
                inputStarName1.y = -38;
                inputStarName1.height = 18;
                inputStarName1.width = 70;
                inputStarName1.background = true;
                inputStarName1.border = true;
                inputStarName1.backgroundColor = 0x555555;
                inputStarName1.borderColor = 0xffff00;
                addChild(inputStarName1);
                stage.focus = inputStarName1;

    Hi Kglad
    Thanks for the prompt response. I tried your suggestion but got error 1120.
    The addedHandler function is presently only used to center the screens created by the class packages.
    I tried moving the addedHandler function before the constructor but it made no difference.
    Just in case there is something else in my code that's messing things up I have copied it in its entirety below.
    As you will see I still need to learn how to use arrays and for loops to shorten up my code but I needed to get this operational in a hurry hence the brute force approach.
    Thanks again for your help
    package
        import flash.display.*;
        import flash.text.*;
        import flash.events.*;
        public class TypeNames extends MovieClip
            public function TypeNames()
                addEventListener(Event.ADDED_TO_STAGE, addedHandler);
                addEventListener(Event.REMOVED_FROM_STAGE, removedHandler);
                backBtn.buttonMode = true;
                resetBtn.buttonMode = true;
                scoreBtn.buttonMode = true;
                backBtn.addEventListener(MouseEvent.CLICK, back);
                resetBtn.addEventListener(MouseEvent.CLICK, reset);
                scoreBtn.addEventListener(MouseEvent.CLICK, mark);
                scoreBtn.alpha = 1;
                feedback.alpha = 0;
                correct1.alpha = 0;
                correct2.alpha = 0;
                correct3.alpha = 0;
                correct4.alpha = 0;
                correct5.alpha = 0;
                correct6.alpha = 0;
                var score:uint = 0;// sets score variable to zero
                // Defines Input text format
                var inputFormat:TextFormat = new TextFormat();
                inputFormat.font = "Arial Bold";
                inputFormat.size = 14;
                inputFormat.align = "center";
                inputFormat.color = 0xffff00;
                //Defines and adds text input boxes for star names
                var inputStarName1:TextField = new TextField();
                inputStarName1.type = "input";
                inputStarName1.defaultTextFormat = inputFormat;
                inputStarName1.x = 12;
                inputStarName1.y = -38;
                inputStarName1.height = 18;
                inputStarName1.width = 70;
                inputStarName1.background = true;
                inputStarName1.border = true;
                inputStarName1.backgroundColor = 0x555555;
                inputStarName1.borderColor = 0xffff00;
                addChild(inputStarName1);
                var inputStarName2:TextField = new TextField();
                inputStarName2.type = "input";
                inputStarName2.defaultTextFormat = inputFormat;
                inputStarName2.x = -270;
                inputStarName2.y = -98;
                inputStarName2.height = 18;
                inputStarName2.width = 80;
                inputStarName2.background = true;
                inputStarName2.border = true;
                inputStarName2.backgroundColor = 0x555555;
                inputStarName2.borderColor = 0xffff00;
                addChild(inputStarName2);
                var inputStarName3:TextField = new TextField();
                inputStarName3.type = "input";
                inputStarName3.defaultTextFormat = inputFormat;
                inputStarName3.x = 75.3;
                inputStarName3.y = 38.3;
                inputStarName3.height = 18;
                inputStarName3.width = 70;
                inputStarName3.background = true;
                inputStarName3.border = true;
                inputStarName3.backgroundColor = 0x555555;
                inputStarName3.borderColor = 0xffff00;
                addChild(inputStarName3);
                var inputStarName4:TextField = new TextField();
                inputStarName4.type = "input";
                inputStarName4.defaultTextFormat = inputFormat;
                inputStarName4.x = 227.45;
                inputStarName4.y = 99.26;
                inputStarName4.height = 18;
                inputStarName4.width = 70;
                inputStarName4.background = true;
                inputStarName4.border = true;
                inputStarName4.backgroundColor = 0x555555;
                inputStarName4.borderColor = 0xffff00;
                addChild(inputStarName4);
                var inputStarName5:TextField = new TextField();
                inputStarName5.type = "input";
                inputStarName5.defaultTextFormat = inputFormat;
                inputStarName5.x = 94.45;
                inputStarName5.y = -72;
                inputStarName5.height = 18;
                inputStarName5.width = 70;
                inputStarName5.background = true;
                inputStarName5.border = true;
                inputStarName5.backgroundColor = 0x555555;
                inputStarName5.borderColor = 0xffff00;
                addChild(inputStarName5);
                var inputStarName6:TextField = new TextField();
                inputStarName6.type = "input";
                inputStarName6.defaultTextFormat = inputFormat;
                inputStarName6.x = 211;
                inputStarName6.y = 29.5;
                inputStarName6.height = 18;
                inputStarName6.width = 70;
                inputStarName6.background = true;
                inputStarName6.border = true;
                inputStarName6.backgroundColor = 0x555555;
                inputStarName6.borderColor = 0xffff00;
                addChild(inputStarName6);
                function mark(event:MouseEvent):void
                    if (inputStarName1.text.toUpperCase() == "POLARIS"
                        && inputStarName2.text.toUpperCase() == "SCHEDAR"
                        && inputStarName3.text.toUpperCase() == "KOCHAB"
                        && inputStarName4.text.toUpperCase() == "ALKAID"
                        && inputStarName5.text.toUpperCase() == "DUBHE"
                        && inputStarName6.text.toUpperCase() == "ALIOTH")
                        MovieClip(parent).gotoReward();
                    else
                        feedback.alpha = 1;
                    if (inputStarName1.text.toUpperCase() !== "POLARIS")
                        correct1.alpha = 1;
                        correct1.text = "Polaris";
                    if (inputStarName2.text.toUpperCase() !== "SCHEDAR")
                        correct2.alpha = 1;
                        correct2.text = "Schedar";
                    if (inputStarName3.text.toUpperCase() !== "KOCHAB")
                        correct3.alpha = 1;
                        correct3.text = "Kochab";
                    if (inputStarName4.text.toUpperCase() !== "ALKAID" )
                        correct4.alpha = 1;
                        correct4.text = "Alkaid";
                    if (inputStarName5.text.toUpperCase() !== "DUBHE" )
                        correct5.alpha = 1;
                        correct5.text = "Dubhe";
                    if (inputStarName6.text.toUpperCase() !== "ALIOTH")
                        correct6.alpha = 1;
                        correct6.text = "Alioth";
            private function reset(event:MouseEvent):void
                MovieClip(parent).gotoType();
            private function addedHandler(event:Event):void
                this.x = stage.stageWidth / 2;
                this.y = stage.stageHeight / 2;
            private function removedHandler(event:Event):void
            private function back(event:MouseEvent):void
                MovieClip(parent).gotoLearn();

  • Textfield-focus within a tabbedpane

    Hi all,
    I have the following focus-problem.
    I have a tabbed-pane with 4-register-cards. First I'm intit the first at last the fourth.
    Now I want that a text-field on the first register-card have the focus. I try it to do it this way:
    public void stateChanged(ChangeEvent event) {
         if (tabbedPane.getSelectedIndex()==3) {
              if (textFieldUser!=null){
                   textFieldUser.setText(" This text-output works well ! ");
                   textFieldUser.requestFocus();
    Now the textfield display the output-text, but the field themselves have no focus?
    If I click on the tab No 2 and go back to No. 1 the field have the focus in the way that I want !?

    Archana1604 wrote:
    You need not use requestFocusInWindow to set the focus for a component. You can simple use requestFocus() inorder to set the focus.Please read the API and don't post bad advice.
    JComponent#requestFocus
    Note that the use of this method is discouraged because its behavior is platform dependent. Instead we recommend the use of requestFocusInWindow(). If you would like more information on focus, see [How to Use the Focus Subsystem|http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html], a section in The Java Tutorial.
    db

  • How to make scrollpane scroll with textfield focus

    I have a JScrollPane that contains a JPanel with a grid of Labels and JTextFields for data entry. When I tab through the textfields down the panel - reaching fields that are out of view, the scroll pane does scroll down fields with the cursor - keeping the cursor in view.
    Is there a way to do this? I've tried some things, but I have not been able to get this working.

    here you go..
    package com.u2d.focustest;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.FocusEvent;
    import java.awt.event.FocusAdapter;
    public class KeepCursorInView extends JFrame
       public KeepCursorInView()
          JPanel contentPane = (JPanel) getContentPane();
          JPanel fieldPanel = new JPanel(new GridLayout(12, 3));
          JScrollPane scrollPane = new JScrollPane(fieldPanel);
          contentPane.add(scrollPane, BorderLayout.CENTER);
          for (int i=0; i<36; i++)
             addField(fieldPanel);
       private void addField(final JPanel panel)
          final JTextField tf = new JTextField();
          panel.add(tf);
          tf.addFocusListener(new FocusAdapter()
             public void focusGained(FocusEvent e)
                Rectangle bounds = tf.getBounds();
                panel.scrollRectToVisible(bounds);
       public static void main(String[] args)
          JFrame f = new KeepCursorInView();
          f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          f.setBounds(100,100,400,400);
          f.setVisible(true);
    }

  • IOS multiline textfield - enter key

    I'm having a weird bug on iOS
    I create 2 dynamic text fields, 1 multiline, 1 singleline. I run this on my ipad, I give the textfields focus by clicking on them to pull up the softkeyboard.
    The multiline textfield will have an enter button that say "Return"
    The singleline textfield will have an enter button that says "Done"
    add a listener for keyup
    stage.addEventListener(KeyboardEvent.KEY_UP, this._onKeyUp);
    private function _onKeyUp($event:KeyboardEvent):void{
                switch($event.keyCode){
                    case Keyboard.ENTER:
                    trace("HELLO?");
              break;
    The trace will only work when you press "Done", it never fires for "Return"

    Hi,
    Yes it's always allowed, however the problem comes when you then try to output it using an outputText, it won't show the line feed as \n is ignored in HTML. You have to use Trinidad's outputDocument component for that.
    Regards,
    ~ Simon

  • How can I use a FocusEvent to distinguish among editable JComboBoxes?

    Hi,
    I have a Frame with multiple editable JComboBoxes, but I am at a loss as to how to sort them out in the focusGained() method.
    It is easy when they are not editable, because the FocusEvent getSource() Method returns the box that fired the event. That lets me read an instance variable that is set differently for each box.
    But with editable boxes, the FocusEvent is not fired by the box. It is fired by a Component object returned by getEditor().getEditorComponent(). So far I cannot find a way to query that object to find the box it it tied to. (I hope this isn't going to be painfully embarassing.).
    Anyway, the code below produces a frame with four vertical components: a JTextField (textField), a NON-Editable JComboBox (comboBox1) and two Editable JComboBoxes (comboBox2 & comboBox3).
    This is the command screen produced by :
    - Running the class
    - Then tabbing through all the components to return to the text field.
    I am not sure why, but it gives the last component added the foucus on startup.Focus Gained - Begin: *****
       This is the comboBox that Is Not Editable
    Focus Gained - End: *******
    Focus Gained - Begin: *****
       This Is The TextField
    Focus Gained - End: *******
    Focus Gained - Begin: *****
       Class: class javax.swing.plaf.metal.MetalComboBoxEditor$1
       Name: javax.swing.plaf.metal.MetalComboBoxEditor$1
    Focus Gained - End: *******
    Focus Gained - Begin: *****
       Class: class javax.swing.plaf.metal.MetalComboBoxEditor$1
       Name: javax.swing.plaf.metal.MetalComboBoxEditor$1
    Focus Gained - End: *******
    Focus Gained - Begin: *****
       This is the comboBox that Is Not Editable
    Focus Gained - End: *******
    Focus Gained - Begin: *****
       This Is The TextField
    Focus Gained - End: *******As you can see, the FocusEvent source for both editable boxes is a MetalComboBoxEditor. Both have identical names.
    Can anyone help me get from there back to the actual combo box so I can read the instance variable to see which one fired the event?
    The (painfully tedious and inelegant ) code that produced the above output is:import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.event.*;
    public class TestListeners extends JFrame
      implements ActionListener, DocumentListener,
                             FocusListener,  ItemListener {
      // Constructor
       TestListeners () {
          super ();
          panel.setLayout (new GridLayout (4, 1));
          textField.addActionListener (this);
          textField.getDocument ().addDocumentListener (this);
          textField.addFocusListener (this);
          panel.add(textField);
          comboBox2.addActionListener (this);
          comboBox2.getEditor().getEditorComponent ().addFocusListener (this);
          comboBox2.addItemListener (this);
          comboBox2.setEditable (true);
          panel.add (comboBox2);
          comboBox3.addActionListener (this);
          comboBox3.getEditor().getEditorComponent ().addFocusListener (this);
          comboBox3.addItemListener (this);
          comboBox3.setEditable (true);
          panel.add (comboBox3);
          comboBox1.addActionListener (this);
          comboBox1.addFocusListener (this);
          comboBox1.addItemListener (this);
          comboBox1.setEditable (false);
          panel.add (comboBox1);
          this.getContentPane ().add(panel);
          this.setVisible (true);
          pack ();
       // Nested class
        public class CB extends JComboBox {
           // Nested Constructor
          public CB  (Vector items, String str) {
             super (items);
             this.type = str;
          public String type;
       // Instance Members
       JTextField     textField = new JTextField ("Test Listener TextField");
       JPanel panel  = new JPanel ();
       String[] str = {"one", "two", "three"};
       Vector items = new Vector (Arrays.asList (str));
       CB comboBox1 = new CB (items, "Is Not Editable");
       CB comboBox2 = new CB (items, "Is Editable 2");
       CB comboBox3 = new CB (items, "Is Editable 3");
       // Methods
       public static void main(String args[]) {
          TestListeners frame = new TestListeners ();
       public void actionPerformed (ActionEvent ae) {
          System.out.print ("ActionEvent: This is ");
           if (ae.getSource ().getClass () == CB.class) {
             System.out.print ( ((CB) ae.getSource ()).type + " ");
          System.out.println (" "+ae.getActionCommand() + "\n" );
       public void focusGained (FocusEvent fge) {
          System.out.println ("Focus Gained - Begin: ***** ");
          if (fge.getSource ().getClass () == CB.class) {
          System.out.println ( "   This is the comboBox that "+((CB) fge.getSource ()).type);
          } else if (fge.getSource ().getClass () == JTextField.class) {
             System.out.println ( "   This Is The TextField");
         } else {
             System.out.println ("   Class: "+fge.getSource ().getClass());
             System.out.println ("   Name: "+fge.getSource ().getClass ().getName ());
         System.out.println ("Focus Gained - End: *******\n*\n");
       public void focusLost (FocusEvent fle) { }
       public void changedUpdate (DocumentEvent de) { }
       public void insertUpdate (DocumentEvent de) { }
       public void removeUpdate (DocumentEvent de) { }
       public void itemStateChanged (ItemEvent ie) { }
    }

    I added the following in your focusGained() method and it seemed to work:
    Component c = ((Component)fge.getSource ()).getParent();
    if (c instanceof JComboBox)
         JComboBox cb = (JComboBox)c;
         System.out.println("Selected: " + cb.getSelectedItem());
    }

  • Need a bit of help with css and fullscreen

    I am currently doing a javaFX application for my university project, I have it everything nearly finished, but need to finish the css component to make the application "prettier" (I am not very good on graphic design to be honest). So if i could get a bit of help on this little issue I'd be very grateful.
    is there any way to make fullscreen (and if possible resizing window) to instead rearranging everything to actually do a fullscreen (like the games) and everything "grows accordingly (even though in games what it usually does is to change the screen resolution, is that possible to reproduce with javaFX?) also how to remove the message and the effect on click the "esc" key to exit the fullscreen mode?
    i know that removing the focus effect on an element is with the following (if a button)
    .button:focused{
         -fx-background-insets: 0;
    }but,is there any way to remove the effect on anything focused (TextField, Combo Box, ...)? (tried with a .textfield:focused but it did not work)
    also i wanted to produce the focused effect by this way but it didn't work, how should i do it? (in fact even if i try to put this line on the button:focused, the focused effect gets removed from there, because of the insets line)
    #highlight{
         -fx-background-insets: 2;
         -fx-background-color: yellow;
    public class controller extends StackPane implements Initializable{
         public void highlight(){
              this.getStyleClass().add("highlight");
    and last thing (for the moment) the .button seems to work for all the buttons, but trying another thing like .gridpane or .textfield or .scrollpane does not seem to work, is there any way to make it work or i should add "id" to all the elements and use the # instead?

    i wrote all them in the same thread becsause there were a total of 4 (and could had been more) separated by ----
    should i leave it how it is or open now 4 threads for each question?

  • How can I stop page scrolling to form on loading?

    Hello.
    My page has a list of services available on it and then right at the bottom there is a form people can complete with name and email to contact me if they wish.
    The problem is that this is quite a way down the page and on loading, the page scrolls down to the bottom automatically with the caret flashing in the Name field of the form.
    How can I prevent this from happening.
    Thank you
    Terry

    Sounds like you have a bit of javascript adding focus to the form onload.
    A link would be best, but if you see something like this in your <body> tag, deleting it should do the trick...
    <body onload="document.form.textfield.focus();">

  • JSpinner validation, how?

    Sure seems like there should be SOME way to make this work.
    I've tried a FocusListener on the JSpinner, and on it's Editor. Never gets called.
    I've tried an InputVerifier on JSpinner, never gets called.
    I've tried setting the FocusPolicy to COMMIT. That commits the value, but the inputVerifier never gets called.
    Does anyone have a Known WORKING technique to do value validation on a JSpinner using the NumberModel?
    When you click or tab away from a JSpinner that you just typed a number into, I would like to validate that value. If it's over the max, I'll set it to the max, if it's under the min, I'll set it to the min.
    And note, I'm talking about the user TYPING into the field, not using the spinner buttons.
    It seems like SOMEBODY will have figured out how to do this.
    In the process of trying to figure this out, I've added practically every type of listener known to mankind to the JSpinner tree.
    Many get called during initialization, but NONE while editing the values of the Spinner. (Even using the spinner buttons doesn't trigger any of them.)
    (All these listeners do is print out a message saying their method has been called.)
              m_Spinner.addChangeListener(new CompChangeListener("Spinner"));
              m_Spinner.addComponentListener(new CompComponentListener("Spinner"));
              m_Spinner.addFocusListener(new CompFocusListener("Spinner"));
              m_Spinner.setInputVerifier(new CompInputVerifier("Spinner"));
              m_Spinner.addPropertyChangeListener(new CompPropertyChangeListener("Spinner"));
              JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) m_Spinner.getEditor();
              editor.addComponentListener(new CompComponentListener("Editor"));
              editor.addFocusListener(new CompFocusListener("Editor"));
              editor.addPropertyChangeListener(new CompPropertyChangeListener("Editor"));
              editor.setInputVerifier(new CompInputVerifier("Editor"));
              editor.addVetoableChangeListener(new CompVetoableChangeListener("Editor"));
              JTextField textfield = editor.getTextField();
              textfield.addActionListener(new CompActionListener("TextField"));
              textfield.addComponentListener(new CompComponentListener("TextField"));
              textfield.addFocusListener(new CompFocusListener("TextField"));
              textfield.addPropertyChangeListener(new CompPropertyChangeListener("TextField"));
              textfield.setInputVerifier(new CompInputVerifier("TextField"));
              textfield.addVetoableChangeListener(new CompVetoableChangeListener("TextField"));
              Document doc = textfield.getDocument();
              doc.addDocumentListener(new CompDocumentListener("Document"));
              doc.addUndoableEditListener(new CompUndoableEditListener("Document"));
              SpinnerNumberModel model = (SpinnerNumberModel) m_Spinner.getModel();
              model.addChangeListener(new CompChangeListener("Model"));

    That's odd, as my code does this:
              JSpinner.NumberEditor editor = (JSpinner.NumberEditor) m_Spinner.getEditor();
              JFormattedTextField textfield = editor.getTextField();
              textfield.addFocusListener(new CompFocusListener("JTextField"));
         private class CompFocusListener implements FocusListener {
              private final String     m_WhatFor;
              public CompFocusListener(String string) {
                   m_WhatFor = string;
                   return;
              public void focusGained(FocusEvent arg0) {
                   if (Logger.isEnabled) logger.debug("focusGained: " + m_WhatFor);
                   return;
              public void focusLost(FocusEvent arg0) {
                   if (Logger.isEnabled) logger.debug("focusLost: " + m_WhatFor);
                   return;
         }Then the JSpinner is added to a GridBagLayout and the panel displayed.
    When I tab (or click) away (or to) the control, I don't get any output.
    (And yes, the logger is working, as my other things are getting logged just fine.)
    I'm using Java 1.6.0_15
    So, I just went and removed ALL the other listeners I'd added while trying to find the problem.
    Now it works, when I only add the FocusListener to the textfield.
    So I spent a chunk of time trying to figure out WHY it wasn't working with the other traces in there.
    Turns out that doing the following is what killed it:
              m_Spinner.setModel(new AnaylzeNumberModel());All that AnalyzeNumberModel does is override all the SpinnerNumberModel methods (and AbstractSpinnerModel methods) to print a message then call the base class.
    SOMEHOW that prevents the textfield focus listener from working.
    Ahh, but it only causes a problem if you call setModel() AFTER setting the listener on the textfield.
    If you call SetModel() before adding the listener to the textfield, the focus listener continues working.
    I'd bet that changing the model causes the editor/textfield to be replaced as well, so none of the listeners I had set previously were actually being used.
    I could have sworn it wasn't working in an earlier version before I added the NumberModel subclass, but since I didn't save every output log, I can't be sure.
    This is reinforced by that line being near the end of my test routine, so it was one of the last things I tried.
    This is just too weird (and damn annoying).
    (Who knows, maybe shutting down and restarting Eclipse made a difference as well.)

  • How to set a Focus to a TextField?

    Suppose I have a 3 text field. After entering some data in the first text field, when I press enter key I want to set focus on the next text field. ie., instead of using TAB key I want to operate it using Enter Key. Similarlly, from the second Text field to third text field using Enter Key. Please help me in this event. If possible, please sent me a sample code.
    email id is [email protected]
    Regards
    Suresh Babu
    India

    Suppose I have a 3 text field. After entering some
    data in the first text field, when I press enter key I
    want to set focus on the next text field. ie., instead
    of using TAB key I want to operate it using Enter Key.
    Similarlly, from the second Text field to third text
    field using Enter Key. Please help me in this event.
    If possible, please sent me a sample code.
    email id is [email protected]
    Regards
    Suresh Babu
    IndiaTo send you a sample code, this fucntion is no big deal. It is very easy. I am assuming that you have a listener to each of your textfields. Let us say you have three text fields...textOne, textTwo, textThree.
    In the action listener of the textOne text filed:
    textOne.addActionListener( new ActionListener()
        public void actionPerformed(ActionEvent ae)
          String text = textOne.getText();
          textTwo.requestFocus();
      }So you basically do this for your textOne and textTwo text fields' action listeners.
    Got the concept?
    Vijay :-)

  • Default Focus on TextField

    Hello All,
    I have got a problem, please help me. I have a textfield in a frame. When call that frame the textfield will be displayed with default focus on it. I dont want to have the cursor in that text field. I dont have any other swing components in that and the text field is the first component in that frame.
    Please help me....
    Thanks in advance

    Hello
    Thanks for your reply. But i my code is some thing like this.
    public class TextFieldExample extends JFrame
         public JTextField textField;
         public JButton enterButton;
         public TextFieldExample()
              super();
              textField = new JTextField(25);
              JPanel jp = new JPanel();
              jp.add(textField);
              getContentPane().add(jp);
              setLocationRelativeTo(this);
              pack();
         public static void main(String args[])
              TextFieldExample textFieldExample = new TextFieldExample();
              textFieldExample.setVisible(true);
              textFieldExample.addWindowListener(new WindowAdapter()
                   public void windowClosing(WindowEvent we)
                        System.exit(0);
    Now Please suggest me the solution.

  • How to set focus on a textfield within a tabbedpane?

    Hallo i have the following problem,
    jTextFieldMyTextField.setText("BlaBla"); -> put the string in the TextField
    jTextFieldMyTextField.requestFocusInWindow(); -> don't set the focus to the TextField
    how i must set the focus?
    Any help is appreciated

    Archana1604 wrote:
    You need not use requestFocusInWindow to set the focus for a component. You can simple use requestFocus() inorder to set the focus.Please read the API and don't post bad advice.
    JComponent#requestFocus
    Note that the use of this method is discouraged because its behavior is platform dependent. Instead we recommend the use of requestFocusInWindow(). If you would like more information on focus, see [How to Use the Focus Subsystem|http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html], a section in The Java Tutorial.
    db

  • Stage.focus doesn't show cursor in TextField?

    stage.focus = txt; // txt is a text input field
    It successfully gives the TextField "txt" focus because when
    I type, the characters appear in the input textfield, however the
    familiar blinking text cursor/beam does not appear in the
    textfield. This makes you think it does not have focus, so you
    click the textfield to give it focus -- completely defeating the
    purpose. What's the deal?

    Doing some searches online I found myself asking this
    question over at asctionscript.org over a year ago. :)
    http://www.actionscript.org/forums/showthread.php3?t=143111
    No one figured it out back then but I revived the thread and
    I think someone identified the issue:
    The SWF must have system focus for the blinking text
    cursor/carat to appear.
    In the browser, SWFs do not have focus until you click on it,
    or force focus using javascript's focus() function.
    What I don't understand is why the Flash Player in the IDE
    doesn't start out with focus -- it seems to start out with a sort
    of half-focus.
    The project I'm working now is in AIR, though. Again, it's
    surprising that the AIR SWF doesn't start out with focus, but I
    also have no idea how to programatically give the SWF focus from
    AIR.

  • How can I set focus on TextField?

    Hi,
    I can't find a method or solution to set focus on TextField,I'd like to set the cursor into a special TextField when user enter my form.how can i implement this functionality?
    Any suggestion?
    Hanlin Li.
    Edited by: noob on Apr 13, 2012 4:28 PM

    textField.requestFocus();

Maybe you are looking for

  • Doubts in ALV?

    Hi, I have few doubts in Reuse_Alv_List_Display. 1. How to use It_Filter? 2. Their is a direct filter option in he list display. then what is the need of this It_Filter? Thanks.

  • Can I sync my iPhone to my Mac when it's already synced to a PC?

    I recently moved and the PC that my iPhone is synced to is in storage in another part of the state. I have my Macbook and I'm wondering if I can sync my iPhone to this or if it will create a problem or cause me to lose all my data? My phone has been

  • OEM 10g - ORA-12505: TNS:listener does not currently know of SID...

    Hello, In OEM (version 10.1.0.5 on Red Hat linux 4), on the page Database Instance: dbexemple.domain.be It says: - Database Instance > up - Listener > up - Agent connection to database > Status: Failed Details: Failed to connect to database instance:

  • DAQ Assist Vi freezes program

    I have a progam that takes data in from a daq for one minute while writing it to a file.  While testing it I noticed that I can have it take multiple minutes of data (via a start switch) while continuously running the program as long as I remain in t

  • MX 7.01 Server Lockup due to down database

    We have two datasources A) Standard SQL database, in the same domain B) SQL database pulled accross a VPN. Then we have a two load balanced CF servers. We host multiple websites, one website (We'll call it Bob) connects to both database A and B The r