Reputation: 21
I am trying to create a custom Label type that will include a function for 'fading out'. This is for displaying messages that will be flashed and then are hidden.
I am using Eclipse, SceneBuilder and Javafx. I'm not sure how to do it or if it is possible, but this is what I have so far:
import javafx.scene.control.Label;
import java.text.DecimalFormat;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JLabel;
public class TimedMessage extends Label {
private int countBy;
private final String SUCCESS_COL = "#0bbf41";
private final String FAIL_COL = "red";
private int counter;
private Timer messageTimer;
public TimedMessage(int countBy) {
this.countBy = countBy;
}
public void showMessage(boolean success, String message) {
//function to show message
/*
* To use: showMessage(false, "error"); or showMessage(true, "nice");
* Need:
* import javafx.scene.control.Label;
* import java.text.DecimalFormat;
* import java.util.Timer;
* import java.util.TimerTask;
*/
this.setVisible(true); //show message label
this.setOpacity(1); //set initial opacity to 1
this.setText(message); //set the message's text
if (success) {
this.setStyle("-fx-text-fill: "+SUCCESS_COL);//set green
}else {
this.setStyle("-fx-text-fill: "+FAIL_COL);//set red
}
// Create new Timer
messageTimer = new Timer();
counter = 0; //should start from 0; do not change this value
//messageTimer.scheduleAtFixedRate(messageTask, 5, 5);
messageTimer.scheduleAtFixedRate(
new TimerTask() {
//timer task
private DecimalFormat deciFormat = new DecimalFormat("0.00");
public void run() {
if (counter >100){
//Stop timer
messageTimer.cancel();
messageTimer.purge();
this.setVisible(false); //hide message
this.setOpacity(1); //set initial opacity to 1
return;
}else {
double opacity = Double.valueOf(deciFormat.format((1.0 - counter/100.0))); //set opacity value
this.setOpacity(opacity); //set the opacity to the correct value
counter+=3;
}
}
}, countBy*100, countBy); //delay value, speed value
}
}
Which obviously doesn't work.
And this is what I first got working in a messy code in one file (so, version 1 that I am trying to pull the code from, into a new 'Object' that I can use in multiple classes):
import javafx.scene.control.Label;
import java.text.DecimalFormat;
import java.util.Timer;
import java.util.TimerTask;
import java.io.IOException;
import java.sql.SQLException;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.stage.Window;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
public class PrimaryController {
...
@FXML
private Label messageLabel;
private int counter;
private Timer messageTimer;
private void showMessage(boolean success, String message) {
//function to show message
/*
* To use: showMessage(false, "error"); or showMessage(true, "nice");
* Need:
* import javafx.scene.control.Label;
* import java.text.DecimalFormat;
* import java.util.Timer;
* import java.util.TimerTask;
*/
messageLabel.setVisible(true); //show message label
messageLabel.setOpacity(1); //set initial opacity to 1
messageLabel.setText(message); //set the message's text
if (success) {
messageLabel.setStyle("-fx-text-fill: #0bbf41");//set green
}else {
messageLabel.setStyle("-fx-text-fill: red");//set red
}
// Create new Timer
messageTimer = new Timer();
counter = 0; //should start from 0; do not change this value
//messageTimer.scheduleAtFixedRate(messageTask, 5, 5);
messageTimer.scheduleAtFixedRate(
new TimerTask() {
//timer task
private DecimalFormat deciFormat = new DecimalFormat("0.00");
public void run() {
if (counter >100){
//Stop timer
messageTimer.cancel();
messageTimer.purge();
messageLabel.setVisible(false); //hide message
messageLabel.setOpacity(1); //set initial opacity to 1
return;
}else {
double opacity = Double.valueOf(deciFormat.format((1.0 - counter/100.0))); //set opacity value
messageLabel.setOpacity(opacity); //set the opacity to the correct value
counter+=3;
}
}
}, 300, 4); //delay value, speed value
}
...
@FXML
void logInOnClick() throws IOException, SQLException {
...
if (userName.getText().isEmpty()) {
showMessage(false, "error in adsadsasdasdadsdasasd");
//showAlert(Alert.AlertType.ERROR, owner, "Form Error!","Please enter your email id");
return;
}
}
}
Any advice or help would be greatly appreciated, thanks.
Upvotes: 1
Views: 739
Reputation: 13858
Try using FadeTransition
. Example app below. The app takes three seconds to go from 1 to 0 transparency.
import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;
/**
* @author rstein
*/
public class App extends Application {
@Override
public void start(final Stage primaryStage) {
Label label = new Label("Label");
FadeTransition ft = new FadeTransition(Duration.millis(3000), label);
ft.setFromValue(1.0);
ft.setToValue(0);
ft.setCycleCount(1);
ft.play();
VBox root = new VBox(label);
Scene scene = new Scene(root, 300, 200);
primaryStage.setTitle(this.getClass().getSimpleName());
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(final String[] args) {
Application.launch(args);
}
}
Upvotes: 4