Using a DocumentListener makes other components dissapear.

I was wanting to use a DocumentListener to cut down on some code but when I used it all the buttons and TextFields dissapeared. Just wondering if anyone has had the same problem. Here is some of the code.
// Only activate the button if there are more than 3 letters
Document doc = lettersField.getDocument();
doc.addDocumentListener(new MyDocumentListener());
Document doc2 = addField.getDocument();
doc2.addDocumentListener(new MyDocumentListener());
class MyDocumentListener implemets DocumentListener {
JButton = tempButton;
Document = tempDoc;
public MyDocumentListener() {
if(letters.hasFocus() ) {
tempButton = letters;
tempDoc = lettersField.getDocument();
} else if (addWord.hasFocus() ) {
tempButton = addWord;
tempDoc = addField.getDocument();
public void insertUpdate(DocumentEvent e) {
try {
if( (tempDoc.getText(0, tempDoc.getLength()) ).length() >= 3) {
tempButton.setEnabled(true);
} catch (BadLocationException badLoc) {
// Deactivate the button when there are less than 3 letters
public void removeUpdate(DocumentEvent e) {
try {
if( (tempDoc.getText(0, tempDoc.getLength()) ).length() < 3) {
tempButton.setEnabled(false);
} catch (BadLocationException badLoc) {
public void changedUpdate(DocumentEvent e) { }
}

First of all the first two lines of your MyDocumentListener class won't compile ("JButton = tempButton"? What's that supposed to mean?)
Second, the constructor for that class is testing whether certain fields have the focus. That only happens when you create the MyDocumentListener, so what's the point?
Mind-reading time again. I am guessing you want to associate a JButton with each instance of the listener, so that the listener can enable and disable it as necessary. So, do this:class MyDocumentListener implements DocumentListener {
  JButton associatedButton;
  public MyDocumentListener(JButton theButton) {
    associatedButton = theButton;
}Note that I didn't tell the MyDocumentListener what Document it was listening to. I don't have to do that because DocumentEvent has a getDocument() method that tells me that for each event. So use this as one of the methods in MyDocumentListener:public void insertUpdate(DocumentEvent e) {
  Document doc = e.getDocument();
  try {
    if(doc.getLength() >= 3){
// sorry about the >, it's supposed to be the greater-than symbol but it won't come out that way.
      associatedButton.setEnabled(true);
  } catch (BadLocationException badLoc) {
}Finally, in your main program do this to assign the listeners to the fields:lettersField.getDocument().addDocumentListener(new MyDocumentListener(letters));
addField.getDocument().addDocumentListener(new MyDocumentListener(addWord));

Similar Messages

  • How to make a calendar above above other components?

    Hi all:
    I made a button and when I clicked it,I hope to show a
    datechooser above other components!How should I do it besides make
    a popup titleWindow?I need help !! Thanks much!

    You can use
    //declare in a place so that it is avaialble outside the
    scope of a function
    var chooser:DateChooser;
    chooser = PopUpManager.createPopUp(this, DateChooser)
    and position the control near the button.
    Once you decide that you do not need the popup call
    PopUpManager.removePopUp(chooser);

  • I received a $15.00 gift card. All but $.99 has been used. I want to make other purchases but can't as I'll be over the .99 with the tax......Of course I have a credit card on file, someone please tell me what to do in order to make another purchase? Than

    I received a $15.00 gift card. All but $.99 has been used. I want to make other purchases but can't as I'll be over the .99 with the tax......Of course I have a credit card on file, someone please tell me what to do in order to make another purchase?
    Thanks.

    JaniNY wrote:
    I thought of that, but my question is does it automatically flip to the CC.
    Yes.
    If you have a gift card balance, iTunes will use that.
    If the total is more than that, the difference will be charged to your credit card.
    You need do nothing.
    See this -> iTunes Store: How to redeem a code
    Free song credits are redeemed first.
    If you have no free song credits, gift certificate, prepaid card, and allowance credits are used next.
    If your gift certificate, prepaid card, and/or allowance credit is less than the total cost of the purchase, your credit card is billed for the remaining balance.
    If you have no free song credits or gift certificates, prepaid cards, or allowance credits, your credit card is billed for the songs you buy.

  • How do I make a professional tenure portfolio using adobe X1 or other product?

    How do I make a professional tenure portfolio using Adobe XI or other product?

    No way with the free Reader. Perhaps with Acrobat, but you would probably need another program for the layout.

  • How to make GUI components feel the state of other objects?

    I need my JButtons in any place of GUI be enabled or disabled depend on states of several objects or events generated by some other objects. Would you recommend the common approach of how it can be implemented in Swing?

    Sorry for the delay, but the job that pay my bills is keeping me very busy today (taking a short break to reply to you) :)
    Yes, the observers observable pattern is a good way to decouple this (but it could be overkill if you only want to update the status of one button). You will need to have references to the button variable so you can enable it / disable it from another place. For example, let's say you have an Class that extends the Observable and performs your heavy processing on a separate thread (Please not than I don't use generics here, just trying to quickly show you how you may want to do this):
    import java.util.Observable;
    public class TaskMan extends Observable {
        private List comp; 
        public void TaskMan (final List aComp) {
            comp = aComp; // Keep track of the components you want to to toggle on / off
        public void notifyObservers(Object arg) {
                setChanged(true);
                super.notifyObservers(arg);
        public void doIt() {
            new Thread (new Runnable() {
                 public void run() {
                         notifyObservers(comp); // Dissable
                         // Your long consumming task here
                        notifyObservers(comp); // Enable
    }Then you have the Observer class that enable / dissable the buttons and other components:
    import java.util.Observer;
    import java.awt.Component;
    public class MyObserver implements Observer {
         public void update(Object observable, Object arg) {
                 if (! (arg instanceof List)) {
                     return;
                 List list = (List) arg;
                 for (Object elem: list) {
                        Component comp = (Component) elem;
                        EventQueue.invokeLater(new Runnable() {
                               public void run() {
                                     comp.setEnabled(! comp.isEnabled());
    }And finally your main class, where you create and put together your GUI, register your observers and you call your long time consuming job
    import java.util.*;
    public class MyApp {
        public static void main(String [] args) throws Exception {
                  List comps = new ArrayList();
                  JButton aBtn = new JButton("Hey dude!");
                  comps.add(aBtn);
                  TaskMan task = new TaskMan(comps);
                  task.addObserver(new MyObserver());
                  task.doIt();
    }Looks like you have several use cases there, maybe you want to try this approache first for some of them, adding more logic to the observer.
    Please reply back, let me know what you think.
    Regards,
    Jose.

  • I m asked to put credit card details whenever I try to make an applied for my iPad4. I donot use credit cards or other plastic money. Can't I make an id without it?

    I m asked to put credit card details whenever I try to make an applied for my iPad4. I donot use credit cards or other plastic money. Can't I make an id without it?

    follow this Creating an iTunes Store account without credit card - Support - Apple

  • Use MF60 for split the components needs in a horizon of several days

    Hi to all,
    I'm working in my Industry to migrate the SAP system from a Repetitive mode to a Discrete one, and trying to launch the use of KanBan and MF60 usage, I'm not doing fine.
    I want to launch MF60 usage, through the bases of using it to distribute the need of components reserve in a Process Order (or several) in a daily frequency.
    I know it is possible to use the pull-list to propose only the replenishment quantity to be used in a time horizon given, for all Process Orders that uses "these" components.
    I ask for guidelines, as I don't know if I need to start building Kan Ban signals through PK transactions, or if I need to reconfigure some Material Master Views, as for the moment I'm not able to split the need as it is only proposing me the overall need of the order, even though I have already upload Resources, Master Recipes and Schedules to obtain a distribution in the Process Order lifetime.
    From what I know, the desire view to work on is the MF60 "Staging type: WM release order parts".
    I'll appreciate any information, or documentation you can share
    Regards,
    David P

    Hi Almeida and thanks for the response.
    I'll try to explain myself in a practical way.
    Now that it has being upload the Master Data in terms of resources and master recipes to work on a SAP Discrete mode, then I am working on the replenishment of components of the BOM's that are "signed" at the Process Order creation.
    My current situation in an example will be: If I create a Process Order for a 7-day duration, it makes the components reserves for the total, nevertheless I want to configure "somewhere"-"somehow" in SAP to be able to use the MF60 transaction to distribute the needs in a given horizon.
    With the scenario I have now, if I run the MF60 transaction for "that" order, no matter the horizon, it propose me the replenishment of the total need of the components minus the product already consumed. And even more, I only can run the MF60 with the "SLoc Level" view, and I have notice the distribution I need should be viewed under the "WM RelOrd. Parts" which is giving me a blank data result.
    What will be my desire usage of the MF60, is to use it to propose me the needed components inside the given horizon, so if I give a 1-day horizon, and expect to get the components quantity for that day.
    I hope, my issue is more clear now
    Thanks
    David P   

  • Component always displays "under" other components......

    Hi there,
    I have a component derived from a "panel" - I'm adding it to the applet but it always displays itself "under" the other components in the applet, never above (which is where I want it).
    I've tried adding it last and adding it first - doesn't make any difference. When I add the component I set it to "setVisible(false)" and "setVisible(true)" when the user clicks a button. I'm a bit confused as to why its underneath everything else!
    Any ideas?
    Thanks.

    I am using my own paint methods indeed - I mean I'm calling "super" and then rendering the component - or calling "super" in a container component and rendering other stuff on the container afterwards.
    But this is all working on the version you see on the website - the one I'm working on is broken.
    btw. I'm using a "Panel" (derives from Container, its a lightweight thingy, nout to do with Schwing etc.).

  • Putting Components on top of other components

    Hi,
    I need some help with the following subject:
    from the super-class i need to place a component on top of all other components (or paint something on top of these), but when i add the component it shows below all other components.
    How can I make this work?

    sorry, but this was a little to essential.
    1. how does the paint method of the VXComponent look like?
    2. where and how do you add the other components, which are drawn on top of your component?
    3. actually you seem to use no LayoutManager?
    - I never did that but I'd guess it causes the container to draw the components just in the (reverse?) order in which they where added.
    if this is the case something like this should help:
    add(vx, 0),
    or
    add(vx, getComponentCount()-1);
    regards
    Spieler

  • The Firefox window doesn't open at all I can't run safe mode or make other trubleshot changes

    since yesterday 08/10/2011 the Firefox window doesn't open at all I can't run safe mode or make other trubleshot changes. When I click on the desktop short-cut the Firefox windows starts loading but before open is crushing.I have sent several messages to you but no answer and my Firfox is not working.

    Which version of Firefox and which OS are you using?
    There are other ways of opening safe mode given at http://kb.mozillazine.org/Safe_Mode - See if that helps.

  • Hi I am using an iphone 4 and its was working fine.  Presently its giving browsing error.  Whenever I am trying to browse internet by using safari or any other browser it retruns with "server not responding" Can somebody give me a solution for this ?

    Hi I am using an iphone 4 and its was working fine.  Presently its giving browsing error.  Whenever I am trying to browse internet by using safari or any other browser it retruns with an error message "server not responding" Even the same thing is happening with youtube also. The worst part is am able to check my mails, able to chat and so on... only thing not able to browse through the browser.  I have tried restore option also.  This is happening with both Wlan and Data con. too
    Please help...

    I do not really know much about this kind of problem, but i may be your internet connection.
    If you are on your own #G network then:
    Go to Settings > then enable "Airplane Mode". Count to 10 and then disable it.
    Then wait patiently until you get a good connection and then try again.
    If you are on a nearby Wi-Fi connection then:
    Go to Settings > Wi-Fi > then disable then enable after counting to 10. Make sure that you are connected and then try again.
    If all else fails, then you need to contact your provider for assistance.
    Sprint:888-211-4727
    AT&T:?
    Verizon:?

  • If you buy a itunes gift card, can you use it to make in game/app purchases?

    if you buy a itunes gift card, can you use it to make in game/app purchases?

    Thank you

  • I am trying to use songs I purchased from iTunes in my video using Windows Movie Maker.   Can anyone tell me how to do that?

    I am trying to use songs I purchased from iTunes, from my library if you will, in my video (slideshow) using Windows Movie Maker.
    Does anyone know if that is possible to do?   If so, would you please tell me how to do that? 

    Hello, a1p2r3. 
    Thank you for visiting Apple Support Communities. 
    There are a couple options available to you to transfer purchased media to your computer.  Below, I have included a couple articles that outline this process.
    iTunes Store: Transferring purchases from your iOS device or iPod to a computer
    http://support.apple.com/kb/ht1848
    Please note that this feature works only for items that were purchased on the iTunes Store. Any items imported from audio CDs or acquired from other sources will not copy from your iPod to the iTunes library.
    Download past purchases
    http://support.apple.com/kb/ht2519
    Cheers,
    Jason H.

  • HT201372 used this to make a bootable copy for a new hard drive installation. copied the install find but gave error code 110 when making the disk bootable.. Any help in what this code is

    Used createinstallmedis to make a copy of the Mavericks app for use in a new hard drive install.. Copied find but gave error code 110 and failed to make the flash drive bootable.. Any help in what errror code means?

    Did you partition and format the flash drive first? See the following:
    Make Your Own Mavericks, Mountain/Lion Installer
    After downloading the installer you must first save the Install Mac OS X application. After the installer downloads DO NOT click on the Install button. Go to your Applications folder and make a copy of the installer. Move the copy into your Downloads folder. Now you can click on the Install button. You must do this because the installer deletes itself automatically when it finishes installing.
       2. Get a USB flash drive that is at least 8 GBs. Prep this flash drive as follows:
    Open Disk Utility in your Utilities folder.
    After DU loads select your flash drive (this is the entry with the mfgr.'s ID and size) from the leftside list. Under the Volume Scheme heading set the number of partitions from the drop down menu to one. Set the format type to Mac OS Extended (Journaled.) Click on the Options button, set the partition scheme to GUID then click on the OK button. Click on the Partition button and wait until the process has completed.
    Select the volume you just created (this is the sub-entry under the drive entry) from the left side list.
    Click on the Erase tab in the DU main window.
    Set the format type to Mac OS Extended (Journaled.) Click on the Options button, check the button for Zero Data and click on OK to return to the Erase window.
    Click on the Erase button. The format process can take up to an hour depending upon the flash drive size.
    Make your own Mavericks flash drive installer using the Mavericks tool:
    Mavericks has its own built-in installer maker you use via the Terminal:
    You will need a freshly partitioned and formatted USB flash drive with at least 8GBs. Leave the name of the flash drive at the system default, "Untitled." Do not change this name. Open the Terminal in the Utilities folder. Copy this command line after the prompt in the Terminal's window:
    sudo /Applications/Install\ OS\ X\ Mavericks.app/Contents/Resources/createinstallmedia --volume /Volumes/Untitled --applicationpath /Applications/Install\ OS\ X\ Mavericks.app --nointeraction
    Press RETURN. Enter your admin password when prompted. It will not be echoed to the screen so be careful to enter it correctly. Press RETURN, again.
    Wait for the process to complete which will take quite some time.

  • Hi. I am trying to use Bridge to make web galleries. There used to be something on PS for that but now I cant find it in PS so I was advised to use Bridge.  However, there is something called Output Module (AOM) that is missing from my Bridge.  I download

    Hi. I am trying to use Bridge to make web galleries. There used to be something on PS for that but now I cant find it in PS so I was advised to use Bridge.  However, there is something called Output Module (AOM) that is missing from my Bridge.  I downloaded this.  The instructions say :Copy the Adobe Output Module folder (not the AOM_Package_[OS] folder) into the Bridge CC Extensions folder in the appropriate location: 
    Windows: C:\Program Files\Common Files\Adobe\Bridge CC Extensions
    Mac OS: /Library/Application Support/Adobe/Bridge CC Extensions.  However there is noplace on my Macbook Air running Yosemetie that has this /Library/Applications SUpport/Bridge CC Extensions. Can anyone advise?

    That file could be set as a Launch Agent or Daemon in your ~/Library or /Library, You could try to look for it or Safeboot your computer and then empty the trash. Safebooting disables Launch Agents/Daemon and Third Party Kernal extensions. Here's the article to Safeboot OS X: What is Safe Boot, Safe Mode? - Apple Support

Maybe you are looking for