Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使JavaFX TableView行不可选择且不可聚焦_Java_Javafx_Tableview_Easybind - Fatal编程技术网

使JavaFX TableView行不可选择且不可聚焦

使JavaFX TableView行不可选择且不可聚焦,java,javafx,tableview,easybind,Java,Javafx,Tableview,Easybind,我试图使JavaFX行不可选择,NoFocusTraversable,但我没有运气这样做。下面是另一个问题的代码,你可以用它来玩。我添加了两个自定义行,以使某些行无焦点可遍历 虽然我不能使某些行不可选择,但有什么想法吗 您需要下载库(或在行工厂内注释上面的行并取消注释下面的行) 代码来自()StackOverflow答案: import java.util.ArrayList; import java.util.Arrays; import java.util.List; import jav

我试图使JavaFX行
不可选择
NoFocusTraversable
,但我没有运气这样做。下面是另一个问题的代码,你可以用它来玩。我添加了两个自定义行,以使某些行
无焦点可遍历

虽然我不能使某些行
不可选择
,但有什么想法吗


您需要下载库(或在
行工厂内注释上面的行并取消注释下面的行)

代码来自()StackOverflow答案:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.function.Function;

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

import org.fxmisc.easybind.EasyBind;

public class DisabledTableRowExample extends Application {

    @Override
    public void start(Stage primaryStage) {
        TableView<Item> table = new TableView<>();
        table.getItems().addAll(createData());

        TableColumn<Item, Item> deleteCol = createTableColumn("Delete", ReadOnlyObjectWrapper<Item>::new);
        deleteCol.setCellFactory(this::createDeleteCell);

        table.getColumns().addAll(Arrays.asList(
                createTableColumn("Name", Item::nameProperty),
                createTableColumn("Value", Item::valueProperty),
                deleteCol 
        ));

        // A row factory that returns a row that disables itself whenever the
        // item it displays has a value less than 5:

        table.setRowFactory(tv -> {
            TableRow<Item> row = new TableRow<>();

            // use EasyBind to access the valueProperty of the itemProperty of the cell:
            row.disableProperty().bind(
                    EasyBind.select(row.itemProperty()) // start at itemProperty of row
                    .selectObject(Item::valueProperty)  // map to valueProperty of item, if item non-null
                    .map(x -> x.intValue() < 5) // map to BooleanBinding via intValue of value < 5
                    .orElse(false)); // value to use if item was null

                row.setFocusTraversable(false); //here
                row.setEditable(false);      //here

            // it's also possible to do this with the standard API, but there are lots of 
            // superfluous warnings sent to standard out:
            // row.disableProperty().bind(
            //          Bindings.selectInteger(row.itemProperty(), "value")
            //          .lessThan(5));

            return row ;
        });
        BorderPane root = new BorderPane(table);
        Scene scene = new Scene(root, 600, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private List<Item> createData() {
        Random rng = new Random();
        List<Item> data = new ArrayList<>();
        for (int i=1; i<=20; i++) {
            data.add(new Item("Item "+i, rng.nextInt(10)));
        }
        return data ;
    }

    private <S,T> TableColumn<S, T> createTableColumn(String name, Function<S, ObservableValue<T>> propertyMapper) {
        TableColumn<S,T> col = new TableColumn<>(name);
        col.setCellValueFactory(cellData -> propertyMapper.apply(cellData.getValue()));
        return col ;
    }

    private TableCell<Item, Item> createDeleteCell(TableColumn<Item, Item> col) {
        ObservableList<Item> itemList = col.getTableView().getItems();
        TableCell<Item, Item> cell = new TableCell<>();
        cell.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
        Button button = new Button("Delete");
        button.setOnAction(event -> itemList.remove(cell.getItem()));
        cell.graphicProperty().bind(Bindings.when(cell.emptyProperty()).then((Node)null).otherwise(button));
        return cell ;
    }





    /**
    * Item class here
    */
    public static class Item {
        private final StringProperty name = new SimpleStringProperty(this, "name");
        private final IntegerProperty value = new SimpleIntegerProperty(this, "value");
        public final StringProperty nameProperty() {
            return this.name;
        }
        public final java.lang.String getName() {
            return this.nameProperty().get();
        }
        public final void setName(final java.lang.String name) {
            this.nameProperty().set(name);
        }
        public final IntegerProperty valueProperty() {
            return this.value;
        }
        public final int getValue() {
            return this.valueProperty().get();
        }
        public final void setValue(final int value) {
            this.valueProperty().set(value);
        }

        public Item(String name, int value) {
            setName(name);
            setValue(value);
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}
import java.util.ArrayList;
导入java.util.array;
导入java.util.List;
导入java.util.Random;
导入java.util.function.function;
导入javafx.application.application;
导入javafx.beans.binding.Bindings;
导入javafx.beans.property.IntegerProperty;
导入javafx.beans.property.ReadOnlyObjectWrapper;
导入javafx.beans.property.SimpleIntegerProperty;
导入javafx.beans.property.SimpleStringProperty;
导入javafx.beans.property.StringProperty;
导入javafx.beans.value.observeValue;
导入javafx.collections.ObservableList;
导入javafx.scene.Node;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.ContentDisplay;
导入javafx.scene.control.TableCell;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TableRow;
导入javafx.scene.control.TableView;
导入javafx.scene.layout.BorderPane;
导入javafx.stage.stage;
导入org.fxmisc.easybind.easybind;
公共类DisabledTableRowExample扩展应用程序{
@凌驾
公共无效开始(阶段primaryStage){
TableView table=新TableView();
table.getItems().addAll(createData());
TableColumn deleteCol=createTableColumn(“删除”,ReadOnlyObjectWrapper::new);
deleteCol.setCellFactory(this::createDeleteCell);
table.getColumns().addAll(Arrays.asList(
createTableColumn(“名称”,项::nameProperty),
createTableColumn(“值”,项::valueProperty),
deleteCol
));
//返回一行的行工厂,该行在
//它显示的项目的值小于5:
表.setRowFactory(电视->{
TableRow行=新TableRow();
//使用EasyBind访问单元格的itemProperty的valueProperty:
row.disableProperty().bind(
EasyBind.select(row.itemProperty())//从行的itemProperty开始
.selectObject(Item::valueProperty)//如果项非空,则映射到项的valueProperty
.map(x->x.intValue()<5)//通过值<5的intValue映射到BooleanBinding
.orElse(false));//项为null时使用的值
row.setFocusTraversable(false);//此处
row.setEditable(false);//此处
//使用标准API也可以做到这一点,但是有很多
//发送到标准输出的多余警告:
//row.disableProperty().bind(
//Bindings.selectInteger(row.itemProperty(),“value”)
//.lessThan(5));
返回行;
});
BorderPane根=新的BorderPane(表);
场景=新场景(root,600400);
初级阶段。场景(场景);
primaryStage.show();
}
私有列表createData(){
随机rng=新随机();
列表数据=新的ArrayList();
for(int i=1;i propertyMapper.apply(cellData.getValue());
返回列;
}
专用TableCell createDeleteCell(TableColumn列){
ObservableList itemList=col.getTableView().getItems();
TableCell=新的TableCell();
cell.setContentDisplay(仅限ContentDisplay.GRAPHIC_);
按钮按钮=新按钮(“删除”);
setOnAction(事件->项目列表.remove(cell.getItem());
cell.graphicProperty().bind(Bindings.when(cell.emptyProperty())。然后((节点)null)。否则(按钮));
返回单元;
}
/**
*物品类别在这里
*/
公共静态类项{
私有最终StringProperty名称=新SimpleStringProperty(此“名称”);
私有最终IntegerProperty值=新的SimpleIntegerProperty(此“值”);
公共最终字符串属性nameProperty(){
返回此.name;
}
public final java.lang.String getName(){
返回此.nameProperty().get();
}
public final void setName(final java.lang.String名称){
this.nameProperty().set(name);
}
公共最终整数属性值属性(){
返回此.value;
}
公共最终整数getValue(){
返回此.valueProperty().get();
}
公共最终无效设置值(最终整数值){
this.valueProperty().set(值);
}
公共项(字符串名称、int值){
集合名(名称);
设置值(值);
}
}
公共静态void main(字符串[]args){
发射(args);
}
}