我非常了解GridView的实现,以实现下面图像的表示,我喜欢得到一个也在javafx中完成的实现,其中数据将从MYSQL数据库中获取 .
我真的不知道如何实现这个实现,即获取一个动态的数据并用上面的样式表示 .
我真诚地感谢你的时间 .
public class Home_pageController extends Application {
@FXML
private GridPane myGrid;
@FXML
private JFXButton yes_button;
/**
* Initializes the controller class.
*/
/**
* Initializes the controller class.
*/
@Override
public void start(Stage stage) throws Exception {
// Parent root = FXMLLoader.load(getClass().getResource("Student.fxml"));
getGrid();
Parent root = FXMLLoader.load(getClass().getResource("home_page.fxml"));
//GridPane root = new GridPane();
Scene scene = new Scene(root);
stage.setTitle("Some scene");
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public void getGrid() {
List<ImageDataObjs> imageURLs ; //you'll use something like this, but I've not for this little helper
//here you'll want to make a database setup and a call: This is a really bad program, but it will demonstrate
conn = javaconnect.ConnectDb();
imageURLs = makeDBCall();
GridPane gridPane = new GridPane();
// myGrid.setGridLinesVisible(true);
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
System.out.print("Just me here again "+imageURLs.get(i).the_image);
Label label = new Label("Label " + i + "/" + j);
// label.setMouseTransparent(true);
GridPane.setRowIndex(label, i);
GridPane.setColumnIndex(label, j);
myGrid.getChildren().add(imageURLs.get(10).the_image);
}
}
}
public static void main(String[] args) {
launch(args);
}
class ImageDataObjs {
// final ImageView imageView;
// String imgURL, price;
@FXML
final ImageView the_image;
String imgURL;
byte[] price;
public ImageDataObjs(String imgURL, byte[] price) throws IOException {
this.imgURL = imgURL;
this.price = price;
InputStream in = new ByteArrayInputStream(price);
WritableImage images = new WritableImage(50, 50);
ByteArrayInputStream bis = new ByteArrayInputStream(price);
BufferedImage read = ImageIO.read(bis);
images = SwingFXUtils.toFXImage(read, null);
this.the_image = new ImageView(images);
}
}
Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;
static byte[] staff_image;
//there a million examples of theis on the web
private List<ImageDataObjs> makeDBCall() {
List<ImageDataObjs> imageDataObjList = new ArrayList<>();
// String myPrice, myURL;
byte[] myPrice;
String myURL;
try {
String sql = "select * from phone_types";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
while (rs.next()) {
myPrice = rs.getBytes("phone_image");
myURL = rs.getString("phone_name");
System.out.println("I am just here "+ myPrice);
imageDataObjList.add(new ImageDataObjs(myURL, myPrice));
}
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return imageDataObjList;
}
}
我正进入(状态
引起:controller.Home_pageController.getGrid(Home_pageController.java:85)的controllers.Home_pageController.start(Home_pageController.java:54)中的java.lang.NullPointerException:com.sun.javafx.application.LauncherImpl.lambda $ launchApplication1 $ 162 (LauncherImpl.java:863)com.sun.javafx.application.PlatformImpl.lambda $ runAndWait $ 175(PlatformImpl.java:326)at com.sun.javafx.application.PlatformImpl.lambda $ null $ 173(PlatformImpl.java:295 )在com.sun.javafx.application.PlatformImpl.lambda的java.security.AccessController.doPrivileged(本地方法)$ comLun $ 174(PlatformImpl.java:294)com.sun.glass.ui.InvokeLaterDispatcher $ Future.run( InvokeLaterDispatcher.java:95)at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)at com.sun.glass.ui.win.WinApplication.lambda $ null $ 148(WinApplication.java:191)
1 回答
我认为你真的是在JavaFX FXML之后,但我会指向JDBC和一些手工编写的JavaFX .
步骤是安装MYSQL,安装ConnectorJ(这是一种从代码连接到MySQL(驱动程序)的方法)记录密码,创建一个新的数据库,添加一个表,以及你需要的列 .
警告:这是一个很大的问题,并且已经在许多地方提供过,但你已经问过,所以我提供了一些代码 . GUI部分将运行,数据库部分将需要调整 .
我想要,以便你的视图自动更新,绑定变量 - 作为一个开始,正如我在代码中评论过,请查看TableView和Cell Factories . 还要检查数据绑定,以及类:https://docs.oracle.com/javase/8/javafx/api/javafx/beans/property/SimpleStringProperty.html
这段代码有一百万个错误:你永远不应该从你的GUI线程等进行数据库调用 . 但你必须从某个地方开始 . 如果这有帮助,请相应标记 .
}