获取图像一次按钮pressed的相应文件名 [英] getting corresponding filename of image once button pressed
问题描述
一个集的图像文件中的加入到File.Then一个imageview的和一个按钮被addded到VBOX类型的数组列表(filelist2),例如纵向方框使用加入到gripane的网格for循环(重复次数等于filelist2的大小)一旦一个按钮是pressed我需要在纵向盒内获取图像的对应的文件名。
说我pressed包含在按钮(1,1){即行NO01,列NO1}我需要获得图像的文件名(1,1)
这里有一个截图:
这是我的code:FXMLController
文件文件=新的文件(D:\\\\ SERVER \\\\服务器内容\\\\ \\\\应用程序图标);
文件[] = filelist1 file.listFiles();
ArrayList的<文件> filelist2 =新的ArrayList<>(); 对于(文件文件1:filelist1){
filelist2.add(文件1); }
btnar =新的ArrayList<>();
的for(int i = 0; I< filelist2.size();我++){
downloadbtn =新按钮(下载);
btnar.add(downloadbtn);
最终诠释指数= I;
downloadbtn.setId(将String.valueOf(指数));
downloadbtn.setOnAction(新的EventHandler<&ActionEvent的GT;(){
@覆盖
公共无效手柄(ActionEvent的为arg0){
尝试{
的System.out.println(SSSSS);
downloadbtn.getId();
//System.out.println(filelist2.get(Integer.valueOf(downloadbtn.getId()))的getName())。 }赶上(例外前){
。Logger.getLogger(HomeUI_2Controller.class.getName())日志(Level.SEVERE,空,前);
}
}
});
} 的System.out.println(filelist2.size());
gridpane.setAlignment(Pos.CENTER);
gridpane.setPadding(新插图(20,20,20,20)); gridpane.setHgap(20);
gridpane.setVgap(20); ColumnConstraints columnConstraints =新ColumnConstraints();
columnConstraints.setFillWidth(真);
columnConstraints.setHgrow(Priority.ALWAYS);
。gridpane.getColumnConstraints()加(columnConstraints); INT imageCol = 0;
INT imageRow = 0; 的for(int i = 0; I< filelist2.size();我++){
的System.out.println(filelist2.get(ⅰ).getName()); 图像=新的图像(filelist2.get(I).toURI()的toString()); PIC =新ImageView的();
pic.setFitWidth(130);
pic.setFitHeight(130);
pic.setImage(图片);
VB =新的垂直框();
。vb.getChildren()的addAll(PIC(按钮)btnar.get(一)); gridpane.add(VB,imageCol,imageRow);
GridPane.setMargin(PIC,新的插图(2,2,2,2));
imageCol ++; //要检查所有的一排3图像完成
如果(imageCol→2){
//重置列
imageCol = 0;
//下一行
imageRow ++;
} }
为什么不直接
的System.out.println(filelist2.get(指数).getName());
(其实,这不是真的明白我,为什么你创建 filelist2
都没有。为什么不这样做。
btnar =新的ArrayList<>();的for(int i = 0; I< filelist1.length;我++){
downloadbtn =新按钮(下载);
btnar.add(downloadbtn);
最终诠释指数= I;
downloadbtn.setId(将String.valueOf(指数));
downloadbtn.setOnAction(新的EventHandler<&ActionEvent的GT;(){
@覆盖
公共无效手柄(ActionEvent的为arg0){
尝试{
的System.out.println(SSSSS);
的System.out.println(filelist1 [指数] .getName()); }赶上(例外前){
。Logger.getLogger(HomeUI_2Controller.class.getName())日志(Level.SEVERE,空,前);
}
}
});
}
A Set of imagefiles are added to an arraylist(filelist2) of type File.Then an imageview and a button are addded to a vbox,such vboxes are added to a grids of a gripane using a for loop.( number of iterations is equal to size of the filelist2)Once a button is pressed I need to get the corresponding filename of the image within that vbox. Say I pressed the button contained at (1,1) {i.e row no01 ,col no1} I need to get filename of image at (1,1) here's a screenshot:
here's my code: FXMLController
File file = new File("D:\\SERVER\\Server Content\\Apps\\icons");
File[] filelist1 = file.listFiles();
ArrayList<File> filelist2 = new ArrayList<>();
for (File file1 : filelist1) {
filelist2.add(file1);
}
btnar = new ArrayList<>();
for (int i = 0; i < filelist2.size(); i++) {
downloadbtn = new Button("Download");
btnar.add(downloadbtn);
final int index=i;
downloadbtn.setId(String.valueOf(index));
downloadbtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
try {
System.out.println("sssss");
downloadbtn.getId();
//System.out.println(filelist2.get(Integer.valueOf(downloadbtn.getId())).getName());
} catch (Exception ex) {
Logger.getLogger(HomeUI_2Controller.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
System.out.println(filelist2.size());
gridpane.setAlignment(Pos.CENTER);
gridpane.setPadding(new Insets(20, 20, 20, 20));
gridpane.setHgap(20);
gridpane.setVgap(20);
ColumnConstraints columnConstraints = new ColumnConstraints();
columnConstraints.setFillWidth(true);
columnConstraints.setHgrow(Priority.ALWAYS);
gridpane.getColumnConstraints().add(columnConstraints);
int imageCol = 0;
int imageRow = 0;
for (int i = 0; i < filelist2.size(); i++) {
System.out.println(filelist2.get(i).getName());
image = new Image(filelist2.get(i).toURI().toString());
pic = new ImageView();
pic.setFitWidth(130);
pic.setFitHeight(130);
pic.setImage(image);
vb = new VBox();
vb.getChildren().addAll(pic, (Button) btnar.get(i));
gridpane.add(vb, imageCol, imageRow);
GridPane.setMargin(pic, new Insets(2, 2, 2, 2));
imageCol++;
// To check if all the 3 images of a row are completed
if (imageCol > 2) {
// Reset Column
imageCol = 0;
// Next Row
imageRow++;
}
}
Why not simply
System.out.println(filelist2.get(index).getName());
?
(Actually, it's not really clear to me why you create filelist2
at all. Why not do
btnar = new ArrayList<>();
for (int i=0; i < filelist1.length; i++) {
downloadbtn = new Button("Download");
btnar.add(downloadbtn);
final int index=i;
downloadbtn.setId(String.valueOf(index));
downloadbtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
try {
System.out.println("sssss");
System.out.println(filelist1[index].getName());
} catch (Exception ex) {
Logger.getLogger(HomeUI_2Controller.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
这篇关于获取图像一次按钮pressed的相应文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!