Last active
April 9, 2018 13:49
-
-
Save ouaibsky/abed16f8eea25358d0819913be13c1dd to your computer and use it in GitHub Desktop.
Table Blink
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample.blinking; | |
import javafx.animation.KeyFrame; | |
import javafx.animation.Timeline; | |
import javafx.application.Application; | |
import javafx.beans.value.ChangeListener; | |
import javafx.collections.FXCollections; | |
import javafx.collections.ObservableList; | |
import javafx.css.PseudoClass; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Button; | |
import javafx.scene.control.TableColumn; | |
import javafx.scene.control.TableRow; | |
import javafx.scene.control.TableView; | |
import javafx.scene.control.cell.PropertyValueFactory; | |
import javafx.scene.layout.BorderPane; | |
import javafx.stage.Stage; | |
import javafx.util.Duration; | |
import java.util.Random; | |
public class TestApp extends Application { | |
public static void main(String[] args) { | |
Application.launch(args); | |
} | |
@Override | |
public void start(Stage stage) throws Exception { | |
BorderPane root = new BorderPane(); | |
final ObservableList<Person> objects = FXCollections.observableArrayList(); | |
objects.addAll(new Person("Cathy", "Freeman"), new Person("Albert", "Namatjira"), | |
new Person("Noel", "Pearson"), new Person("Oodgeroo", "Nooncal")); | |
Button button = new Button("test"); | |
root.setBottom(button); | |
TableView<Person> table = new TableView<Person>(); | |
table.setItems(objects); | |
TableColumn<Person, String> firstNameCol = new TableColumn<Person, String>( | |
"First Name"); | |
firstNameCol.setCellValueFactory( | |
new PropertyValueFactory<Person, String>("firstName")); | |
table.getColumns().add(firstNameCol); | |
TableColumn<Person, String> lastNameCol = new TableColumn<Person, String>( | |
"Last Name"); | |
lastNameCol.setCellValueFactory( | |
new PropertyValueFactory<Person, String>("lastName")); | |
lastNameCol.setCellFactory(e -> new FlashingTableCell2<Person, String>()); | |
//lastNameCol.setSortType(SortT); | |
table.setRowFactory(e -> new TableRow<>()); | |
final Random random = new Random(System.currentTimeMillis()); | |
button.setOnAction(e -> { | |
final int i = random.nextInt(objects.size()); | |
if (i == 0) { | |
final Person p = new Person("Foo_" + random.nextInt(1000), "Bar"); | |
p.isNewProperty().setValue(true); | |
objects.add(p); | |
} | |
else { | |
final Person person = objects.get(i); | |
person.setLastName(person.getFirstName() + "_" + random.nextInt(1000)); | |
} | |
// table. | |
// table.getItems().add(person); | |
}); | |
table.getColumns().add(lastNameCol); | |
PseudoClass flashHighlight = PseudoClass.getPseudoClass("flash-highlight"); | |
table.setRowFactory(tv -> { | |
TableRow<Person> row = new TableRow<>(); | |
Timeline flasher = new Timeline( | |
new KeyFrame(Duration.seconds(0.1), e -> { | |
row.pseudoClassStateChanged(flashHighlight, true); | |
}), | |
new KeyFrame(Duration.seconds(0.2), e -> { | |
row.pseudoClassStateChanged(flashHighlight, false); | |
}) | |
); | |
flasher.setCycleCount(4); | |
ChangeListener<Boolean> cautionListener = (obs, cautionWasSet, cautionIsNowSet) -> { | |
if (cautionIsNowSet) { | |
flasher.play(); | |
} else { | |
flasher.stop(); | |
row.pseudoClassStateChanged(flashHighlight, false); | |
} | |
}; | |
row.itemProperty().addListener((obs, oldItem, newItem) -> { | |
if (oldItem != null) { | |
oldItem.isNewProperty().removeListener(cautionListener); | |
} | |
if (newItem == null) { | |
flasher.stop(); | |
row.pseudoClassStateChanged(flashHighlight, false); | |
} else { | |
newItem.isNewProperty().addListener(cautionListener); | |
if (newItem.isNewProperty().get()) { | |
flasher.play(); | |
flasher.setOnFinished(e -> newItem.isNewProperty().set(false)); | |
} else { | |
flasher.stop(); | |
row.pseudoClassStateChanged(flashHighlight, false); | |
} | |
} | |
}); | |
return row ; | |
}); | |
root.setCenter(table); | |
Scene scene = new Scene(root, 800, 600); | |
// scene.getStylesheets().add("styles.css"); | |
scene.getStylesheets().add(getClass().getResource("flash-highlight.css").toExternalForm()); | |
stage.setScene(scene); | |
stage.show(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.table-row-cell:flash-highlight { | |
-fx-background: rgb(255, 255, 204); | |
-opacity: 0.5; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample.blinking;/* | |
* Copyright 2015 the original author or authors. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
import javafx.animation.FadeTransition; | |
import javafx.geometry.Insets; | |
import javafx.scene.control.ContentDisplay; | |
import javafx.scene.control.Label; | |
import javafx.scene.control.TableCell; | |
import javafx.scene.layout.*; | |
import javafx.scene.paint.Color; | |
import javafx.util.Duration; | |
import java.util.Objects; | |
public class FlashingTableCell2<S, T> extends TableCell<S, T> { | |
private static final Duration HIGHLIGHT_TIME = Duration.millis(300); | |
private final Pane fadePane = new Pane(); | |
private final FadeTransition animation; | |
private final Label labeledText = new Label(""); | |
private final StackPane container = new StackPane(); | |
private final Background background = new Background(new BackgroundFill(Color.RED, CornerRadii.EMPTY, Insets.EMPTY)); | |
public FlashingTableCell2() { | |
super(); | |
setContentDisplay(ContentDisplay.GRAPHIC_ONLY); | |
fadePane.setBackground(background); | |
fadePane.setOpacity(0); | |
animation = new FadeTransition(HIGHLIGHT_TIME, fadePane); | |
setPadding(Insets.EMPTY); | |
container.getChildren().addAll(fadePane, labeledText); | |
setGraphic(container); | |
} | |
@Override | |
public void updateItem(T value, boolean empty) { | |
T old = itemProperty().getValue(); | |
// System.out.println("index: "+indexProperty().getValue()+"TableRow: "+getTableRow().indexProperty().getValue()); | |
super.updateItem(value, empty); | |
if(empty) { | |
labeledText.setText(""); | |
return; | |
} | |
final String str = value.toString(); | |
//System.out.println("old: '"+labeledText.getText()+"' new: '"+str+"' oldI: '"+old+"' newI:'"+value+"' empty:"+empty); | |
labeledText.setText(str); | |
if (!Objects.isNull(old) && !old.equals(value)) { | |
animation.setFromValue(1); | |
animation.setToValue(0); | |
animation.setCycleCount(1); | |
animation.setAutoReverse(true); | |
animation.playFromStart(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment