I quite understand the implementation of GridView to achieve a representation of the image below, I do like to get an implementation of that also done in javafx, whereby the data will be fetched from MYSQL database.
I really do not know how to get about this implementation, i.e. fetching a data which will be dynamic and represented with the style above.
I sincerely appreciate your time.
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;
}
}
I am getting
Caused by: java.lang.NullPointerException at controllers.Home_pageController.getGrid(Home_pageController.java:85) at controllers.Home_pageController.start(Home_pageController.java:54) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863) at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326) at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294) at 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)