我通过javafx创建了一个图书管理应用程序。通过这个应用程序,我们可以执行添加,搜索和删除操作。对于搜索,我使用表视图来获取用户搜索的书的所有详细信息。所以最后我得到的结果显示在控制台中,但不是在表格视图中显示
package Managemnet;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import connection.*;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
public class Searching {
Connection connect;
@FXML
TableView<Book> search_view ;
@FXML
ObservableList<Book> observeList;
@FXML
TableColumn<Book, Integer> Book_I;
@FXML
TableColumn<Book, String> Book_n;
@FXML
TableColumn<Book, String> publish;
@FXML
TableColumn<Book, Integer> ye;
@FXML
TextField sea_Book_name;
@FXML
TextField sea_pub;
@FXML
TextField sea_year;
@FXML
AnchorPane SearchPane;
@FXML
Button search;
public void search() throws ClassNotFoundException, SQLException{
Sql_querry sql=new Sql_querry();
sql.connect_sql();
connect=sql.getConnect();
observeList =FXCollections.<Book>observableArrayList();
search_view=new TableView<Book>();
Book_I=new TableColumn<Book ,Integer>();
Book_I.setCellValueFactory(new PropertyValueFactory<Book ,Integer>
("Book_id"));
Book_n=new TableColumn<Book ,String>();
Book_n.setCellValueFactory(new PropertyValueFactory<Book ,String>
("Book_name"));
publish=new TableColumn<Book ,String>();
publish.setCellValueFactory(new PropertyValueFactory<Book ,String>
("Publisher"));
ye=new TableColumn<Book ,Integer>();
ye.setCellValueFactory(new PropertyValueFactory<Book ,Integer>("year"));
String s1= sea_Book_name.getText();
String s2= sea_pub.getText();
String s= sea_year.getText();
int s3;
if(!s.isEmpty()){
s3=Integer.parseInt(s);
}
else s3=0;
ResultSet res;
Answer_from_dataset obj= new Answer_from_dataset();
res=obj.getAnswer(s1, s2, s3);
while(res.next()){
observeList.add(new Book(res.getInt(1), res.getString(2),
res.getString(3), res.getInt(4)));
//System.out.println(observeList);
}
search_view.setItems(observeList);
System.out.println(search_view);
}
package Managemnet;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
public class Book {
SimpleStringProperty Book_name;
SimpleStringProperty Publisher;
SimpleIntegerProperty Book_id;
SimpleIntegerProperty year;
public Book(int book_id, String book_name, String publisher, int year) {
this.Book_name =new SimpleStringProperty(book_name);
this.Publisher =new SimpleStringProperty(publisher);
this.Book_id =new SimpleIntegerProperty (book_id);
this.year = new SimpleIntegerProperty(year);
}
public String getBook_name() {
return Book_name.get();
}
public String getPublisher() {
return Publisher.get();
}
public Integer getBook_id() {
return Book_id.get();
}
public Integer getYear() {
return year.get();
}
public void setBook_name(String book_name) {
Book_name.set(book_name);
}
public void setPublisher(String publisher) {
Publisher.set(publisher);;
}
public void setBook_id(int book_id) {
Book_id.set(book_id);;
}
public void setYear(int year) {
this.year.set(year);;
}
@Override
public String toString() {
return getBook_id() + " " + getBook_name() + " " + getPublisher() + " "
+ getYear();
}
}
答案 0 :(得分:0)
因此,当我使用JavaFX进行第一个项目时,我就遇到了FXML的问题。
我尝试了很多使用FXML的解决方案,但是这花费了时间,并且没有任何效果。因此,我转而使用POJO。这可能不是您想要的理想选择,但在创建表时会给您带来很大的灵活性。
理想情况下,您要创建TableView的表本身
TableView<Book> table = new TableView();
然后为要创建TableColumn的每一列。现在,在这里您需要小心
TableColumn<Book, String> col1 = new TableColumn<>("Name");
col.setCellValueFactory(data -> new SimpleStringProperty(data.getValue().getName()));
尖括号中的值为
然后您当然可以设置您喜欢的宽度,而不是什么。但是创建TableColumns的相同过程会针对您拥有的表重复其自身。我认为这可能要花更长的时间,但是相对容易理解,最重要的是它可以工作。
尝试一下,让我知道它是否有效!干杯!