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());
}

Similar Messages

  • How can i use original audio track in video editing for iPad 2?

    I am struggling to understand how the video editing apps work for iPad 2, and specifically, to understand why all the editing apps I have looked at explain how you can make these terrific video clips by ADDING audio tracks from iTunes, or. From the audio library. I just want to use the original audio on the tape?  Is that so strange? Today I took a video clip of my daughter singing with her guitar. I wanted to shorten the length a bit, so I downloaded a couple of Visio editing apps. When I pulled the clip out of my Camera Roll storage into the video editing screen, there was no sound at all,though there were ways to add other sounds ( for .99cents) most frustrating is that nowhere, does Apple explain this.  The tutorials just show someone editing a video of their friends ( with no sound) And adding text and music, as if it's normal not to include the actual sound that originally accompanied the video clip.  Seems like i wouldn't be the only customer who would prefer to hear my daughters own voice Ina video of her singing, and would like thisexplainedbefore I started downloading editing apps. If it can't be done, couldn't This be posted?

    I am not understanding your question, or your complaint, if it is a complaint.
    I movies takes a video clip, including its audio track. 
    You have the option of mutimg it, or of importing some other sound from your music library, or narrating.  What other option do you want?

  • How can I use ALL audio tracks in multiclip editing?

    I want to be able to use all audio tracks after the multiclip cut.
    It's only a two camera shoot, but I want to be able to use the ambient room sound from the second camera ... and control its level (in relation to the master audio track on the first camera) AFTER the cuts are made.
    Is this possible in multiclip editing? And how does one go about it?
    Apologies. I've spent an hour or so in the manual, and it isn't clear ... or I'm just not finding the right reference.
    All ears,
    Ben

    Sorry if I'm steering you down a path you don't want to go or even if there's a better way but I wouldn't be ding complex audio mixing anywhere near the multiclip interface. Export your reference movie or nest the multiclip project into another sequence, bring in your additional tracks, get to work. Just my preferences, I put a bunch of rough keyframes in the audio tracks and enter some markers (and make a lot of paper notes). Then I go back using the large mixer window to do the fine tweaking.
    Stick around for a couple of days, some other multiclip users with more experience than I will offer some more useful suggestions.
    bogiesan

  • What is User flag and how can i use it with the mails?

    Hi.
    i do wana know what is meant by user flag and how it is different from System flag.?
    can i use user flag to distinguish the mail. in my application?
    Thanks,
    Dushyant Kumar

    System flags are defined by the IMAP spec and every server has to support them.
    User flags are defined by the application. Different IMAP servers may have different
    limitations on user flags.

  • How can I use 2 Apple IDs in Itunes? I have 2 IOS Devices. They each have there own AppleID. What is the proper way to sync both of them to Itunes?

    How can I use 2 Apple IDs in Itunes? I have 2 IOS Devices. They each have there own AppleID. What is the proper way to sync both of them to Itunes? I wanted my teenager's AppleID to be different from mine so that she couldn't charge stuff to my AppleID, therefore I created me another one. Now when I go to Sync either device, it tells me that this IOS device can only be synced with one AppleID. Then I get a message to erase it, not going to do that, lol. If I logout as one ID and login as the other, will it still retain all synced information on the PC from the first IOS device? If I can just log in out of the AppleID, then I have no problem doing that as long as the synced apps, music, etc stays there for both. I am not trying to copy from one to the other, just want to make sure I have a backup for the UhOh times. If logging in and out on the same PC of multiple AppleIDs is acceptible then I need to be able to authorize the PC for both devices. Thanks for the help. I am new to the iOS world.

    "Method Three
    Create a separate iTunes library for each device. Note:It is important that you make a new iTunes Library file. Do not justmake a copy of your existing iTunes Library file. If iTunes is open,quit it.
    This one may work. I searched and searched on the website for something like this, I guess I just didn't search correctly, lol. I will give this a try later. My daughter is not be back for a few weekends, therefore I will have to try the Method 3 when she comes back for the weekend again. "
    I forgot to mention that she has a PC at her house that she also syncs to. Would this cause a problem. I am already getting that pop up saying that the iPod is synced to another library (even though she is signed in with her Apple ID to iTunes) and gives the pop up to Cancel, Erase & Sync, or Transfer Purchases. My question arose because she clicked on "Erase & Sync" by mistake when she plugged the iPod to her PC the first time. When the iPod was purchased and setup, it was synced to my PC first. When she went home, she hooked it up to her PC and then she erased it by accident. I was able to restore all the missing stuff yesterday using my PC. However, even after doing that it still told me the next time I hooked it up last night that the iPod was currently synced with a different library. Hopefully, you can help me understand all this. She wants to sync her iPod and also backup her iPod at both places. Both PCs have been authorised. Thanks

  • How can i use my account without the billing info, as i do not have a credit card. and my shipping and billing info is under US. i'm in singapore. how do i change this?

    how can i use my account without the billing info, as i do not have a credit card. and my shipping and billing info is under US. i'm in singapore. how do i change this?

    If you are just visiting Singapore, then leave the account as it is. If you have moved there, then view your account using the iTunes app on a Mac or PC and change the country/region to your current location and address. If you do not have a bank card, you can fund your account using iTunes gift cards if available in Singapore.

  • How can I use one photo twice in a slideshow

    I am in iPhoto '11 9.4.3 making a slideshow to send to iDVD for my mother-in-laws funeral next Wednesday.
    How can I use this one particular photo of her several times in the slide show.
    I have tried copy/past and that does not seem to work.
    Thanx in advance

    I would just duplicate the picture several times and keep inserting duplicates ...

  • How can i use microsoft word on ipad 2

    how can i use the microsoft application on my ipad 2?

    The Pages app is also compatible with MS Word. Unlike some of the others mentioned, the document is actually stored on the iPad as opposed to 'the clouds'. Again, most of those mentioned are cut down versions of MS Word.
    Stand back and look at this constructively; do you need to regularly access to the document from more than one computer/device without the need to keep amending your document on your computer and sending it to your iPad? If so, Documente to Go etc may be the best choice.

  • How can I use the credit on my account instead of credit card to buy apps?

    I have a gift card credit on my account but when I want to buy an app it takes me to my credit card details. How can I use my credit on my account instead of credit card?

    You shouldn't have to put in a credit card, no, but from time to time the iTunes Store seems for some reason to try to insist on one. If just confirming your mailing address doesn't stop it from complaining, go here:
    http://www.apple.com/support/itunes/contact/
    and follow the instructions to report the issue to the iTunes Store.
    Regards.

  • How can i use one SQL statement to solve problem?

    How can i use one SQL statement to solve the question below?
    For a Table named A, there is a column named F(char type).
    Now select all the records where F like '%00' and update their F value to '%01'
    Just one SQL statement.Do not use PL/SQL block.
    How to do that?
    Thanks.

    What is the data volume for this table?
    Do you expect lots of rows to have '%00' as their value?
    Following two statements come to mind. Other experts would be able to provide better alternatives:
    If you have index on SUBSTR(f, 2):
    UPDATE A
    SET    f = SUBSTR(f,
                      1,
                      length(f) - 2) || '01'
    WHERE  substr(f,
                  -2) = '00';If most of the rows have pattern '%00':
    UPDATE A
    SET    f = SUBSTR(f,
                      1,
                      length(f) - 2) ||
               DECODE(SUBSTR(f,
                             -2),
                      '00',
                      '01',
                      SUBSTR(f,
                             -2));

  • How can i use one iCloud Photostream on 2 devices?

    Due to a bug (or feature) on iOS 8 i can't use the same icloud id on 2 iphones:
    https://discussions.apple.com/thread/6536998
    So how can i use ONE iCloud Photostream then on 2 devices?
    Also, on OSX i can define only one Photostream.
    So, how do you import pictures from 2 streams into iPhoto?
    Familyshare is not usefull as the pictures are not in Original-Filesize!

    Hi ..
    Yes. " iPhone 6 or 6 Plus.  iPad Air 2  or  iPad mini 3"
    Set up and use Apple Pay

  • HT204053 Dear Support Team, every time i tried to logon Icloud its gives me wrong user name or password and at the end it show me error " This Apple ID is valid but is not an ICloud Account" then how can i use one account for same Apple ID and ICloud???

    Dear Support Team,
    Every time i tried to logon Icloud its gives me wrong user name or password and at the end it show me error " This Apple ID is valid but is not an ICloud Account" then how can i use one account for same Apple ID and ICloud?
    Thanks

    It is not possible to create a new iCloud account using a Windows machine. You must create the account using a Mac (10.7.5 or more) or an IOS device (iPhone etc). Once that is done you can sign into and use the account on your Windows machine.

  • How can I use same account in 2 iphone with diff contact list?

    how can I use same account number but other all totally diff such as contact list, application, everything ..... I found that it's bad on IOS5.
    when I used IOS4 .. old verison, I can keep my phone in diff data. Afte upgrade IOS 5, it's bad. Even take photo, it can not focus zoom in and out.

    how can I use same account number but other all totally diff such as contact list, application, everything ..... I found that it's bad on IOS5.
    when I used IOS4 .. old verison, I can keep my phone in diff data. Afte upgrade IOS 5, it's bad. Even take photo, it can not focus zoom in and out.

  • How can i use my ipod on 2 computers without deleting my music

    how can i use my ipod on 2 computers without deleting my music on ipod?

    See here: Using iPhone, iPad, or iPod with multiple computers
    B-rock

  • How can I use home sharing between our iphones (create playlists from both accounts at once)

    How can I use home sharing between our iphones, so that we can create playlists with songs from both of our libraries?  We share a Macbook Pro (separate accounts) and each have the new iphone, but when I have my iphone and try to create a new playlist, I can only grab songs from one account.  Is there a way to create a playlist using both accounts since we are sharing?
    Does it have to be created within iTunes first on our Mac?

    The short answer: because Home Sharing isn't designed for sharing apps, and apps aren't designed to be shared.
    The longer answer:  Think of it like this...
    You download some music in iTunes.  With Home Sharing, another user can listen to it by streaming it over the network.  But the data itself is on your computer.  If you turn off the computer or take it off the network, the other user can't access it.
    But if you send another user the app, they aren't just streaming the data- they installed it on their iOS device.  Because they weren't the one that purchased it, such a transfer could easily be considered "piracy" or "stealing," and nobody likes that (least of all, Apple). 
    When iTunes sees this app, it obviously recognized it, as if saying, "Hey, how did you get this app on your device, if I have no record of you purchasing/downloading it from the iTunes store?"
    post edited to correct spelling errors

Maybe you are looking for

  • Best place for Contacts

    What have people found is the best place to use as their master contact list?  I use Outlook 2010 with gSyncit for Gmail. I really don't care about having my Facebook contacts in my Droid since I don't use Facebook that much. I've turned that out. Is

  • Domain Value Mapping with Text file

    Hai, I have done the Domain value mapping with the xml file to xml file and it is working fine. But in the case of Text file to Text file it is not working i.e. If the citiname is the first field it is domain value mapping and working fine. INPUT: Er

  • IDVD poor movie quality

    I know there are a few other discussions with the same sor of problem but hopefully someone can help with mine. Basically, I completed a 15 minute movie on imovie. Quality is brilliant on preview. I export to imovie, once again the quality on preview

  • How do i remove jammed cartridge?

    how do i remove a jammed cartridge without damaging the machine?

  • INK CARTRIDGES IN NEW COUNTRY NOT ABLE TO BE USED

    Hello - I bought my old Photosmart C6183 in South Africa about 8 years ago where the ink cartridges are numbered 177.  I am now living in Britain and I brought my printer with me - here, you don't get 177 catridges, you have to use cartridges numbere