这是我的代码
// this event is attached to TableCell
public EventHandler dblclickDescription = new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent event) {
if(event.getButton().equals(MouseButton.PRIMARY)){
if(event.getClickCount() == 2){
printRow(event.getTarget());
}
}
event.consume();
}
};
// print row
public void printRow(Object o){
Text text = (Text) o;
// ??? don't know what to write here
System.out.println(row.toString());
}
Run Code Online (Sandbox Code Playgroud)
1)如何从单击的单元格中获取行?
2)我可以将事件附加到整行而不是每列吗?
编辑:3)我以为我附上了活动 TableCell
TableCell cell = TableColumn.DEFAULT_CELL_FACTORY.call(p);
cell.setOnMouseClicked(dblclickDescription);
Run Code Online (Sandbox Code Playgroud)
但是当我测试时,
event.getSource();// is a TableColumn
event.getTarget();// is a Text if clicked on text
event.getTarget();// is a TableColumn if clicked …
Run Code Online (Sandbox Code Playgroud) 我正在学习javafx.scene.control.ContextMenu,现在我遇到了一个问题:
如何从EventHandler获取单击的对象?event.source()和event.target()均返回MenuItem。
让我用一个例子来解释:我应该在函数句柄里面写什么?
TextField text = new TextField();
Label label1 = new Label("hello");
Label label2 = new Label("world");
Label label3 = new Label("java");
ContextMenu menu = new ContextMenu();
MenuItem item = new MenuItem("copy to text field");
menu.getItems().add(item);
item.setOnAction(new EventHandler(){
public void handle(Event event) {
//I want to copy the text of the Label I clicked to TextField
event.consume();
}
});
label1.setContextMenu(menu);
label2.setContextMenu(menu);
label3.setContextMenu(menu);
Run Code Online (Sandbox Code Playgroud)
编辑:我希望有一些简单的解决方案(一个班轮),但如果没有,那么有很多复杂的方法可以做到这一点。
我是 fxml 的新手,一开始它看起来很容易使用,但是当我给自己一个练习将 oracle 上的一个演示翻译成 fxml 时,我发现自己花了一整天的时间在 google 上。
但我仍然找不到这两个问题的答案:
btnAdd.setMaxHeight(Control.USE_PREF_SIZE);
btnAdd.setMaxWidth(Double.MAX_VALUE);
Run Code Online (Sandbox Code Playgroud)
如果有人知道我在哪里可以找到教程,请分享。
在教程http://docs.oracle.com/javafx/2/ui_controls/tree-view.htm中,它解释了如何使用ContextMenu或CheckBox创建TreeView.
但有可能同时拥有它们吗?
当我第一次复制粘贴代码时,我了解到我只能有一个setCellFactory,因为它们会相互覆盖.
// the following two setCellFactory are copied from the tutorial
// this create TreeCell with ContextMenu
treeView.setCellFactory(new Callback<TreeView<String>,TreeCell<String>>(){
@Override
public TreeCell<String> call(TreeView<String> p) {
return new TextFieldTreeCellImpl();
//the class TextFieldTreeCellImp is a TreeCell with ContextMenu
}
});
// this create TreeCell with CheckBox
tree.setCellFactory(CheckBoxTreeCell.<String>forTreeView());
Run Code Online (Sandbox Code Playgroud)
然后我尝试用CheckBoxTreeCell替换TreeCell
//class TextFieldTreeCellImpl extends TreeCell<String> {
class TextFieldTreeCellImpl extends CheckBoxTreeCell<String> {
...
//TreeItem newTag = new TreeItem<String>("New tag");
CheckBoxTreeItem newTag = new CheckBoxTreeItem<String>("New tag");
Run Code Online (Sandbox Code Playgroud)
但是没有出现复选框.它仍然是一个正常的树视图.
首先renameTo()工作正常,问题是在重命名之后,File f的路径仍然是旧路径而不是新路径,换句话说File f是不可用的,必须进行更改.但是我该怎么做呢?
ArrayList<File> list = new ArrayList();
public void myfunction() {
// some code to fill list
for (File f:list){
changeName(f);
System.out.println(f.getName()); // this print the old name
}
}
public void changeName(File f){
File newFile = new File(new SimpleDateFormat("yyyyMMdd_HHmmss").format(f.lastModified())+".txt");
f.renameTo(newFile);
f = newFile; //this line here doesn't work
}
}
Run Code Online (Sandbox Code Playgroud) 我想确保两个java.io.File没有指向同一个文件,我尝试了各种方法,最后找到了一种方法,但我想确保它周围没有漏洞.
这很重要,因为我正在尝试编写一个删除重复文件的程序,我不想因为两个java.io.File指向同一个文件而最终删除一个唯一的文件.
File f1 = new File("file.txt");
File f2 = new File("./file.txt");
//these methods can't tell it's the same file
System.out.println(f1.compareTo(f2)); // 56 which mean not equal
System.out.println(f1.equals(f2)); // false
System.out.println(f1 == f2); // false
System.out.println(f1.getAbsolutePath().compareTo(f2.getAbsolutePath())); // 56
// this method can tell it's the same file... hopefully.
try{
System.out.println(f1.getCanonicalPath().compareTo(f2.getCanonicalPath())); // 0
}catch (Exception e){
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
另一方面,我的try-catch代码有问题吗?我跑的时候会给我一个警告.
javafx ×4
contextmenu ×2
java ×2
checkbox ×1
eventhandler ×1
file ×1
file-rename ×1
fxml ×1
treeview ×1