Using Enabled Property on a JButton in netbeans

I am trying to setup a button similar to the save button with the default CRUD database template (where the button only becomes active if a variable is true). I have looked at the code for the save button and worked out that i need:

  1. A variable to link it with (saveNeeded in their case)
  2. An action to run

I have recreated both of these on another button but it never seams to get enabled. I have print statements on 2 other buttons i am using to set the variable i have my button linked to to true and false so i can see the value is changing. Is there some crucial step i am missing? this seems like it should be fairly straight forward.

One other thing, if i manualy change the variable to true in my constructor to true and run the application it enables the button and false disables it so that part is working, just not the change.

Any help would be appreciated as i have spent the last few hours trying and can not figure it out Thanks

Answers


The variable or "property" needs to be watched somehow, perhaps by using a PropertyChangeSupport object and allowing other objects to add a PropertyChangeListener to it, making it a "bound property". There's a special version of this for Swing applications that takes care with the Swing event thread, SwingPropertyChangeSupport, and you may wish to use it.

Edit You asked

Thanks for the reply, i assume that would be what firePropertyChange("saveNeeded", !saveNeeded, saveNeeded); is doing but waht is this doing? does this just notify the program or do i need to catch an handle this somewhere. This is based off the pre generated code so im not sure if it added something in the background.

The class that holds the watched variable would need a private SwingPropertyChangeSupport field. You would give it a public addPropertyChangeListener method where you'd allow other classes to listen to its bound properties, something like this (if the property were a String):

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.event.SwingPropertyChangeSupport;

public class Foo {
   public static final String MY_BOUND_PROPERTY = "My Bound Property";
   private SwingPropertyChangeSupport spcSupport = new SwingPropertyChangeSupport(
         this);
   private String myBoundProperty;

   public void addPropertyChangeListener(PropertyChangeListener listener) {
      spcSupport.addPropertyChangeListener(listener);
   }

   public void removePropertyChangeListener(PropertyChangeListener listener) {
      spcSupport.removePropertyChangeListener(listener);
   }

   public String getMyBoundProperty() {
      return myBoundProperty;
   }

   public void setMyBoundProperty(String myBoundProperty) {
      Object oldValue = this.myBoundProperty;
      Object newValue = myBoundProperty;

      this.myBoundProperty = myBoundProperty;
      PropertyChangeEvent pcEvent = new PropertyChangeEvent(this,
            MY_BOUND_PROPERTY, oldValue, newValue);
      spcSupport.firePropertyChange(pcEvent);
   }

}

Then any class that would like to listen for changes would simply add a PropertyChangeListener to an object of this class and respond to changes as it saw fit.


Need Your Help

CRM 2013: Using the value of an Option Set input parameter in a custom workflow activity

c# dynamics-crm crm dynamics-crm-2013

I was trying to use an optionset input parameter in custom workflow activity, so I have used the below code:

Learning Glade along with PyGTK

python gtk pygtk glade

I've got my self sort of familiar with pygtk. I find it to be a lot of work to build GUI from code. So I turned to Glade. I'm trying to follow tutorials for Glade and PyGTK, and how they work toget...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.