How do I properly bind a .Text property to the Overridden .asString() method of a SimpleObjectProperty?
Tag : java , By : Anonymous
Date : March 29 2020, 07:55 AM
will be helpful for those in need The problem is that you create a new Binding every time the asString() method is called. Since you first call it when the file is null, you get the binding created by Bindings.format("[NONE]", this). So your binding on the button is equivalent to: playButton.textProperty().bind(Bindings.format("[NONE]", bgmFile));
ObjectProperty<File> fileProperty = new SimpleObjectProperty<File>() {
/* your previous implementation */
};
fileProperty.set(new File("/path/to/some/valid/file"));
// now bind when you get the filename:
playButton.textProperty().bind(fileProperty.asString());
// setting the fileProperty to null will now invoke the binding that was provided when it wasn't null
// and you'll see a nice bunch of null pointer exceptions:
fileProperty.set(null);
new SimpleObjectProperty<File>(this, "BGM File", null){
final StringBinding string = Bindings.createStringBinding(() -> {
File file = this.get();
if (file != null && file.exists()) {
return file.getName();
} else {
return "[NONE]";
}
}, this);
@Override public StringBinding asString(){
return string ;
}
}
ObjectProperty<File> bgmFile = new SimpleObjectProperty(this, "bgmFile", null);
StringBinding fileName = Bindings.createStringBinding( () -> {
File file = bgmFile.get();
if (file != null && file.exists()) {
return file.getName();
} else return "[NONE]";
}, bgmFile);
|
Does OkHttp have something easier similar to Unirest's field method for creating a RequestBody?
Date : March 29 2020, 07:55 AM
it helps some times Instead of Unirest, I'm using okhttp because there are responses where I only need the header so I don't need to download it using its ResponseBody.string() method. , I found okhttp has FormBody which has a Builder: Call call = httpClient.newCall(
new Request.Builder()
.url(url)
.header("Authorization", tempToken)
.post(new FormBody.Builder()
// TODO user getId()
.add("id","")
.add("custom_fields", field)
.build())
.build()
);
|
Bind simpleStringProperty to a simpleIntegerProperty using custom asString method
Tag : javafx , By : Daniel Reslie
Date : March 29 2020, 07:55 AM
will help you The NumberExpression.asString(String) formats the number according to the rules of Formatter, same as if using String.format or Print[Stream|Writer].printf. Unfortunately, unless I'm missing something, the Formatter class expects date/time objects to represent a moment in time, not a duration of time. To format your property as a duration with a HH:MM:SS format you'll need to create your own binding. To get the String you want you can still use String.format, but by formatting as integral numbers rather than time. This requires you to calculate the hours, minutes, and seconds. String str = String.format("%02d:%02d:%02d", hours, minutes, seconds);
import java.time.Duration;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
private final IntegerProperty seconds = new SimpleIntegerProperty(this, "seconds");
@Override
public void start(Stage primaryStage) {
Label label = new Label();
label.textProperty().bind(Bindings.createStringBinding(() -> {
// java.time.Duration
Duration duration = Duration.ofSeconds(seconds.get());
return String.format("%02d:%02d:%02d", duration.toHoursPart(),
duration.toMinutesPart(), duration.toSecondsPart());
}, seconds));
primaryStage.setScene(new Scene(new StackPane(label), 500, 300));
primaryStage.show();
Timeline timeline = new Timeline(
new KeyFrame(javafx.util.Duration.seconds(1.0), e -> seconds.set(seconds.get() + 1))
);
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
}
}
|
Why am I getting "java.lang.NoClassDefFoundError: kong/unirest/Unirest"?
Date : March 29 2020, 07:55 AM
wish helps you This line in exception's stack trace means that you don't have that class Unirest in your class path Exception in thread "main" java.lang.NoClassDefFoundError: kong/unirest/Unirest However it seems, you added it's lib in dependencies section to be resolved by maven, but it's in "provided scope", and that means you're expecting something else to provide it to you like application server for instance, but i think it's not your environment. <dependency>
<groupId>com.konghq</groupId>
<artifactId>unirest-java</artifactId>
<version>3.1.00</version>
<scope>provided</scope>
</dependency>
|
How to set multiple queryString parameters in com.mashape.unirest.http.Unirest
Tag : java , By : mtnmuncher
Date : March 29 2020, 07:55 AM
|