- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我非常了解 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;
}
}
我正在获取
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)
最佳答案
我猜您确实喜欢 JavaFX FXML,但我会向您指出 JDBC 和一些手工编码的 JavaFX。
步骤是安装 MYSQL,安装 ConnectorJ(这是从代码连接到 MySQL(驱动程序)的一种方法)记录密码,然后创建一个新数据库,添加一个表和您需要的列。
警告:这是一个大问题,并且已经在很多地方可用,但是您已经问过,所以我提供了一些代码。 GUI 部分将运行,数据库部分需要调整。
我还在下面为您演示了一个 GridPane。但我认为您想要的是绑定(bind)变量,以便您的 View 自动更新 - 作为开始,正如我在代码中评论的那样,请查看 TableView 和 Cell Factories。另请检查数据绑定(bind)和类,例如: https://docs.oracle.com/javase/8/javafx/api/javafx/beans/property/SimpleStringProperty.html
这段代码有一百万个错误:你永远不应该从 GUI 线程等进行数据库调用。但你必须从某个地方开始。如果这有帮助,请相应标记。
import com.techtalix.util.Const;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class Test extends Application {
Connection connect = null;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
List<ImageDataObjs> imageURLs = null; //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
connectToDB();
imageURLs = makeDBCall();
GridPane root = new GridPane();
Scene theScene = new Scene(root);
stage.setTitle("Some scene");
stage.setScene(theScene);
root.setPadding(new Insets(10,10,10,10));
root.setHgap(10);
root.setVgap(10);
int cols=2, colCnt = 0, rowCnt = 0;
for (int i=0; i<imageURLs.size(); i++) {
root.add(imageURLs.get(i).imageView, colCnt, rowCnt);
colCnt++;
if (colCnt>cols) {
rowCnt++;
colCnt=0;
}
}
// in terms of auto update, instead of gridpane, I suggest you look into TableView, and cell factories.
// once you have bound your cell factories, you can make calls to your DB, and the values bound will update automatically
//http://www.java2s.com/Tutorials/Java/JavaFX/0650__JavaFX_TableView.htm
stage.initStyle(StageStyle.UNDECORATED); //remove this is you want to see the title bar etc.
stage.show();
}
class ImageDataObjs {
final ImageView imageView;
String imgURL, price;
public ImageDataObjs(String imgURL, String price) {
this.imgURL = imgURL;
this.price = price;
Path imagePath = Paths.get(imgURL);
File imageFile = imagePath.toFile();
Image image = new Image(imageFile.toURI().toString());
this.imageView = new ImageView(image);
}
}
//there a million examples of theis on the web
private List<ImageDataObjs> makeDBCall() {
List<ImageDataObjs> imageDataObjList = new ArrayList<>();
String myPrice, myURL;
try {
Statement statement = connect.createStatement();
ResultSet resultSet = statement
.executeQuery("select * from myProductList"); //you need to have a table called myProductList in your database
while (resultSet.next()) {
myPrice = resultSet.getString("myImageURL"); //these are the SQL cols that you should have already created in your database
myURL = resultSet.getString("somePriceData");
imageDataObjList.add(new ImageDataObjs(myURL, myPrice));
}
connect.close();
} catch (Exception e) {
e.printStackTrace();
}
/*
JUST FOR EXAMPLE - this would come from your database query
*/
imageDataObjList.add(new ImageDataObjs(Const.BASE_DIRECTORY + "png/bridge.png", "1.99"));
imageDataObjList.add(new ImageDataObjs(Const.BASE_DIRECTORY + "png/bridge.png", "2.99"));
imageDataObjList.add(new ImageDataObjs(Const.BASE_DIRECTORY + "png/bridge.png", "3.99"));
imageDataObjList.add(new ImageDataObjs(Const.BASE_DIRECTORY + "png/bridge.png", "4.99"));
imageDataObjList.add(new ImageDataObjs(Const.BASE_DIRECTORY + "png/bridge.png", "5.99"));
imageDataObjList.add(new ImageDataObjs(Const.BASE_DIRECTORY + "png/bridge.png", "6.99"));
imageDataObjList.add(new ImageDataObjs(Const.BASE_DIRECTORY + "png/bridge.png", "6.99"));
return imageDataObjList;
}
private void connectToDB() {
//Note: you'll still it in tutorials, but Class.forName() is not needed since JDBC 4.0.
//make sure but this point that MYSQL is installed, and that ConnectorJ is also installed.
//create a database called "someDatabase" - make sure user and password are are you installed them.
try {
connect =
DriverManager.getConnection("jdbc:mysql://localhost/someDatabase" +
"?user=root&password=root");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
关于java - 如何在javafx中实现GridView样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45450489/
我需要将文本放在 中在一个 Div 中,在另一个 Div 中,在另一个 Div 中。所以这是它的样子: #document Change PIN
奇怪的事情发生了。 我有一个基本的 html 代码。 html,头部, body 。(因为我收到了一些反对票,这里是完整的代码) 这是我的CSS: html { backgroun
我正在尝试将 Assets 中的一组图像加载到 UICollectionview 中存在的 ImageView 中,但每当我运行应用程序时它都会显示错误。而且也没有显示图像。 我在ViewDidLoa
我需要根据带参数的 perl 脚本的输出更改一些环境变量。在 tcsh 中,我可以使用别名命令来评估 perl 脚本的输出。 tcsh: alias setsdk 'eval `/localhome/
我使用 Windows 身份验证创建了一个新的 Blazor(服务器端)应用程序,并使用 IIS Express 运行它。它将显示一条消息“Hello Domain\User!”来自右上方的以下 Ra
这是我的方法 void login(Event event);我想知道 Kotlin 中应该如何 最佳答案 在 Kotlin 中通配符运算符是 * 。它指示编译器它是未知的,但一旦知道,就不会有其他类
看下面的代码 for story in book if story.title.length < 140 - var story
我正在尝试用 C 语言学习字符串处理。我写了一个程序,它存储了一些音乐轨道,并帮助用户检查他/她想到的歌曲是否存在于存储的轨道中。这是通过要求用户输入一串字符来完成的。然后程序使用 strstr()
我正在学习 sscanf 并遇到如下格式字符串: sscanf("%[^:]:%[^*=]%*[*=]%n",a,b,&c); 我理解 %[^:] 部分意味着扫描直到遇到 ':' 并将其分配给 a。:
def char_check(x,y): if (str(x) in y or x.find(y) > -1) or (str(y) in x or y.find(x) > -1):
我有一种情况,我想将文本文件中的现有行包含到一个新 block 中。 line 1 line 2 line in block line 3 line 4 应该变成 line 1 line 2 line
我有一个新项目,我正在尝试设置 Django 调试工具栏。首先,我尝试了快速设置,它只涉及将 'debug_toolbar' 添加到我的已安装应用程序列表中。有了这个,当我转到我的根 URL 时,调试
在 Matlab 中,如果我有一个函数 f,例如签名是 f(a,b,c),我可以创建一个只有一个变量 b 的函数,它将使用固定的 a=a1 和 c=c1 调用 f: g = @(b) f(a1, b,
我不明白为什么 ForEach 中的元素之间有多余的垂直间距在 VStack 里面在 ScrollView 里面使用 GeometryReader 时渲染自定义水平分隔线。 Scrol
我想知道,是否有关于何时使用 session 和 cookie 的指南或最佳实践? 什么应该和什么不应该存储在其中?谢谢! 最佳答案 这些文档很好地了解了 session cookie 的安全问题以及
我在 scipy/numpy 中有一个 Nx3 矩阵,我想用它制作一个 3 维条形图,其中 X 轴和 Y 轴由矩阵的第一列和第二列的值、高度确定每个条形的 是矩阵中的第三列,条形的数量由 N 确定。
假设我用两种不同的方式初始化信号量 sem_init(&randomsem,0,1) sem_init(&randomsem,0,0) 现在, sem_wait(&randomsem) 在这两种情况下
我怀疑该值如何存储在“WORD”中,因为 PStr 包含实际输出。? 既然Pstr中存储的是小写到大写的字母,那么在printf中如何将其给出为“WORD”。有人可以吗?解释一下? #include
我有一个 3x3 数组: var my_array = [[0,1,2], [3,4,5], [6,7,8]]; 并想获得它的第一个 2
我意识到您可以使用如下方式轻松检查焦点: var hasFocus = true; $(window).blur(function(){ hasFocus = false; }); $(win
我是一名优秀的程序员,十分优秀!