Share the same code between TextField's actionEvent() and focusedProperty.addListener()

Hello.
Following the DRY principle, I want the same validation piece of code to be executed in the actionEvent() and focusedProperty.addListener() of my TextField. I tried creating a private method and calling it in both handlers, but in the actionEvent handler I'm accessing the event object to play an animation on the textfield, which isn't passed in the changed() method of the listener for the focusedProperty. What's the best way to solve this without repeating code?

Yup, sorry; that's what comes from working off a phone without checking the API docs... It's a ReadOnlyProperty, not a Property.
This works:
import java.util.regex.Pattern;
import javafx.application.Application;
import javafx.beans.property.ReadOnlyProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ValidatingTextFieldExample extends Application {
@Override
  public void start(Stage primaryStage) {
  final VBox root = new VBox(5);
  final ChangeListener<Boolean> focusListener = new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observable,
                    Boolean oldValue, Boolean newValue) {
                if (! newValue) {
                    TextField textField = (TextField) ((ReadOnlyProperty<?>) observable).getBean();
                     doValidation(textField);
  final EventHandler<ActionEvent> actionHandler = new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                TextField textField = (TextField) event.getSource();
                doValidation(textField);
        TextField tf1 = new TextField();
        TextField tf2 = new TextField();
        tf1.setOnAction(actionHandler);
        tf1.focusedProperty().addListener(focusListener);
        tf2.setOnAction(actionHandler);
        tf2.focusedProperty().addListener(focusListener);
        root.getChildren().addAll(tf1, tf2);
        primaryStage.setScene(new Scene(root, 300, 150));
        primaryStage.show();
  private void doValidation(TextField textField) {
      if (isTextValid(textField.getText())) {
          textField.setStyle("");
      } else {
          textField.setStyle("-fx-text-fill: red;");
  private boolean isTextValid(String text) {
      // just digits are valid (very simple example)
      return Pattern.matches("^[0-9]*$", text);
  public static void main(String[] args) {
  launch(args);
But also consider this solution, which doesn't use the events at all, has no downcasting, but still fulfills your DRY principle:
import java.util.regex.Pattern;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ValidatingTextFieldExample extends Application {
@Override
  public void start(Stage primaryStage) {
  final VBox root = new VBox(5);
        TextField tf1 = new TextField();
        TextField tf2 = new TextField();
        registerValidationHandlers(tf1);
        registerValidationHandlers(tf2);
        root.getChildren().addAll(tf1, tf2);
        primaryStage.setScene(new Scene(root, 300, 150));
        primaryStage.show();
  private void registerValidationHandlers(final TextField textField) {
      textField.setOnAction(new EventHandler<ActionEvent>() {
          @Override
          public void handle(ActionEvent event) {
              doValidation(textField);
      textField.focusedProperty().addListener(new ChangeListener<Boolean>() {
          @Override
          public void changed(ObservableValue<? extends Boolean> observable, Boolean wasFocused, Boolean isFocused) {
              if (! isFocused) {
                  doValidation(textField);
  private void doValidation(TextField textField) {
      if (isTextValid(textField.getText())) {
          textField.setStyle("");
      } else {
          textField.setStyle("-fx-text-fill: red;");
  private boolean isTextValid(String text) {
      // just digits are valid (very simple example)
      return Pattern.matches("^[0-9]*$", text);
  public static void main(String[] args) {
  launch(args);
You could even make the registerValidationHandlers method a varargs method, which would even avoid replicating the call to that method...

Similar Messages

  • How to share the same session between JNLP Application and browser session

    How to share the same session between JNLP Application and browser session using BasicService.showDocument(url) method? It appears whenever i launch any URL from within a JNLP application using BasicService.showDocument(url) , it creates new instance of browser and session even after i used cooke handlers to restore JSESSIONID and Ltpa token etc.
    public static int showDocument(String urlToOpen)
    BasicService obj = (BasicService) ServiceManager.lookup("javax.jnlp.BasicService");
    obj.showDocument(urlToOpen);
    }

    Try using Reflection

  • Can two ipads share the same icloud account

    hello. My wife and i recently added a second ipad to our household (his and hers). We would like to share the same icloud account fior photo streaming and music and such, but want to have our seperate emails, contacts and calendars on each ipad. Is this possible without having to create seperate icloud accounts?

    Don't understand what you are trying to do.
    You said 2 iPads using the same iCloud acoount, are you try to switch to another iCloud account?
    Then you need to delete the one you have on iPad. Deleting iPad iCloud account do not delete the content in iCloud. You will be asked.

  • Contacts merged with device that shares the same iTunes account as me

    My husband and I share the same itunes account on our phones so whenever he downloads something new it downloads on my phone as well and vice-versa.
    Just recently, after iOS 6 update, we noticed that his contacts merged with my iPhone &amp; my contacts have merged with his. Also, he is getting notifications from an app that we share... But the notifications are from MY friends on facebook. The app is called voxer and whenever someone (from my Facebook friends) sign up, my husband gets the notification. Which is really weird because im not even signed on to Facebook on his phone. Somehow my information has merged with his phone and his mine. Why is this? Is there a way I can make things back to normal? Thanks in advanced.

    Sounds like you may be syncing your contacts to the same iCloud account on both phones if your contacts are merged.  You can share the same iTunes account for purchasing apps and music, but to avoid merging data you sync with iCloud you need to set up separate iCloud accounts on each phone.
    To do this, first decide which phone will be changing accounts.  On this phone that will be changing, go to Settings>iCloud and turn all data that is syncing with iCloud (contacts, calendars, etc.) to Off.  When prompted choose to keep the data on the iPhone.  After everything is turned off, scroll to the bottom and tap Delete Account.  (This will only delete the account from this phone, not from iCloud.  The other phone will not be effected by this.)  Next, set up a new iCloud account on this phone using a different Apple ID and turn iCloud data syncing for contacts, etc. back to On.  When prompted, choose Merge.  This will upload the data to this new account.  At this point you will have two different iCloud accounts, one for each phone, with identical data on them.
    You will then have to go to icloud.com on your computer and sign into each iCloud account separately and manually delete the other person's data from your account (such as deleting your husband's contacts from your account, and vice versa).  These changes will be reflected on each phone; when you're finished you will have individual iCloud accounts with just your own data on them.
    Don't know anything about your problem with voxer notifications.

  • How do you share photos on the same computer between users???

    Hello,
    I would like to know how to share photos on the same computer between users...
    My wife and I have different user accounts on our iMac and I can't figure out how to share photos.
    I tried everything as told by Apple but it seems you can only share photos on your iMac with a network. If that is what I understand...
    I just want to have all the photos on my user account and when my wife is on her user account I want her to be able to view the photos.
    Again, we are only using one iMac.
    Thank you for your help in advance...

    For iPhoto 09 (version 8.0.2) and later:
    What you mean by 'share'.
    If you want the other user to be able to see the pics, but not add to, change or alter your library, then enable Sharing in your iPhoto (Preferences -> Sharing), leave iPhoto running and use Fast User Switching to open the other account. In that account, enable 'Look For Shared Libraries'. Your Library will appear in the other source pane.
    Any user can drag a pic from the Shared Library to their own in the iPhoto Window.
    Remember iPhoto must be running in both accounts for this to work.
    If you want the other user to have the same access to the library as you: to be able to add, edit, organise, keyword etc.
    Quit iPhoto in both accounts. Move the Library to the Users / Shared Folder
    (You can also use an external HD set to ignore permissions, a Disk Image or even partition your Hard Disk.)
    In each account in turn: Double click on the Library to open it. (You may be asked to repair the Library Permissions.) From that point on, this will be the default library location. Both accounts will have full access to the library, in fact, both accounts will 'own' it.
    However, there is a catch with this system and it is a significant one. iPhoto is not a multi-user app., it does not have the code to negotiate two users simultaneously writing to the database, and trying will cause db corruption. So only one user at a time, and back up, back up back up.
    Regards
    TD

  • Can we share the same Business Action between clients?

    Hi All,
    I have a scenario wherein we have two Remote Trading Partners (say Remote1 and Remote2). Both these clients send EDI-850 documents and expect EDI-997.
    So,
    1. I first go to the Protocols Tab.
    2. Create a Business Action (Process_850) and Follow the Wizard for the details.
    In the process i create
    a. Process Protocol
    b. Document Protocol
    c. Exchange Protocol
    Now for the second client also sends us the EDI-850 document. So can I reuse all the above created objects and just have two Agreements one with Remote1 using the same Business Action (Process_850) but different Delivery Channels
    Or should i be creating different Business Actions for the different Clients?
    If i indeed have to create diff business actions then can i reuse the Document Protocol between clients? It does allow you to select and existing one.
    Finally, If i understand this correct, We create a Business Action and hence the Document Protocol. We set the default values of the Document Protocol Parameters in here and when we create Trading Partners we override these (Application Sender Code, etc.). In a way we are saying that Process_850 is a business action that is processing 850 documents. 850 being a template and the document parameters are the replacements done on 850 when it is sent by Sender we have say Acme and when it is receiver we have GlobalChips.

    If it is for two diffrent trading partner you can share the same business action.

  • Is there a way to stop sharing apps between my iphone and my daughters ipad. We share the same email and itunes account.

    is there a way to stop sharing apps between my iphone and my daughters ipad...we share the same email and itunes account.

    You can keep the same Apple ID for purchases, but set up her own iCloud account for email and syncing.
    You can easily and effectively use the same ID for purchases.
    What you do is use the apple ID you have been using for app/book/movie/music purchases. In Settings > Store, make sure you are using your usual apple ID.
    Then, set up an icloud (@me.com) account for you on your phone, and one for your daughter on her phone. In Settings > iCloud, make sure each of you are using your new iCloud accounts, and not your Apple ID.
    Also, in Settings > Messages > Receive At, make sure you are using your iCloud accounts
    This way, all of your purchases are using one account, and you can effectively use iCloud.

  • Two cRIOs with the same code in 1 project

    I have a project that contains two cRIOs that run the same exact
    program.  The only difference is the IP address in the ni-rt.ini file. 
    I would like to create a project that represents the actual targets but
    uses the same vis.  In doing this I found the FPGA targets must have a
    different name and therefore a different bit file.  Also, the DMA FIFO
    is a different instance in each target.  I'm really trying to get away
    from supporting two copies of the same exact code.
    So, it seems this type of project doesn't fit well into the project
    explorer and decided to create only 1 target with 2 source
    distribution's. I can create 2 source distributions but the target
    destination directory must be different. Had anyone run into this
    situation and come up with a better way to handle this?  I guess I'm
    back to manually deploying the files and making sure the correct
    ni-rt.ini file get placed in the corresponding cRIO.

    Dwisti,
    I suggest you look into using the Open FPGA Reference node in Bitfile
    mode (right-click the Open FPGA Reference node). From my experience,
    that's the best way to share the same FPGA code between multiple
    targets; you just need to be aware that if you recompile, you may need
    to point the Open FPGA Ref node to the new bitfile. With this approach
    you don't really need to have the two FPGA Targets in the project; just
    keep one as the master and that should be it (in case the FPGA Targets
    are of the same type)
    Hope this helps.
    JMota

  • Sharing iphoto library on the same computer between different users

    Can anyone tell me how (without having to write my own code) I can share photos easily (so I can teach my parents, who know nothing about mac, how to do it) between different users on the SAME COMPUTER?
    I am so frustrated! You can share with anyone else in the world, but when it is on the same computer I can't figure out how to share!

    Hi Mereditty,
    Here are all the links I have for sharing a couple of different ways.
    SHARING IPHOTO LIBRARIES
    Sharing libraries between users on the same local network
    You can also share a library on the same machine between users the same way.
    Sharing libraries between users on the same machine...
    -both users must have sharing enabled in their iPhoto Preferences
    -both users must be logged in and have iPhoto running
    -Remember that you can only view the other library. You can't edit the photos or play the saved slideshows or view the made books or burn them to CD or DVD. The books and slideshows will show up as an album. You can play that album as an "on the fly" slideshow. You can drag images from the sharing library to your library in the source column to import them to your library. You can then edit, add to albums, books, slideshow, etc.
    Sharing links from Apple....
    Sharing your photos between computers
    About shared photos
    Turning off photo sharing
    Looking for shared photos
    SHARING ONE IPHOTO LIBRARY BETWEEN USERS ON SAME MACHINE
    You can try one of these three methods:
    1- Use iPhoto Library Manager-the paid version
    The documentation page will give instructions on how it is done.
    2- Sharing one iPhoto library between several users on one machine
    3- Share an iPhoto Library in tiger Using ACL's
    4- I have also read about ShareAlike
    There is no other info on the site about how it works.
    I strongly urge anyone wanting to try any of the methods for sharing one iPhoto Library folder among more than one user to backup all iPhoto Library folders before attemptin anything.
    Lori

  • HT4865 So who believes Apple should be a bit more cautionary about the sharing of iMessages and other content when several members of a family share the same iCloud account and unknowing can audit each other's iMessages, Contacts, and god only knows what

    I think Apple should be a bit more cautionary about iCloud and privacy.  My family shares the same iCloud account as we all enjoy the music we collectively purchase on iTunes; we paid Apple for this feature with some kind of grouped account.  We didn't know, however that this joins our devices so that iMessages, contacts, pictures and just about everything else is shared too.  The unprivate default seems to be to share everything between all devices vs. to allow access by exception or by choice (or by password?).  Sure, when my kids get all my texts I can go figure out why and fix it but that is in my mind the antithesis of privacy and could be quite embarrassing for any family.  I guess it is good for stalking the kids or parents though if they don't know about the partyline approach to privacy.  Maybe a tech solution would be to have the iPhone show somehow the extent of its audience to its user.

    No argument from me about the vagaries of using and sharing Apple IDs.  This can lead to unintended consequences, especially in a family situation.
    If you're sharing the same ID for FaceTime, you might want to go to Settings>FaceTime, tap the ID, sign out, then sign in with separate IDs there too.  Otherwise, you'll end up getting each other's FaceTime calls.
    Also, if you need to migrate everyone's devices to separate iCloud accounts to keep your synced data separated, you can do this by saving any photo stream photos you wish to keep to your camera roll (unless already there) by opening your my photo stream album, tapping Select, tapping the photos, tap the share icon (box with upward facing arrow), then tapping Save to Camera Roll.  If you are syncing notes with iCloud, you'll need to open each of your notes and email them to yourself so you can later copy and paste the text into new notes created in your new account.  Then go to Settings>iCloud, tap Delete Account (which only deletes it from this device, not from iCloud), choose Keep on My iDevice and provide the password to turn off Find My iPhone.  Then sign back in with a different Apple ID to create your new account and choose Merge to upload your data.  Once everyone's devices are on separate accounts, you can go to icloud.com and delete each other's data from your accounts.

  • Can two people share the same library?

    I installed a new mac mini which we're planning on using as a media hub and as a machine to sync my iphone from.  I want to have a "Server" login on it just for itunes to run the backend for Apple TV.  However, I'd also like my itunes library on my "Personal" login.  Can I share the same library betwixt the two (if put into the Shared folder?) or do I have to set it up in my Personal and then share it to the Server login?  If I do that, don't I have to login to both and run itunes before it'll work?  I'd like to avoid anything like that because I travel often and want something my family can leave on and reboot easily while I'm away, without giving my kids access to my Personal login.

    Hi chris brian,
    If you are looking to share the same iTunes library between multiple user accounts on the same Mac, you may find the following article helpful:
    iTunes: How to share music between different user accounts on a single computer
    Regards,
    - Brenden

  • Can two iTunes accounts share the same appleTV simultaneously ?

    can two iTunes accounts share the same appleTV simultaneously ?

    This should help:
    iTunes: How to share music between different accounts on a single computer
    Note that when it says "publicly accessible location", it needs to be a place where everyone has read and write access. The most common such place is the Shared folder in the Users folder, but you can place the music elsewhere if you change the access permissions manually (don't start tinkering with permissions unless you're confident you know what you're doing and can reverse things if they get messed up). 
    Regards.

  • Having multiple CAs share the same private key

    We are developing a system which implements an HA cluster across two separate geographical locations.
    Each site will have several Windows Server 2012 machines and at least one DC, and we basically have to do a master-master replication between the two sites.
    The entire system will be under a single domain.
    We will be deploying AD CS since some of our sub-systems need certificates,
    but we want to limit the variety certificate to just one (i.e. we want all CAs to issue identical certificates).
    To do that, we have to setup AD CS so that all the DCs (both intra-site and inter-site) share the same private key.
    Is it possible to have all DCs in a domain to share a single private key?
    This article on TechNet suggests that we can do it within a cluster,
    https://technet.microsoft.com/en-us/library/cc742450%28v=ws.10%29.aspx
    but we are not sure if we can do it across different sites.
    Any advice and comments are highly appreciated.
    Wanko

    Hi Wanko,
    Its not much clear what you mean by "DCs to have single private key".
    However as per the article it indicates that you can use the same (SAN) certificate on both servers (nodes) of the cluster, the certificate SN will be the common clustername.
    This is common when you are using clustering or load-balanced system which requires you to have a common name, but individual nodes.
    Basically if you want to use single private key for the HA nodes, use the same certificate across all the nodes, that would be generated on the first node(generally). You don't need to issue identical certificates(this will not work as per my understanding)
    CA First Node: Export the Cert
    On the Welcome page of the CA Backup Wizard, click Next. Select
    Private key and CA certificate, and provide a directory name where you want to temporarily store the CA certificate and optionally the key. Click
    Next.
    Provide a password to protect the CA key, click Next, and then click
    Finish.
    CA Second Node: Import the Cert
    Open the Certificates snap-in for the computer account.
    In the console tree, double-click Certificates (Local Computer), and click
    Personal.
    On the Action menu, click All Tasks, and then click
    Import to open the Certificate Import Wizard. Click Next.
    Enter the file name of the CA certificate that was previously created on the first node, and click
    Next. If you click Browse to find the certificate, change the file type to
    Personal Information Exchange (*.pfx,*.p12).
    Type the password that you have previously used to protect the private key. The password is required even if there is no private key in the .pfx file. Do not mark this key as exportable. Click
    Next.
    Place the certificate in the Personal certificate store, and click
    Next. To complete the certificate import process, click
    Finish, and then click OK.
    Secondly I don't get what do you mean by: "we basically have to do a master-master replication between the two sites."
    Please note a Cluster can only run a single instance of Certificate Services. A failover cluster of any size can be used to provide a high availability environment for certificate services. However, Microsoft does not support more than one instance
    of certificate services on a cluster.
    References:
    Overview of CA Clustering-2003
    Active Directory Certificate Services (AD CS) Clustering - Requirements-2012
    Regards,
    Satyajit
    Please“Vote As Helpful”
    if you find my contribution useful or “MarkAs Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.

  • Should multiple developers share the same MDS repository?

    We are starting a new customizable application using ADF, WebCenter and SOA Suite 11g wondering what is the best practice for MDS repositories. By default JDeveloper seems to use a file-based repository, however we need to take advantage of a database repository. The question is: Can & should multiple developers share the same MDS repository, or do we need to use the RCU tool to create a new repository for each developer? If developers share the same repository I"m wondering what happens to seed customizations if two developers both edit the same thing (like a SOA composite).
    Thanks for your input

    Thanks again ... I'm not so worried about packing the customizations in the EAR or MAR, I just hate developers having to manually change adf-config.xml from source control all the time.
    I've tried using {oracle.home} and it does not work. This seems to be a bug in JDeveloper, because I print the env variable and verify that it is correct but the deployment still fails.
    In adf-config.xml I have:
    <property value="*${oracle.home}\integration*"
    name="metadata-path"/>
    When WLS starts up, I print the env variable and see it is set to: D:\home\fmw\JDEVEL~1
    But then when I try to deploy my application I get this:
    INFO: MDSException encountered in parseADFConfigurationMDS-00503: The metadata path "D:\home\fmw\jdeveloper\system11.1.1.1.33.54.07\o.j2ee\drs\PriceManagementPOC\adf\META-INF\D:\home\fmw\JDEVEL~1\integration" does not contain any valid directories.
    For some reason it seems to be appending the directory of adf-config to the oracle home directory. It's as if the code assumes its a relative path. Looks like a bug to me.
    Billy
    Edited by: Billy Turchin on Aug 28, 2009 9:33 AM

  • All users share the same iCal

    I want all the users on my iMac to share the same Calendar. How do I do it?

    Here's how to share iCal calendars among multiple users on a single Mac.
    What you need to do is to move the iCal folder to a shared folder that is accessible to all users and then replace the iCal folder in each user's library folder with a "symbolic link' to the shared iCal folder. The symbolic link looks like an "alias", but if you try this using "aliases" to the shared iCal folder it will not work (trust me). You can create symbolic links using the Terminal, but I have no idea how, and the idea of using the Terminal scares me. The easiest way that I know of to create a symbolic link is with a shareware application called Cocktail. It allows you to do a number of different things including System Maintenance, Network Optimization, etc. It took me a while to remember how to do this; I originally found the information in the Discussions when iCal was first released, but apparently none of those original postings are listed in the forums anymore.
    Here's how to do it (you must have Administrator privileges):
    First locate your iCal folder (your user name > Library > Application Support > iCal) and make a copy of it (Finder > File > Duplicate). Drag the copy to your Desktop until you're sure iCal is being shared between users.
    Next move the original iCal folder into a Shared folder in the Users folder (Hard Disk > Users > Shared > iCal). To make sure all users can access the information, select the Shared folder, choose File > Get Info, click the little Lock icon, then under "Ownership & Permissions -> Details" make sure that the "Owner" is "System", "Group" is "Wheel" and "Access" for Owner, Group and Others is "Read & Write". Do the same with the iCal folder within the Shared folder, but this time, under "Ownership & Permissions -> Details" make sure that the "Owner" is "your user name", "Group" is "Staff" and "Access" for Owner, Group and Others is "Read & Write" and click the "Apply to Enclosed Items" button. I basing this information on what the Ownership & Permissions are for each of the folders in my system.
    Open Cocktail (I'm using version 3.6.5). Once the app is open (you must enter an administrator's user name and password) click on the "Files" button, then click on the "Links" button. Where it says "Create Symbolic Link" click on the "Choose" button. Navigate to the iCal folder (Hard Disk > Users > Shared > iCal) and click "Choose" again. In the "Save As" box that appears enter "iCal", then navigate to the Application Support folder in your Home folder (your user name > Library > Application Support) and click "Save". Quit Cocktail and open iCal. You should see all of your calendars as you did before.
    Log in under each additional User, move that User's iCal folder (user name > Library > Application Support > iCal) onto the Desktop, then create another Symbolic Link to replace it, using Cocktail as described above. In each case you should see the same calendar information as when you were logged in under your user name. As a test, add a New Event or To Do in iCal under one user, log out and back in under another user and you should see the changes when you open iCal.
    Once everything appears to be working, you can delete the iCal folders from each User's Desktop.
    As an additional maintenance measure, use Disk Utility to repair Disk Permissions.
    Unfortunately, if each User has his/her own calendars in iCal they will not be in the shared calendars. Only the calendars in the first iCal folder used to set up the shared folder will be available and I don't know of any way to merge or combine the other user's calendars into one.
    Hope this helps and good luck!

Maybe you are looking for