Disable Button when integer value - java

I tried disable button when some integer value != 1
For example (my idOrder is IntegerProperty)
refreshButton.disableProperty().bind(new BooleanBinding() {
#Override
protected boolean computeValue() {
return currentOrder.getIdOrder() != 1;
}
});
And it's works. But when I changed value on 1 (currentOrder.setIdOrder(1)) button is still disabled.
What I doing wrong?

You've created a BooleanBinding but haven't configured it to observe anything, thus it will never be notified of your property changing. You need to invoke BooleanBinding#bind(Observable...) during instantiation. For example:
refreshButton.disableProperty().bind(new BooleanBinding() {
{
bind(currentOrder.idOrderProperty());
}
#Override protected boolean computeValue() {
return currentOrder.getIdOrder() != 1;
}
#Override public void dispose() {
// for a proper implementation, we need this as well
unbind(currentOrder.idOrderProperty());
}
});
That said, the above can be simplified with Bindings#createBooleanBinding(Callable,Observable...):
refreshButton.disableProperty()
.bind(Bindings.createBooleanBinding(
() -> currentOrder.getIdOrder() != 1, currentOrder.idOrderProperty()));
But even that can be simplified further with one of the following:
Bindings#notEqual(int,ObservableNumberValue):
refreshButton.disableProperty().bind(Bindings.notEqual(1, currentOrder.idOrderProperty());
NumberExpresion#isNotEqualTo(int):
refreshButton.disableProperty().bind(currentOrder.idOrderProperty().isNotEqualTo(1));

Related

How to remove or deactivate a listener

So, the way I have things set up, is that at specific conditions, I want a listener to be active, and after it has fired, stop listening for changes. Maybe I'm missing something, but I can't seem to figure out how to use the removeListener() function, or if that's even the way to go about doing this.
Some code:
break1.setOnAction(e ->{
final String fieldValue = manage_money.getText();
int pp = Integer.parseInt(platinum_num.getText());
int gp = Integer.parseInt(gold_num.getText());
int sp = Integer.parseInt(silver_num.getText());
int cp = Integer.parseInt(copper_num.getText());
if (fieldValue != null && fieldValue.matches("\\d+")) {
int value = Integer.parseInt(manage_money.getText());
if (silver.isSelected()){
if (value <= sp){
try {
tooltip_inv.getChildren().addAll(select);
radio_money2.selectedToggleProperty().addListener(((observable, oldValue, newValue) -> {
if (newValue == copper){
silver_num.setText(Integer.toString(sp - value));
copper_num.setText(Integer.toString(cp + value * 10));
manage_money.clear();
tooltip_inv.getChildren().clear();
}
}));
} catch (IllegalArgumentException ex) {
}
}else{
manage_money.setText("ERR");
}
}
}else{
manage_money.setText("NaN");
}
});
What you've done in your example is you've defined a listener in your addListener method. You don't have any reference to it except there! One way to fix this is to simply create a variable for it and keep it somewhere, something like
ChangeListener listener = new ChangeListener(){
#Override
public void changed(ObservableValue observable, Object oldValue, Object newValue) {
// your code here
}
};
radio_money2.selectedToggleProperty().addListener(listener);
radio_money2.selectedToggleProperty().removeListener(listener);

GWT Java - CellTable - ButtonCell - how to make it respond to a click

I have the following ButtonCell. How do I make it respond to a click please (e.g., addClickHandler)? I have tried a number of ways I have found yet none work. None of the Window.alert return a response.
ButtonCell selectButton = new ButtonCell();
Column <HikingMeals,String> update = new Column <HikingMeals,String>(selectButton){
#Override
public String getValue(HikingMeals selectButton)
{
return "Select";
}
public void execute(HikingMeals selectButton) {
// EDIT CODE
Window.alert("Pressed");
}
//#Override
public void update(int index, HikingMeals object, String value) {
// The user clicked on the button for the passed auction.
Window.alert("Pressed2");
}
};
table.addColumn(update, "Select");
You just need to set a FieldUpdater on the update column:
update.setFieldUpdater(new FieldUpdater<HikingMeals, String>() {
#Override
public void update(int index, HikingMeals object, String value) {
Window.alert("Pressed");
}
});

Low level bind of disableProperty and setDisable of a controller

Is there a way to do a low level bind but also be able to still do setDisable(ture/false) to a controller?
For example:
HBoxSomeBox.disableProperty().bind(new BooleanBinding() {
{
bind(someIntValue);
}
#Override
protected boolean computeValue() {
return someIntValue >=2 ;
}
});
And somewhere else in the code to do HBoxSomeBox.setDisable(false).
Currently when I try to do that it throws an exception:
java.lang.RuntimeException: HBox.disable : A bound value cannot be set.
So is there another way to have a bound controller but also to be able to set it?
From the comments, you appear to want to disable your control anytime the value of someIntValue is at least two, or under other circumstances "dictated by the view". You could either create a BooleanProperty representing those other circumstances, and use it in the binding:
IntegerProperty someIntProperty = ... ;
BooleanProperty forceDisable = new SimpleBooleanProperty();
hboxSomeHBox.disableProperty().bind(new BooleanBinding() {
{
bind(someIntValue, forceDisable);
}
#Override
public boolean computeValue() {
return someIntValue.get() >= 2 || forceDisable.get() ;
}
}
or, more succinctly,
BooleanProperty forceDisable = new SimpleBooleanProperty();
hboxSomeHBox.disableProperty().bind(someIntValue.greaterThanOrEqualTo(2).or(forceDisable));
Then calling forceDisable.set(true); will disable the control.
You can also achieve this simply with a listener:
someIntValue.addListener((obs, oldValue, newValue) -> {
if (newValue.intValue() >= 2) {
hboxSomeHBox.setDisable(true);
}
});
Since the disable property is not bound, you are free to set it in the usual way.

JavaFX custom dialog has references after removing froom scene

I have a custom dialog that is added to the scene and then removed again. Doing profiling with VisualVM, I noticed that even after a GC run the instance of this dialog is still retained.
I know that this means that there must be a reference to that object somewhere so I had a look at the references:
As seen in the image there are a lot of references from this$ which means inner classes, in this case they are bindings or ChangeListeners. The change listener can be replaced with WeakChangeListener. I'm not quite sure how I should handle the Bindings however.
Furthermore there are some references that do not make much sense at first glance:
bean of type SimpleStringProperty or SimpleObjectProperty
oldParent and value of type Node$1
So here are the concrete questions:
How to get around these strong references, so the object can actually be garbage collected? Would the use of lambda expressions instead of anonymous inner classes have any effect in this respect? How to figure out where the object is references by bean, oldParent and value.
EDIT1:
The bean references of type SimpleStringProperty are used in the super class and therefore should not cause an issue here, I guess. One SimpleObjectProperty bean reference comes from a utility method that provides an EventHandler. How would I resolve that, is there something similar for EventHandler as for ChangeListeners?
EDIT2:
I tried to come up with a simple application to reproduce the same thing. I could manage it and saw that I have basically the same fields listed in the heap dump, but then noticed that I have retained a reference to the component that is removed from the scene in my application. Once I let go of that reference it was cleaned up. The only noticeable difference is in my small example there is no reference in an Object array.
EDIT3:
I did some digging and found two places in the code that when commented out or not used, will not cause the object become eligible for garbage collection. The first one is this ChangeListener:
sailorState.numberOfSailorsProperty().addListener(new ChangeListener<Number>() {
#Override
public void changed(ObservableValue<? extends Number> observableValue,
Number oldValue, Number newValue) {
int inTavern = newValue.intValue()-sailorsAdditionalOnShip.get();
if (inTavern < 0) {
sailorsAdditionalOnShip.set(Math.max(sailorsAdditionalOnShip.get() + inTavern, 0));
inTavern = 0;
}
sailorsInTavern.set(inTavern);
}
});
The second one is a bit more complex. The component is a Dialog that has a close button. On pressing that one the dialog closes. This is the code of the button, I do not think that with this part is the problem, but for completeness sake:
public class OpenPatricianButton extends Control {
protected final StringProperty text;
protected final ReadOnlyObjectProperty<Font> currentFont;
protected final ObjectProperty<EventHandler<MouseEvent>> onAction;
public OpenPatricianButton(String text,
final Font font) {
super();
this.text = new SimpleStringProperty(this, "text", text);
this.currentFont = new ReadOnlyObjectPropertyBase<Font>() {
#Override
public Object getBean() {
return this;
}
#Override
public String getName() {
return "currentFont";
}
#Override
public Font get() {
return font;
}
};
this.onAction = new SimpleObjectProperty<EventHandler<MouseEvent>>(this, "onAction");
this.getStyleClass().add(this.getClass().getSimpleName());
}
#Override
public String getUserAgentStylesheet() {
URL cssURL = getClass().getResource("/ch/sahits/game/javafx/control/"+getClass().getSimpleName()+".css");
return cssURL.toExternalForm();
}
public StringProperty textProperty() {
return text;
}
public String getText() {
return text.get();
}
public void setText(String text) {
this.text.set(text);
}
public Font getFont() {
return currentFont.get();
}
public ObjectProperty<EventHandler<MouseEvent>> onActionProperty() {
return onAction;
}
public EventHandler<MouseEvent> getOnAction() {
return onAction.get();
}
public void setOnAction(EventHandler<MouseEvent> onAction) {
this.onAction.set(onAction);
}
}
public class OpenPatricianSmallWaxButton extends OpenPatricianButton {
public OpenPatricianSmallWaxButton(String text,
final Font font) {
super(text, font);
}
#Override
protected Skin<?> createDefaultSkin() {
return new OpenPatricianSmallWaxButtonSkin(this);
}
public OpenPatricianSmallWaxButton(String text) {
this(text, Font.getDefault());
}
}
public class OpenPatricianSmallWaxButtonSkin extends SkinBase<OpenPatricianSmallWaxButton> {
public OpenPatricianSmallWaxButtonSkin(final OpenPatricianSmallWaxButton button) {
super(button);
InputStream is = getClass().getResourceAsStream("sealingWaxFlattend.png");
Image img = new Image(is);
final ImageView imageView = new ImageView(img);
final Label label = new Label();
label.textProperty().bind(button.textProperty());
label.getStyleClass().add("OpenPatricianSmallWaxButtonLabeled");
label.setFont(button.getFont());
label.onMouseClickedProperty().bind(button.onActionProperty());
label.textProperty().bind(button.textProperty());
imageView.onMouseReleasedProperty().bind(button.onActionProperty());
StackPane stack = new StackPane();
stack.getChildren().addAll(imageView, label);
Group group = new Group(stack);
group.setManaged(false);
button.setPrefHeight(img.getHeight());
button.setPrefWidth(img.getWidth());
getChildren().add(group);
}
}
And here is the code fragment where the button is instantiated:
closeButton = new OpenPatricianSmallWaxButton("X", font);
closeButton.setLayoutX(WIDTH - CLOSE_BUTTON_WIDTH - CLOSE_BUTTON_PADDING);
closeButton.setLayoutY(CLOSE_BTN_Y_POS);
closeButton.setOnAction(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
executeOnCloseButtonClicked();
}
});
closeButton.getStyleClass().add("buttonLabel");
getContent().add(closeButton);
The call to remove the button is done through Guava AsyncEventBus. Therefore the code is a bit length. It starts in the Application thread and then gets posted to the event bus thread which then eventually has to call Platform.runLater:
protected void executeOnCloseButtonClicked() {
ViewChangeEvent event = new ViewChangeEvent(MainGameView.class, EViewChangeEvent.CLOSE_DIALOG);
clientEventBus.post(event);
}
public void handleViewChange(ViewChangeEvent event) {
if (event.getAddresse().equals(MainGameView.class)) {
if (event.getEventNotice() instanceof DialogTemplate) {
setNewDialog((DialogTemplate) event.getEventNotice());
} else {
sceneEventHandlerFactory.getSceneEventHandler().handleEvent(event.getEventNotice());
}
}
}
public void handleEvent(Object eventNotice) {
Preconditions.checkNotNull(dialogContoller, "Dialog controller must be initialized first");
if (eventNotice == EViewChangeEvent.CLOSE_DIALOG) {
dialogContoller.closeDialog();
}
....
public void closeDialog() {
if (Platform.isFxApplicationThread()) {
closeDialogUnwrapped();
} else {
Platform.runLater(() -> closeDialogUnwrapped());
}
}
private void closeDialogUnwrapped() {
if (dialog != null) {
new Exception("Close dialog").printStackTrace();
getChildren().remove(dialog);
dialog = null;
dialogScope.closeScope();
}
}
The really peculiar thing is that the dialog can be cleaned up by the GC (provided the first issue with the ChangeListener is commented out) when I call closeDialog from a timer. In other words this behaviour does only happen if I close the dialog with a mouse click.

How to add a hook to save event for existing DataObject in NetBeans?

I want to make some processing every time when a particular DataObject is saved. If I understand NetBeans IDE API correctly, there is an Savable interface that can be used to implement saving options for custom editors. The problem here is that I do not want to implement my own editor, nor DataObject. I have a MIME type that is edited by a default Gsf editor (the common scripting language api) and has a GsfDataObject (I expect with the DOSavable). I want to keep all that way, just to add a hook, maybe a callback method or something, that would be called every time a save is done upon a given GsfDataObject (and I want a default save action be called, I dont want to override it).
So far I came to this simple solution but it seems ugly (it is more or less inspired by http://wiki.netbeans.org/DevFaqListenForSaveEvents ):
// I have a FileObject fobj
final DataObject dobj = DataObject.find(fobj);
dobj.addPropertyChangeListener(new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals(DataObject.PROP_MODIFIED)) {
if (!((Boolean) evt.getOldValue()) & ((Boolean) evt.getNewValue())) {
System.out.println(">>>> here it gets modified");
} else {
System.out.println(">>>> here the data object gets saved");
}
}
}
});
However, this is not called only when the save is done, but also when the file gets modified, but then the modifications are reverted by Ctrl + Z. It only checks whether the data object changes its state from modified to unmodified. Is there a way to hook to a save event only?
P.S.: I tried to call new SJDOSavable(dobj).add(); in the moment when the DataObject gets modified and then to remove it in the other branch. However, the handleSave method does not get called. SJDOSavable class is a simple Savable implemented according to DOSavable from the DataSystems API:
private static final class SJDOSavable extends AbstractSavable implements Icon {
final DataObject obj;
public SJDOSavable(DataObject obj) {
this.obj = obj;
}
#Override
public String findDisplayName() {
return obj.getNodeDelegate().getDisplayName();
}
#Override
protected void handleSave() throws IOException {
System.out.println(">>>>> but this does not get called");
}
#Override
public boolean equals(Object other) {
if (other instanceof SJDOSavable) {
SJDOSavable dos = (SJDOSavable) other;
return obj.equals(dos.obj);
}
return false;
}
#Override
public int hashCode() {
return obj.hashCode();
}
final void remove() {
unregister();
}
final void add() {
register();
}
#Override
public void paintIcon(Component c, Graphics g, int x, int y) {
icon().paintIcon(c, g, x, y);
}
#Override
public int getIconWidth() {
return icon().getIconWidth();
}
#Override
public int getIconHeight() {
return icon().getIconHeight();
}
private Icon icon() {
return ImageUtilities.image2Icon(obj.getNodeDelegate().getIcon(BeanInfo.ICON_COLOR_16x16));
}
}
Did you try this ?
http://wiki.netbeans.org/DevFaqListenForSaveEvents
Also if you want to listen to global Save events, it seems you can do that now.
https://netbeans.org/bugzilla/show_bug.cgi?id=140719

Categories