Advertisement
Guest User

Untitled

a guest
Aug 19th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.24 KB | None | 0 0
  1. package driver;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Set;
  5. import java.util.concurrent.atomic.AtomicInteger;
  6.  
  7. import javafx.application.Application;
  8. import javafx.beans.property.SimpleBooleanProperty;
  9. import javafx.beans.value.ChangeListener;
  10. import javafx.beans.value.ObservableValue;
  11. import javafx.collections.FXCollections;
  12. import javafx.collections.ObservableList;
  13. import javafx.scene.Scene;
  14. import javafx.scene.control.ListCell;
  15. import javafx.scene.control.ListView;
  16. import javafx.scene.control.ToggleButton;
  17. import javafx.scene.layout.HBox;
  18. import javafx.scene.layout.StackPane;
  19. import javafx.stage.Stage;
  20. import lombok.Data;
  21.  
  22. public class TestListView2 extends Application{
  23.  
  24.     private static final int NUMBER_OF_COMPUTERS = 10;
  25.  
  26.     public enum Tag{
  27.         FAVORITE, ANIMAL, CLOTHING
  28.     }
  29.  
  30.     @Override
  31.     public void start(Stage primaryStage) throws Exception{
  32.  
  33.         // Initialize Computers
  34.         Computer[] computers = Arrays.stream(new Computer[NUMBER_OF_COMPUTERS])
  35.                 .map(computer -> new Computer()).toArray(Computer[]::new);
  36.         ObservableList<Computer> obsComputers = FXCollections.observableArrayList(computers);
  37.  
  38.         // Create ListView
  39.         ListView<Computer> computerView = new ListView<>();
  40.         computerView.setCellFactory((value) -> {
  41.             return new ListCell<Computer>(){
  42.  
  43.                 @Override
  44.                 public void updateItem(Computer item, boolean isEmpty){
  45.                     super.updateItem(item, isEmpty);
  46.                     if(item != null && !isEmpty){
  47.                         HBox parentPane = new HBox();
  48.                         for(Tag tag : Tag.values()){
  49.                             ToggleButton toggleButton = new ToggleButton(tag.toString());
  50.                             toggleButton.selectedProperty()
  51.                                     .addListener(new ChangeListener<Boolean>(){
  52.                                         @Override
  53.                                         public void changed(
  54.                                                 ObservableValue<? extends Boolean> observable,
  55.                                                 Boolean oldValue, Boolean newValue){
  56.                                             if(newValue.booleanValue()){
  57.                                                 item.getTags().add(tag);
  58.                                             } else{
  59.                                                 item.getTags().remove(tag);
  60.                                             }
  61.                                             System.out.println(
  62.                                                     "COMPUTER " + item.getId() + ":" + item.tags);
  63.                                         }
  64.  
  65.                                     });
  66.                             parentPane.getChildren().add(toggleButton);
  67.                         }
  68.  
  69.                         this.setGraphic(parentPane);
  70.                     }
  71.                 }
  72.             };
  73.         });
  74.         computerView.setItems(obsComputers);
  75.  
  76.         StackPane contentPane = new StackPane();
  77.         contentPane.getStylesheets()
  78.                 .add(TestListView.class.getResource("list-view.css").toExternalForm());
  79.  
  80.         // list-view.css
  81.         // .toggle-button{
  82.         // -fx-background-color: blue;
  83.         // }
  84.         //
  85.         // .toggle-button:selected{
  86.         // -fx-background-color: red;
  87.         // }
  88.  
  89.         contentPane.getChildren().add(computerView);
  90.         primaryStage.setScene(new Scene(contentPane, 500, 500));
  91.         primaryStage.show();
  92.  
  93.     }
  94.  
  95.     public static void main(String[] args){
  96.         launch(args);
  97.     }
  98.  
  99.     @Data
  100.     private static class Computer{
  101.         private static final AtomicInteger ID_GENERATOR = new AtomicInteger();
  102.         private SimpleBooleanProperty stateProperty = new SimpleBooleanProperty(false);
  103.         private Set<Tag> tags = FXCollections.observableSet();
  104.         private static final boolean OFF = false;
  105.         private static final boolean ON = true;
  106.         private int id = ID_GENERATOR.getAndIncrement();
  107.  
  108.         private SimpleBooleanProperty stateProperty(){
  109.             return stateProperty;
  110.         }
  111.     }
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement