When to call requestFocus()?

I have a panel with some JTextFields and I want to focus to one of them. I know that calls to java.awt.Component's requestFocus() function only work if the Component is already displayed.
My question is, when is the best time to call requestFocus()?
Should I explicitly figure out when this is getting drawn, and then drill back down to my panel to call requestFocus() on the field, or perhaps just hack and override the paint method of the panel like this?
    public void paint (Graphics g)
        super.paint(g);
        myTextField.requestFocus();
    }Any ideas?

* Focus_Test.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Focus_Test extends JFrame {
    public Focus_Test() {
        initComponents();
        setVisible(true);
        jTextField2.requestFocus();
        getRootPane().setDefaultButton(jButton1);
    private void initComponents() {
        jTextArea1 = new JTextArea();
        jPanel2 = new JPanel();
        jPanel1 = new JPanel();
        jLabel1 = new JLabel();
        jTextField1 = new JTextField();
        jLabel2 = new JLabel();
        jTextField4 = new JTextField();
        jLabel3 = new JLabel();
        jTextField2 = new JTextField();
        jLabel4 = new JLabel();
        jTextField5 = new JTextField();
        jLabel5 = new JLabel();
        jTextField3 = new JTextField();
        jLabel6 = new JLabel();
        jTextField6 = new JTextField();
        jButton1 = new JButton();
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setTitle("Focus Test (2)");
        jTextArea1.setBackground(new Color(255, 255, 204));
        jTextArea1.setEditable(false);
        jTextArea1.setText("Hit the Tab/Enter key and watch the Caret postition.");
        jTextArea1.setFocusable(false);
        getContentPane().add(jTextArea1, BorderLayout.NORTH);
        jPanel2.setLayout(new GridBagLayout());
        jPanel1.setPreferredSize(new Dimension(260, 105));
        jLabel1.setText("1 : ");
        jPanel1.add(jLabel1);
        jTextField1.setNextFocusableComponent(jTextField2);
        jTextField1.setPreferredSize(new Dimension(100, 19));
        jPanel1.add(jTextField1);
        jLabel2.setText("4 : ");
        jPanel1.add(jLabel2);
        jTextField4.setNextFocusableComponent(jTextField5);
        jTextField4.setPreferredSize(new Dimension(100, 19));
        jPanel1.add(jTextField4);
        jLabel3.setText("2 : ");
        jPanel1.add(jLabel3);
        jTextField2.setNextFocusableComponent(jTextField3);
        jTextField2.setPreferredSize(new Dimension(100, 19));
        jPanel1.add(jTextField2);
        jLabel4.setText("5 : ");
        jPanel1.add(jLabel4);
        jTextField5.setNextFocusableComponent(jTextField6);
        jTextField5.setPreferredSize(new Dimension(100, 19));
        jPanel1.add(jTextField5);
        jLabel5.setText("3 : ");
        jPanel1.add(jLabel5);
        jTextField3.setNextFocusableComponent(jTextField4);
        jTextField3.setPreferredSize(new Dimension(100, 19));
        jPanel1.add(jTextField3);
        jLabel6.setText("6 : ");
        jPanel1.add(jLabel6);
        jTextField6.setNextFocusableComponent(jTextField1);
        jTextField6.setPreferredSize(new Dimension(100, 19));
        jPanel1.add(jTextField6);
        jButton1.setText("OK");
        jButton1.setFocusable(false);
        jButton1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                jButton1ActionPerformed(evt);
        jPanel1.add(jButton1);
        jPanel2.add(jPanel1, new GridBagConstraints());
        getContentPane().add(jPanel2, BorderLayout.CENTER);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-415)/2, (screenSize.height-182)/2, 415, 182);
    private void jButton1ActionPerformed(ActionEvent evt) {
        if( jTextField1.hasFocus() ){
            jTextField2.requestFocus();
        }else if( jTextField2.hasFocus() ){
            jTextField3.requestFocus();
        }else if( jTextField3.hasFocus() ){
            jTextField4.requestFocus();
        }else if( jTextField4.hasFocus() ){
            jTextField5.requestFocus();
        }else if( jTextField5.hasFocus() ){
            jTextField6.requestFocus();
        }else {
            jTextField1.requestFocus();
    public static void main(String args[]) {
         new Focus_Test();
    private JButton jButton1;
    private JLabel jLabel1;
    private JLabel jLabel2;
    private JLabel jLabel3;
    private JLabel jLabel4;
    private JLabel jLabel5;
    private JLabel jLabel6;
    private JPanel jPanel1;
    private JPanel jPanel2;
    private JTextArea jTextArea1;
    private JTextField jTextField1;
    private JTextField jTextField2;
    private JTextField jTextField3;
    private JTextField jTextField4;
    private JTextField jTextField5;
    private JTextField jTextField6;
}

Similar Messages

  • DLL Wrapper works when functions called out of main(), not from elsewhere?

    Hello all,
    I am currently trying the JSAsio wrapper out ( http://sourceforge.net/projects/jsasio )
    Support on this project is nearly unexisting and a lot of people seem to complain that it doesn't work well.
    It works very nicely here, I wrote a few test classes which called some functions (like playing a sound or recording it) and had no problems whatsoever.
    These test classes were all static functions and ran straight out of the main() method and printed some results to the console.
         public static void main(String[] args)
              boolean result = callFunction();
              .. end..
         public static boolean callFunction()
              initASIO();
              openASIOLine();
              return true;
         }The results were all great!
    Then I tried to implement these test classes into my swing-based applications. So I want to call these same functions, as in the test classes, as a result of any user action (for example, selecting the asio driver in a combobox) But then these asio driver functions just stop to work. I get errors saying that the ASIO driver is not available. (meaning that the dll wrapper loads the wrong asio driver or can't load one at all)
    The library path and classpath are all set correctly, exactly the same as the test classes. Even copied the test code word for word in to my swing applications but it still will not work. I am calling these functions in a new Thread, and even put them in a static methods to try and get that working. When calling these asio methods from the main() method AFTER I set up my components gives me the desired results as well. But as soon as I call these same methods (which are in the same class) from a swing event, it fails;
    public class ASIOTest
         public static void main(String[] args)
              ASIOTest test = new ASIOTest();
              test.callFunction(); // <-- WORKS
         public ASIOTest()
              initializeComponents();
         private void initializeComponents()
              frame = new JFrame();
              choices = new JComboBox();
              choices.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent event)
                     // user made selection
                    new Thread(
                            new Runnable() {
                                public void run() {
                                    try
                                         callFunction(); // <-- DOES NOT WORK
                                    catch (Exception e)
                                        e.printStackTrace();
                            }).start();
         public void callFunction()
              initASIO();
              openASIOLine();
    }Is there something fundamental I am missing here?
    For what reasons can an application which uses JNI functions go wrong when working in a swing enviroment? (and out of a static context, although this does not seem to make any difference, eg. when calling these functions from static methods inside another class, inside a new thread when the user has generated an event)
    I am hoping someone could point me in the right direction :-)
    Thank you in advance,
    Steven
    Edited by: dekoffie on Apr 21, 2009 11:11 AM
    Edited by: dekoffie on Apr 21, 2009 11:16 AM

    jschell wrote:
    Two applications.
    And you probably run them two different ways.
    The environment is different so one works and the other doesn't.Thank you for your fast reply!
    Well, I am running the "updated" version from the same environment; I copied the jframe, and a jcombobox into my original test class which only ran in the java console. Consider my second code example in my original post as the "updated" version of the first code example. And as I pointed out, it works fine when I call the jni functions in the main method, but not when I call it from inside the ActionListener.
    Or am I again missing something essential by what you mean with a different environment? The classpath and the working directory is exactly the same, as is the Djava.library.path option. :-)
    Thanks again!

  • Help needed! Just want to cancel my year subscription. No answer from support via mail, noone answers when I call, support chat don't work.

    Help needed! Just want to cancel my year subscription. No answer from support via mail, noone answers when I call, support chat don't work.

    Hi there
    I'll pass your details to our Russian team and they'll contact you to assist further.
    Kind regards
    Bev

  • Why does my 3G iPhone no longer rings nor vibrates when receiving calls and text messages?

    Why does my 3G iPhone no longer rings nor vibrates when receiving calls and text messages?
    During a two weeks process, little by little I notice my phoe doesn't ring when I get a phone call.  So Far I have missed 22 calls that I am aware of.  Moreover, the phone does not alert me when I get a text message.  Sometimes I receive notice the next day that I received a call or a text message. 

    Follow the basic troubleshooting steps outlined in the user guide... reset, restore from backup, restore as new.

  • HT1414 Can anyone shed some light on my problem with iPhone 5 please? I am 'stuck' in recovery mode and do EVERYTHING that's been suggested online. (Even Apple at Bondi said 'Oh...um...oh that' doesn't sound too good.') when I called them for help.

    To say that I am livid would be the greatest understatement EVER made in the ENTIRE history of the planet. Last night my iPhone 5 decided it would go into Recovery Mode. (That is.....a Black screen showing the iTunes logo, a white 'up' arrow and with the white cable/plug at the bottom of the screen).
    (Hold onto your Samsung Galaxys people - do NOT throw/give them away when you 'update (yeh right) to an iPhone).
    So I turned it off and left it off all of last night. I arrived at work this morning to 'sync/restore' it with my computer where it asked me to install the latest version of 'whatever it is the latest version of.' HOWEVER, as I do not have the administrative rights to do such things on my work computer, I was unable to do so and now I have been riding the 'Apple Merry-Go-Round' since 0630hrs (WAST). It comes up with the following message in a window on the screen:-
    'The iPhone "iPhone" could not be restored. An unknown error occurred (14).'
    Gee - thanks for that - I'd never have known unless I saw that little window!!!! The fact that my screen has frozen and I am unable to install, sync or restore anything kind of gives it away!!!
    'Just go to the nearest Apple Store' I hear you say. That is a 'tad' hard as I work in The Pilbara Region of North West, Western Australia (for 4+ weeks at any one time) and won't be back in 'civilisation' until next Monday week (10th of Feb, 2014)........Talk about being held to ransom by technology!! I have asked other people at work (who ALL have iPhones) and when I showed them the screen (& the whole ridiculous process) they screwed their faces up as they had never seen such a thing. Some people NEVER get asked to update/install anything on their phones.....So, I have to wonder why mine 'asks me'.......constantly.
    I fear that my 'Apple' is not an apple but that it is actually a 'Lemon.'
    Hey 'Apple' - don't let me get a hold of you!!
    Oh yeh - the opening hours actually say Sun 10:00am til 4:00pm or even 6:00pm in some places.......how convenient - when you call (them on their landline FROM a landline) you get the engaged signal!

    Thanks for that 'sberman' - because my iPhone is backed-up to my work computer (only at this stage) I have had to call our IT Department in Adleaide. (4 times this morning). The last guy managed to get the phone into 'DFU Mode' - no more recovery mode screen - (kind of 'asleep' perhaps) from my understanding of same. I am awaiting a call again from IT so they can get my computer to actually recognise my iPhone on the C Drive. This also happened to  one of my colleagues in Newman (WA). She got so frustrated with the whole process that she bought another phone the next time she was in 'civilisation.' She hasn't had any problems since. (Cross fingers).
    Thanks again, Sandra2474.

  • I just downloaded the latest version of firefox (5.0). Can I delete all the older versions that are still on my drive? 2) When I call up some web sites sometimes the web site comes up 3 or 4 times...how do I stop this from happening?

    I just downloaded the latest version of firefox (5.0). Can I delete all the older versions that are still on my drive?
    When I call up some web sites sometimes the web site comes up 3 or 4 times...how do I stop this from happening? For example if I click on a link the exact same link comes up 3 or 4 times and I have to manually delete all of them but one.

    Do you organise your email into separate folders, or do you just let it accumulate in the Inbox?
    I would strongly advise you not to let it sit in the Inbox.
    When you see this happening, try right-click on the affected folder, select '''Properties''' then '''Repair Folder'''.
    Large folders (actually, a folder in Thunderbird is stored as a file, so it is sensitive to file size limits set by the underlying file system) can be problematic. This is one reason not to let messages collect in one folder. I use Thunderbird's Archive folders so that accumulated mails over several years can happily coexist because they are effectively stored in many small folders. No single mail store folder then exceeds the 2GB or 4GB sizes that have been known to stress the OS. I appreciate that current builds of Thunderbird and a modern 64-bit OS should be able to cope, but practically I find it slows down when given huge files, so I err towards a pragmatic solution; a large number of not very big files.
    Archives are searchable, and a Saved Search folder can give you a virtual composite folder allowing access to the entire Archive.

  • Why is it that when I call my MIL with FaceTime I end up calling another person?

    When I call my mother in law with FaceTime, I end up calling a total stranger. I use her number from my contacts and it is the right number, it works just fine if I make a regular phone call. Today, I called her and talked to her for some time and after a while we decided to switch to FaceTime. While on the phone with her, I pushed the FaceTime button on my screen, the call ended, FaceTime started and instead of my MIL it was a complete stranger that answered the call. What can I do to fix this problem?

    Check to see if you have an old phone number associated with this person in your address book. When you call someone on Facetime it calls ALL numbers/emails associated with that persons contact sheet! This was my problem, this stranger has my husband's old phone number!

  • Why is it that when someone calls my dad's phone, the incoming call shows up on both our screens?

    Whenever someone is calling my dad, we both get the call. It only happens when we are close. It only happens when the calls are for him. It happens even when the other person is not doing a multiple call. Does it have something to do with us having the same Wi-Fi? He has an iPhone 5 with iOS 8 and I have an iPhone 5c with iOS 8.

    It has to do with you and him sharing an Apple ID. This is not a good practice. Create a new Apple ID: http://appleid.apple.com.
    You could also go to Settings > FaceTime > iPhone Cellular Calls and turn if off, but ultimately you'll probably run into other problems down the road by sharing an ID.

  • How can I activate an old version of Photoshop that I've loaded on my new tablet? The web activation doesn't work, and when I call the number, it says it's not being used anymore. Meanwhile, I'm down to 13 days till it stops working due to not being activ

    How can I activate an old version of Photoshop that I've loaded on my new tablet? The web activation doesn't work, and when I call the number, it says it's not being used anymore. Meanwhile, I'm down to 13 days till it stops working due to not being activated. HELP?  I really need to continue using this product for my home business.It works fine not activated but the threat is that it will stop working in 13 more days if I don't get it activated, and none of the activation methods they list seem to work.

    The new serial number is to the right of your chosen download.

  • I got problem when trying to contact my iphone 4 using facetime,it did ringing,but my iphone 4 did not receive that call as if nothing happen.But,when the call ended,that call appeared in the missed call list.fyi both are connected to internet.

    i got problem when trying to call my iphone 4 using facetime with my Macbook Pro 13',it did ringing,but my iphone 4 did not receive that call as if nothing happen.But,when the call ended,that call appeared in the missed call list.fyi both are connected to internet.Help!! thank you in advance

    i got problem when trying to call my iphone 4 using facetime with my Macbook Pro 13',it did ringing,but my iphone 4 did not receive that call as if nothing happen.But,when the call ended,that call appeared in the missed call list.fyi both are connected to internet.Help!! thank you in advance

  • I cannot log onto the App Store, the page appears when I call it from the dock or the Apple menu. I can call the log-in box from the menu bar, but on signing in the spinning icon just keeps spinning,(not the beachball). Any suggestions? Using 10.6.7 and u

    I cannot log onto the App Store, the page appears when I call it from the dock or the Apple menu and I can call the log-in box from the menu bar, but on signing in the spinning icon just keeps spinning,(not the beachball).  Using 10.6.7 and up-to-date on all software. Any suggestions?

    Could be a third party app preventing you from being able to login.
    Mac App Store: Sign in sheet does not appear, or does not accept typed text
    If it's not a third party issue, follow the instructions here.
    Quit the MAS if it's open.
    Open a Finder window. Select your Home folder in the Sidebar on the left then open the Library folder then the Caches folder.
    Move the com.apple.appstore and the com.apple.storeagent files from the Caches folder to the Trash
    From the same Library folder open the Preferences folder.
    Move these files Preferences folder to the Trash: com.apple.appstore.plist and com.apple.storeagent.plist
    Same Library folder open the Cookies folder. Move the com.apple.appstore.plist file from the Cookies folder to the Trash.
    Relaunch the MAS.

  • Is anyone having problems with iPhone 5 not vibrating properly when receiving calls? It seems to vibrate once and stops?

    When receiving a call, my phone only vibrates once even if its on silent or not. I have checked all the settings and all appears ok. It works sometimes and doesn't other times. Does anyone know how this can be fixed?
    Thanks

    Maybe your buzzer gets stuck when it vibrates? Try playing it by creating a custom vibration. Does it keep working properly then?
    If that works, try setting up another vibration that should be played when someone calls you.

  • Hello i have a unusual problem,anyway my iphone 4 wont turn on and when someone calls they hear ringing,but my phone doesent ring,so i open cimcas now ring off. Screen is just black,because it's turned off..Please help me to solve this problem..Thanks

    Hello i have a unusual problem,anyway my iphone 4 wont turn on and when  someone calls they hear ringing,but my phone doesent ring.So i poen simcase and now ring off. Screen is just  black,because it's turned off..Please help me to solve this  problem..Thanks

    As far as trying to power up your device, make sure it has a good charge and then hold the button on the top of the phone and the home button on the faceplate together until the apple appears on the screen.

  • I have recently updated the operating system on my iPad. Now my alarm is silent and my FaceTime doesn't ring aloud when a call comes in. How do I fix this please?

    I have recently updated the operating system on my iPad. Now my alarm is silent and my FaceTime doesn't ring aloud when a call comes in. How do I fix this please?

    Do you see a half moon icon in the upper right corner of the screen? If so, you have Do Not Disturb enabled. Settings>Do Not Disturb. Turn it off.
    Or do you have system sounds muted?
    System sounds can be muted and controlled two different ways. The screen lock rotation can be controlled in the same manner as well.
    Settings>General>Use Side Switch to: Mute System sounds. If this option is selected, the switch on the side of the iPad above the volume rocker will mute system sounds.
    If you choose Lock Screen Rotation, then the switch locks the screen. If the screen is locked, you will see a lock icon in the upper right corner next to the battery indicator gauge.
    If you have the side switch set to lock screen rotation then the system sound control is in the control center. Swipe up from the bottom of the screen to get to control center. Tap on the bell icon and system sounds will return.
    If you have the side switch set to mute system sounds, then the screen lock can be accessed in the same manner as described above.
    This support article from Apple explains how the side switch works.
    About the iPad Side Switch - Apple Support

  • TS3367 I have two IPhones and one IPad all with the same Apple ID. I can call facetime between the two IPhones, but I cannot call the IPAD from either of the IPhones and when I call my IPhone from the IPad it rings my wife's IPhone after briefly calling m

    I have two IPhones and one IPad all with the same Apple ID. I can call facetime between the two IPhones, but I cannot call the IPAD from either of the IPhones and when I call my IPhone from the IPad it rings my wife's IPhone after briefly calling mine.

    Each device needs a separate address. Use an email address (gmail.com) on the iPad.
    Using FaceTime http://support.apple.com/kb/ht4319
    Troubleshooting FaceTime http://support.apple.com/kb/TS3367
    The Complete Guide to FaceTime + iMessage: Setup, Use, and Troubleshooting
    http://tinyurl.com/a7odey8
    Troubleshooting FaceTime and iMessage activation
    http://support.apple.com/kb/TS4268
    Using FaceTime and iMessage behind a firewall
    http://support.apple.com/kb/HT4245
    iOS: About Messages
    http://support.apple.com/kb/HT3529
    Set up iMessage
    http://www.apple.com/ca/ios/messages/
    Troubleshooting Messages
    http://support.apple.com/kb/TS2755
    Setting Up Multiple iOS Devices for iMessage and Facetime
    http://macmost.com/setting-up-multiple-ios-devices-for-messages-and-facetime.htm l
    FaceTime and iMessage not accepting Apple ID password
    http://www.ilounge.com/index.php/articles/comments/facetime-and-imessage-not-acc epting-apple-id-password/
    Unable to use FaceTime and iMessage with my apple ID
    https://discussions.apple.com/thread/4649373?tstart=90
    For non-Apple devices, check out the TextFree app https://itunes.apple.com/us/app/text-free-textfree-sms-real/id399355755?mt=8
    How to Send SMS from iPad
    http://www.iskysoft.com/apple-ipad/send-sms-from-ipad.html
     Cheers, Tom

Maybe you are looking for

  • Reporting.... Cost savings, open purchase orders

    I am new to SAP and I have so many questions and ZERO answers.   I would like to be able to find all my open orders (and find ones overdue) so I can follow up with the suppler.   My second report is tracking cost savings on min/max items.  Is there a

  • External Firewire Drive not Mounting

    Hi, I've been searching these forums but can't find an aswer to my question. I just got a new 27" iMac, and purchased a Thunderbolt-to-Firewire adapter. I have 3 external Firewire drives, two of which are DC powered (the third, I understand, might be

  • Alert Category in XI

    Hi...   I am doing file to file scenario using BPM. I have created an exception. For that i want to create Alert Category in Xl. Kindly help me out in this by detailed descripition of how to create alert category.

  • How do I Reinstall My Creative Suite Master Collection version CS6 onto my New Mac???

    Hello all,    After 2.5 years, my "new" now "old" macbook pro crapped out.  Well before it did, as a graphic designer, I purchased the CS6 Master Suite Collection.  I have my serial number, yet am unable to figure out how to reinstall the programs on

  • In service order

    what is logic to put purchase group in service order? where we do setting of purchase group for service order in config?