如何使用JavaFX中数组列表中的图像填充表?
我正在学习JavaFX,我想用书籍图像填充一个表列,用摘要填充另一个表列。我可以这样做吗?当我尝试时,我只成功地为所有书籍获得了一个图像。简单例子如何使用JavaFX中数组列表中的图像填充表?,java,javafx,Java,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 cla
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;
导入java.io.File;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.ArrayList;
导入javafx.scene.image.image;
公共课堂用书{
公共静态字符串path=“src/tableviewexample/Opis/”;
public static ArrayList list=new ArrayList();
私有布尔检查;
私有布尔值是可用的;
私有字符串书名;
专用字符串isbn;
私有字符串authorName;
私人int页码;
私有字符串摘要;
私有字符串发布器;
私有字符串主页;
私人双重成本;
私人整数数量;
私人弦乐体裁;
私有图像;
公共书籍(){
}
公共图书(布尔值选中,布尔值isItAvailable,字符串bookName,字符串isbn,字符串authorName,int numberOfPages,
字符串摘要、字符串发布者、字符串主页、双重成本、,
整数数量,字符串类型){
this.isItAvailable=isItAvailable;
this.checked=checked;
this.bookName=bookName;
这是isbn=isbn;
this.authorName=authorName;
this.numberOfPages=numberOfPages;
this.summary=摘要;
this.publisher=publisher;
this.mainPage=mainPage;
成本=成本;
这个。数量=数量;
这个。流派=流派;
}
公共字符串getBookName(){
返回bookName.get();
}
public void setBookName(字符串bookName){
this.bookName.set(bookName);
}
公共字符串getBookName(){
返回书名;
}
public void setBookName(字符串bookName){
this.bookName=bookName;
}
公共字符串getIsbn(){
返回isbn;
}
公共无效集合isbn(字符串isbn){
这是isbn=isbn;
}
公共字符串getAuthorName(){
返回authorName;
}
public void setAuthorName(字符串authorName){
this.authorName=authorName;
}
public int getNumberOfPages(){
返回页数;
}
公共无效setNumberOfPages(int numberOfPages){
this.numberOfPages=numberOfPages;
}
公共字符串getSummary(){
返回摘要;
}
公共void集合摘要(字符串摘要){
this.summary=摘要;
}
公共字符串getPublisher(){
返回出版商;
}
public void setPublisher(字符串发布器){
this.publisher=publisher;
}
公共字符串getMainPage(){
返回主页;
}
公共void setMainPage(字符串mainPage){
this.mainPage=mainPage;
}
公共成本{
退货成本;
}
公共成本(双倍成本){
成本=成本;
}
公共整数getQuantity(){
退货数量;
}
公共无效设置数量(整数数量){
这个。数量=数量;
}
公共类型(字符串类型){
这个。流派=流派;
}
公共字符串getgreen(){
回归体裁;
}
公共布尔getChecked(){
退货检查;
}
公共void setChecked(布尔值已选中){
this.checked=checked;
}
公共布尔getIsItAvailable(){
返回isItAvailable;
}
公共无效集合isItAvailable(布尔值isItAvailable){
this.isItAvailable=isItAvailable;
}
公共字符串toString(){
返回“AUTHOR:+getAuthorName()+”\n发布者:+getPublisher()+
“\n名称:”+getBookName()+“\n价格:”+getCost();
}
//从文件到字符串转换器的助手函数
公共静态字符串fromFileToString(文件)
抛出IOException{
内伦;
char[]chr=新字符[4096];
最终StringBuffer=新StringBuffer();
最终文件读取器=新文件读取器(文件);
试一试{
而((len=reader.read(chr))>0){
追加缓冲区(chr,0,len);
}
}最后{
reader.close();
}
返回buffer.toString();
}
公共静态void init(){
ArrayList txtSummary=新建ArrayList();
txtsummmary.add(“basara.txt”);
添加(“igraPrestola.txt”);
txtsummmary.add(“sudar_kraljeva.txt”);
添加(“oluja_maceva.txt”);
试一试{
Book pBasara=新书(假,真,“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(新文件(path+txtsummmary.get(0)));
Book pIgraPrestola=新书(假,真,“伊格拉·普雷斯托拉”,“978-86-743-6099-6”,“德佐兹·R·马丁”,
599,“,”拉古纳“,”src/tableviewexample/Images/igraPrestola.jpg“,”20,2,“爱普斯卡幻想曲,政治战略”;
pIgraPrestola.setSummary(Book.fromFileToString(新文件(path+txtsummmary.get(1)));
Book pSudarKraljeva=新书(假,真,“Sudar Kraljeva”,“978-86-743-6140-5”,“Dzordz R.R.Martin”,
672“,”拉古纳“,”src/tableviewexample/Images/sudar_kraljeva.jpg“,”26.20,2,“epska fantastika,Politica strategija”);
pSudarKraljeva.setSummary(Book.fromFileToString(新文件(path+txtsummmary.get(2)));
Book pOlujaMaceva=新书(假、真,“Oluja maceva-deo prvi:Celik i Sneng”,“978-86-743-6185-6”,“Dzordz R.R.Martin”,
384“,”拉古纳“,”src/tableviewexample/Images/oluja_maceva.jpg“,”20.50,2,“epska fantastika,Politica strategija”);
setSummary(Book.fromFileToString(新文件(path+txtsummmary.get(3)));
添加列表(pBasara);
添加列表(pIgraPrestola);
列表。添加(pSuda)
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);
}
}
Book book = getTableView().getItems().get(getIndex());
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