2016-06-11 39 views
-1

我正在學習JavaFX,我想用書籍和其他圖片的圖片填充一個表格列。我可以這樣做嗎?當我嘗試時,我只能成功地爲所有書籍獲得一張圖片。簡單的例子如何在JavaFX中使用數組列表中的圖像填充表格?

package tableviewexample; 

import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.ArrayList; 
import javafx.scene.image.Image; 


public class Book { 
     public static String path = "src/tableviewexample/Opis/"; 
     public static ArrayList<Book> list = new ArrayList<>(); 
    private boolean checked; 
    private boolean isItAvailable; 
    private String bookName; 
    private String isbn; 
    private String authorName; 
    private int numberOfPages; 
    private String summary; 
    private String publisher; 
    private String mainPage; 
    private double cost; 
    private int quantity; 
    private String genre; 
    private Image image; 
    public Book() { 

    } 
    public Book(boolean checked, boolean isItAvailable, String bookName, String isbn, String authorName, int numberOfPages, 
        String summary, String publisher, String mainPage, double cost, 
        int quantity, String genre) { 
     this.isItAvailable = isItAvailable; 
     this.checked = checked; 
     this.bookName = bookName; 
     this.isbn = isbn; 
     this.authorName = authorName; 
     this.numberOfPages = numberOfPages; 
     this.summary = summary; 
     this.publisher = publisher; 
     this.mainPage = mainPage; 
     this.cost = cost; 
     this.quantity = quantity; 
     this.genre = genre; 
    } 
    public String getBookName() { 
     return bookName.get(); 
    } 

    public void setBookName(String bookName) { 
     this.bookName.set(bookName); 
    } 
    public String getBookName() { 
     return bookName; 
    } 
    public void setBookName(String bookName) { 
     this.bookName = bookName; 
    } 
    public String getIsbn() { 
     return isbn; 
    } 

    public void setIsbn(String isbn) { 
     this.isbn = isbn; 
    } 

    public String getAuthorName() { 
     return authorName; 
    } 

    public void setAuthorName(String authorName) { 
     this.authorName = authorName; 
    } 

    public int getNumberOfPages() { 
     return numberOfPages; 
    } 

    public void setNumberOfPages(int numberOfPages) { 
     this.numberOfPages = numberOfPages; 
    } 

    public String getSummary() { 
     return summary; 
    } 


    public void setSummary(String summary) { 
     this.summary = summary; 
    } 


    public String getPublisher() { 
     return publisher; 
    } 


    public void setPublisher(String publisher) { 
     this.publisher = publisher; 
    } 


    public String getMainPage() { 
     return mainPage; 
    } 


    public void setMainPage(String mainPage) { 
     this.mainPage = mainPage; 
    } 

    public double getCost() { 
     return cost; 
    } 

    public void setCost(double cost) { 
     this.cost = cost; 
    } 


    public int getQuantity() { 
     return quantity; 
    } 


    public void setQuantity(int quantity) { 
     this.quantity = quantity; 
    } 

    public void setGenre(String genre) { 
     this.genre = genre; 
    } 

    public String getGenre() { 
     return genre; 
    } 

    public boolean getChecked() { 
     return checked; 
    } 

    public void setChecked(boolean checked) { 
     this.checked = checked; 
    } 

    public boolean getIsItAvailable() { 
     return isItAvailable; 
    } 

    public void setIsItAvailable(boolean isItAvailable) { 
     this.isItAvailable = isItAvailable; 
    } 

    public String toString() { 
     return "AUTHOR: " + getAuthorName() + "\nPUBLISHER:" + getPublisher() + 
       "\nNAME:" + getBookName() + "\nPRICE: " + getCost(); 
    } 

    //helper function from file to string convertor 
    public static String fromFileToString(File file) 
    throws IOException { 
     int len; 
     char[] chr = new char[4096]; 
     final StringBuffer buffer = new StringBuffer(); 
     final FileReader reader = new FileReader(file); 
     try { 
      while ((len = reader.read(chr)) > 0) { 
       buffer.append(chr, 0, len); 
      } 
     } finally { 
      reader.close(); 
     } 
     return buffer.toString(); 
    } 
    public static void init() { 
     ArrayList<String> txtSummary = new ArrayList<>(); 
     txtSummary.add("basara.txt"); 
     txtSummary.add("igraPrestola.txt"); 
     txtSummary.add("sudar_kraljeva.txt"); 
     txtSummary.add("oluja_maceva.txt"); 

     try { 
     Book pBasara = new Book(false, true, "Fama o biciklistima", "978-86-521-1090-2", "Svetislav Basara", 
     334," ","Dereta", "src/tableviewexample/Images/basara.jpg", 16.50, 20, "filozofsko - religijski roman, istorijska"); 
     pBasara.setSummary(Book.fromFileToString(new File(path + txtSummary.get(0)))); 

     Book pIgraPrestola = new Book(false, true, "Igra prestola", "978-86-743-6099-6", "Dzordz R.R. Martin", 
     599,"","Laguna", "src/tableviewexample/Images/igraPrestola.jpg", 20 , 2, "epska fantastika, politicka strategija"); 
     pIgraPrestola.setSummary(Book.fromFileToString(new File(path + txtSummary.get(1)))); 

     Book pSudarKraljeva = new Book(false, true,"Sudar Kraljeva", "978-86-743-6140-5", "Dzordz R.R. Martin", 
     672,"","Laguna", "src/tableviewexample/Images/sudar_kraljeva.jpg", 26.20 , 2, "epska fantastika, politicka strategija"); 
     pSudarKraljeva.setSummary(Book.fromFileToString(new File(path + txtSummary.get(2)))); 

     Book pOlujaMaceva = new Book(false, true, "Oluja maceva -deo prvi: Celik i sneg", "978-86-743-6185-6", "Dzordz R.R. Martin", 
     384,"","Laguna", "src/tableviewexample/Images/oluja_maceva.jpg", 20.50 , 2, "epska fantastika, politicka strategija"); 
     pOlujaMaceva.setSummary(Book.fromFileToString(new File(path + txtSummary.get(3)))); 
     list.add(pBasara); 
     list.add(pIgraPrestola); 
     list.add(pSudarKraljeva); 
     list.add(pOlujaMaceva); 
     } catch(Exception e) { 
      e.printStackTrace(); 
    } 


     } 
    } 

而且實現代碼如下例子

package tableviewexample; 

import java.io.File; 
import javafx.application.Application; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.geometry.Insets; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.TableCell; 
import javafx.scene.control.TableColumn; 
import javafx.scene.control.TableView; 
import javafx.scene.control.cell.PropertyValueFactory; 
import javafx.scene.image.Image; 
import javafx.scene.image.ImageView; 
import javafx.scene.layout.AnchorPane; 
import javafx.scene.layout.GridPane; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.StackPane; 
import javafx.stage.Stage; 
import javafx.util.Callback; 


public class TableViewExample extends Application { 

    Button catalogue; 
    Button back; 
    Stage stage; 
    Scene sceneTable; 
    Scene sceneFirst; 
    GridPane pane; 
    @Override 
    public void start(Stage primaryStage) { 

     catalogue = new Button("Catalogue"); 
     Book.init(); 
     StackPane root = new StackPane(); 
     stage = primaryStage; 
     pane = new GridPane(); 
     sceneFirst = new Scene(pane,200, 200); 
     pane.add(catalogue, 0, 0); 
     catalogue.setOnAction(new EventHandler<ActionEvent>() { 
      @Override 
      public void handle(ActionEvent event){ 
       ObservableList<Book> data = FXCollections.observableArrayList(Book.list); 
       TableView<Book> table = new TableView<Book>(); 
       table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY); 
       TableColumn<Book, String> imageCol = new TableColumn<Book, String>(); 
       TableColumn<Book, String> descriptionCol = new TableColumn<Book, String>(); 
       Callback<TableColumn<Book, String>, TableCell<Book, String>> cellFactory; 
       table.setItems(data); 

       back = new Button("Back"); 
       AnchorPane root = new AnchorPane(); 
       sceneTable = new Scene(root, 500, 500); 

       imageCol.setCellValueFactory(new PropertyValueFactory<Book, String>("mainPage")); 
       descriptionCol.setCellValueFactory(new PropertyValueFactory<Book, String>("summary")); 

       cellFactory = new Callback<TableColumn<Book, String>, TableCell<Book, String>>() { 
       @Override 
       public TableCell call(final TableColumn param) { 
              final HBox box= new HBox();       
        ImageView imageview = new ImageView();      
        final TableCell cell = new TableCell() { 
        @Override 
        public void updateItem(Object item, boolean empty) { 
         super.updateItem(item, empty); 
         if (item != null) { 
          box.setSpacing(10) ; 
          box.setPadding(new Insets(10, 10, 10, 10)); 
          Image img = null; 
          //if i put foor loop here, it wil set image of last book in list 
//this is confusing part for me 
// how to make to get exact image for every book 
           //this line is only for example, just to show something in table 

          Book book = Book.list.get(0); 
          img = new Image(new File(book.getMainPage()).toURI().toString()); 

           imageview.setImage(img); 
           imageview.setFitHeight(320.0); 
           imageview.setFitWidth(200.0); 
           box.getChildren().add(imageview);  

         } 
        } 
        }; 
       cell.setGraphic(box); 
       return cell; 
       } 
      }; 
      imageCol.setCellFactory(cellFactory); 
       table.getColumns().addAll(imageCol, descriptionCol); 
       AnchorPane.setTopAnchor(table, 30.0); 
       AnchorPane.setLeftAnchor(table, 10.0); 
       AnchorPane.setRightAnchor(table, 10.0); 
       AnchorPane.setBottomAnchor(table, 10.0); 
      //  refresh(table, data); 
       root.getChildren().addAll(table, back); 
       back.setOnAction(e -> ButtonClicked(e)); 
      stage.setScene(sceneTable); 
      } 

      public void ButtonClicked(ActionEvent e) { 
       if(e.getSource() == back) { 
        stage.setScene(sceneFirst); 
       } 

      } 
     });  


     primaryStage.setScene(sceneFirst); 
     primaryStage.show(); 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     launch(args); 
    } 

} 
+0

爲什麼在單元工廠中使用原始類型?你應該有'公共TableCell 調用(TableColumn param){...}',然後相應地修復'updateItem'方法中的所有類型。 –

+0

對不起,但我不確定我瞭解你:( – MGKP

+0

你的單元工廠的調用方法被定義爲'公共TableCell調用(TableColumn param)'。爲什麼???爲什麼你使用'TableCell'(這是一個原始類型,你應該得到一個關於它的警告),而不是一個正確的類型參數:'TableCell '? –

回答

0

這是因爲你總是拿到的第一本書在列表中:Book book = Book.list.get(0);

嘗試改變,爲: img = new Image(new File(item.toString()));

+0

是的,是的,我只是修復它以獲得一些東西。 item; 但只拋出 java.lang.ClassCastException:java.lang.String不能轉換爲tableviewexample.Book – MGKP

+0

你能告訴我什麼類型的項目在「updateItems」嗎?你可以使用item.getClass()來打印 – SomeDude

+0

It's String(item.getClass()的輸出:class java.lang.String)我認爲這是因爲我在Book類中擁有mainPage屬性,它代表了圖書首頁的路徑 – MGKP

0

要獲得本書從updateItem()方法當前行中,你可以做

Book book = getTableView().getItems().get(getIndex()); 

對於這個工作,你需要正確輸入的一切,而不是使用原始類型(你知道有你的IDE正在發出各種警告的原因吧?):

cellFactory = new Callback<TableColumn<Book, String>, TableView<Book, String>>() { 
    @Override 
    public TableCell<Book, String> call(TableColumn<Book, String> param) { 
     return new TableCell<Book, String>() { 
      @Override 
      protected void updateItem(String item, boolean empty) { 
       super.updateItem(item, empty); 
       if (empty) { 
        setGraphic(null); 
       } else { 
        // if you really need the book, do: 
        Book book = getTableView().getItems().get(getIndex()); 

        // but surely you just need 
        imageview.setImage(new Image(item)); 
        // ... 
        setGraphic(box); 
       } 
      } 
     }; 
    } 
}; 

或者在更新的樣式中:

cellFactory = tCol -> new TableCell<Book, String>() { 
    @Overide 
    protected void updateItem(String item, boolean empty) { 

     super.updateItem(item, empty); 
     if (empty) { 
      setGraphic(null); 
     } else { 
      Book book = getTableView().getItems().get(getIndex()); 
      // ... 
      setGraphic(box); 
     } 

    } 
}l 
+0

非常感謝!我被困在這兩天:) – MGKP