2015-10-06 54 views
1

我有多個對象。比方說,我有10本書對象,我希望用戶選擇任何數量的書。當用戶按下提交按鈕時,我想檢索用戶選擇的所有書籍「對象」。如何在javafx中爲複選框保存整個對象

截至目前,同時顯示出對用戶屏幕我使用

CheckBox cb= new CheckBox(book.getName()); 

這表明BOOKNAME到用戶和用戶選擇的書。但是在運行時,我還需要bookid和book對象的其他屬性。

無論如何,通過它我可以保存圖書對象在複選框?

+0

爲什麼不使用'ListView '而只是使用內置的選擇模型,而不是使用複選框? –

回答

3

基本檢查。如果你需要更多的特徵你需要發佈你的代碼,我們可以使用setUserDate設置對象到節點,然後我們可以在需要時使用該對象。在這裏我使用例如對象的ID,侑情況下保存該對象我希望這將解決您的問題,?的發表評論

import javafx.application.Application; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.CheckBox; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 

public class UserData extends Application { 

public void start(Stage primaryStage) { 
    VBox root = new VBox(); 
    Book book = new Book(22, "firstBok"); 
    Book book1 = new Book(2, "secondBok"); 
    CheckBox checkB = new CheckBox(book.getName()); 
    checkB.setUserData(book); 
    CheckBox checkB1 = new CheckBox(book1.getName()); 
    checkB1.setUserData(book1); 
    Button btn = new Button("Submit"); 
    btn.setOnAction(new EventHandler<ActionEvent>() { 

     @Override 
     public void handle(ActionEvent event) { 
      if (checkB.isSelected()) { 
       int firstCheckBxId = ((Book) checkB.getUserData()).getId(); 
       System.out.println("id:" + firstCheckBxId); 
      } 
      if (checkB1.isSelected()) { 
       int secondCheckBxId = ((Book) checkB1.getUserData()).getId(); 
       System.out.println("id:" + secondCheckBxId); 
      } 

     } 
    }); 
    root.getChildren().addAll(checkB, checkB1, btn); 
    primaryStage.setScene(new Scene(root)); 
    primaryStage.show(); 
} 

class Book { 
    int id; 
    private String name; 

    Book(int id, String name) { 
     this.id = id; 
     this.name = name; 
    } 

    public int getId() { 
     return this.id; 
    } 

    public String getName() { 
     return name; 
    } 

} 

public static void main(String[] args) { 
    launch(args); 
} 

} 
0

考慮使用控制內置的選擇功能,如ListView 。然後你可以檢查選擇模型。

import java.util.stream.IntStream; 

import javafx.application.Application; 
import javafx.beans.property.SimpleStringProperty; 
import javafx.beans.property.StringProperty; 
import javafx.geometry.Insets; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.ListCell; 
import javafx.scene.control.ListView; 
import javafx.scene.control.SelectionMode; 
import javafx.scene.layout.BorderPane; 
import javafx.stage.Stage; 

public class BookSelection extends Application { 

    @Override 
    public void start(Stage primaryStage) { 

     ListView<Book> bookList = new ListView<>(); 
     bookList.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); 

     bookList.setCellFactory(lv -> new ListCell<Book>() { 
      @Override 
      public void updateItem(Book book, boolean empty) { 
       super.updateItem(book, empty); 
       if (empty) { 
        setText(null); 
       } else { 
        setText(book.getTitle()); 
       } 
      } 
     }); 

     IntStream.rangeClosed(1, 10).mapToObj(i -> new Book("Book "+i)).forEach(bookList.getItems()::add); 

     Button submit = new Button("Submit selection"); 
     submit.setOnAction(e -> 
       bookList.getSelectionModel().getSelectedItems().forEach(book -> System.out.println(book.getTitle()))); 

     BorderPane root = new BorderPane(bookList, null, null, submit, null); 
     BorderPane.setAlignment(submit, Pos.CENTER); 
     BorderPane.setMargin(submit, new Insets(10)); 

     Scene scene = new Scene(root, 600, 600); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    public static class Book { 
     private final StringProperty title = new SimpleStringProperty() ; 
     public Book(String title) { 
      setTitle(title); 
     } 
     public final StringProperty titleProperty() { 
      return this.title; 
     } 
     public final java.lang.String getTitle() { 
      return this.titleProperty().get(); 
     } 
     public final void setTitle(final java.lang.String title) { 
      this.titleProperty().set(title); 
     } 


    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 

如果由於某種原因,你真的想用複選框來實現這一點,你可以保持一個Set<Book>代表所選書籍,並選擇每個複選框時更新/未選擇。請注意,這與@ user99370的答案類似,但更爲強大,因爲它避免了將(基本上未知類型的)userData向下轉換爲您的數據類型。

import java.util.HashSet; 
import java.util.List; 
import java.util.Set; 
import java.util.stream.Collectors; 
import java.util.stream.IntStream; 

import javafx.application.Application; 
import javafx.beans.property.SimpleStringProperty; 
import javafx.beans.property.StringProperty; 
import javafx.geometry.HPos; 
import javafx.geometry.Insets; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.CheckBox; 
import javafx.scene.control.Label; 
import javafx.scene.layout.GridPane; 
import javafx.stage.Stage; 

public class BookSelection extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     GridPane grid = new GridPane(); 
     grid.setHgap(5); 
     grid.setVgap(5); 
     grid.setPadding(new Insets(10)); 
     List<Book> books = IntStream.rangeClosed(1, 10).mapToObj(i -> new Book("Book "+i)).collect(Collectors.toList()); 

     Set<Book> selectedBooks = new HashSet<>(); 

     int row = 0 ; 
     for (Book book : books) { 
      CheckBox checkBox = new CheckBox(); 
      checkBox.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> { 
       if (isNowSelected) { 
        selectedBooks.add(book); 
       } else { 
        selectedBooks.remove(book); 
       } 
      }); 
      grid.addRow(row, checkBox, new Label(book.getTitle())); 
      row++ ; 
     } 

     Button submit = new Button("Submit selection"); 
     submit.setOnAction(e -> 
       selectedBooks.forEach(book -> System.out.println(book.getTitle()))); 

     GridPane.setHalignment(submit, HPos.CENTER); 
     grid.add(submit, 0, row, 2, 1); 

     Scene scene = new Scene(grid, 600, 600); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    public static class Book { 
     private final StringProperty title = new SimpleStringProperty() ; 
     public Book(String title) { 
      setTitle(title); 
     } 
     public final StringProperty titleProperty() { 
      return this.title; 
     } 
     public final java.lang.String getTitle() { 
      return this.titleProperty().get(); 
     } 
     public final void setTitle(final java.lang.String title) { 
      this.titleProperty().set(title); 
     } 


    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
}