无法在 javaFX 上选择 CheckBoxTableCell
CheckBoxTableCell Can't be selected on javaFX
JavaFx 的新手我创建了一个视图 table 使用 Scene builder 和一个名为 checkedCol 的列 checkedCol 它应该包含 chekboxes ,在我的控制器上我以这种方式创建这些复选框:
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Callback;
public class RecapController implements Initializable{
@FXML
private TableView<OMission> missionTable;
@FXML
private TableColumn<OMission, Boolean> checkedCol;
private ObservableList<OMission> UserMission =
FXCollections.observableArrayList();
private OMission mission=null;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
try {
load_recap();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void load_recap(){
.....
checkedCol.setCellFactory(
CheckBoxTableCell.forTableColumn(checkedCol)
);
checkedCol.setCellValueFactory(new PropertyValueFactory<>("remark"));
checkedCol.setEditable(true);
}
}
在 class 模型上我有这个代码:
public class OMission {
private String ObjMission ;
private Boolean remark;
public OMission( String objMission ) {
This.objMission = ObjMission ;
remark = false;
}
public Boolean getRemark() {
return remark;
}
public void setRemark(Boolean remark) {
this.remark = remark;
}
public String getObjMission() {
return ObjMission;
}
public void setObjMission(String objMission) {
ObjMission = objMission;
}
}
fxml代码:
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane fx:id="pan" prefHeight="740.0" prefWidth="1258.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.RecapController">
<children>
<JFXButton layoutX="16.0" layoutY="22.0" onAction="#OpenMission" style="-fx-background-color: #0A4969;" text="Ajouter une nouvelle mission : " textFill="WHITE">
<font>
<Font size="14.0" />
</font>
</JFXButton>
<TableView fx:id="missionTable" layoutX="16.0" layoutY="64.0" prefHeight="659.0" prefWidth="1226.0">
<columns>
<TableColumn prefWidth="612.5" text="RAPPORT DE MISSION">
<columns>
<TableColumn fx:id="local" prefWidth="100.0" text="Localité" />
<TableColumn fx:id="objmission" prefWidth="243.0" text="Objet Mission" />
</columns>
</TableColumn>
<TableColumn fx:id="checkedCol" prefWidth="75.0" text="select" />
</columns>
</TableView>
<JFXButton layoutX="263.0" layoutY="25.0" onAction="#print_tab" text="print" />
<CheckBox fx:id="SelectAll" layoutX="1154.0" layoutY="41.0" mnemonicParsing="false" text="Select All" />
</children>
问题是我没有得到任何错误,但我什至无法 select/check 我在视图中看到的复选框 table,我无法弄清楚问题 :) 任何想法?提前致谢。
有什么想法吗?
从您的代码中不清楚 Tableview
是否可编辑。假设 TableView
是 missionTable
,试试:
missionTable.setEditable(true);
或者,您可以通过添加 editable="true"
:
在 FXML 中设置 Tableview
可编辑
<TableView fx:id="missionTable" editable="true" layoutX="16.0" layoutY="64.0" prefHeight="659.0" prefWidth="1226.0">
JavaFx 的新手我创建了一个视图 table 使用 Scene builder 和一个名为 checkedCol 的列 checkedCol 它应该包含 chekboxes ,在我的控制器上我以这种方式创建这些复选框:
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Callback;
public class RecapController implements Initializable{
@FXML
private TableView<OMission> missionTable;
@FXML
private TableColumn<OMission, Boolean> checkedCol;
private ObservableList<OMission> UserMission =
FXCollections.observableArrayList();
private OMission mission=null;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
try {
load_recap();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void load_recap(){
.....
checkedCol.setCellFactory(
CheckBoxTableCell.forTableColumn(checkedCol)
);
checkedCol.setCellValueFactory(new PropertyValueFactory<>("remark"));
checkedCol.setEditable(true);
}
}
在 class 模型上我有这个代码:
public class OMission {
private String ObjMission ;
private Boolean remark;
public OMission( String objMission ) {
This.objMission = ObjMission ;
remark = false;
}
public Boolean getRemark() {
return remark;
}
public void setRemark(Boolean remark) {
this.remark = remark;
}
public String getObjMission() {
return ObjMission;
}
public void setObjMission(String objMission) {
ObjMission = objMission;
}
}
fxml代码:
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane fx:id="pan" prefHeight="740.0" prefWidth="1258.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.RecapController">
<children>
<JFXButton layoutX="16.0" layoutY="22.0" onAction="#OpenMission" style="-fx-background-color: #0A4969;" text="Ajouter une nouvelle mission : " textFill="WHITE">
<font>
<Font size="14.0" />
</font>
</JFXButton>
<TableView fx:id="missionTable" layoutX="16.0" layoutY="64.0" prefHeight="659.0" prefWidth="1226.0">
<columns>
<TableColumn prefWidth="612.5" text="RAPPORT DE MISSION">
<columns>
<TableColumn fx:id="local" prefWidth="100.0" text="Localité" />
<TableColumn fx:id="objmission" prefWidth="243.0" text="Objet Mission" />
</columns>
</TableColumn>
<TableColumn fx:id="checkedCol" prefWidth="75.0" text="select" />
</columns>
</TableView>
<JFXButton layoutX="263.0" layoutY="25.0" onAction="#print_tab" text="print" />
<CheckBox fx:id="SelectAll" layoutX="1154.0" layoutY="41.0" mnemonicParsing="false" text="Select All" />
</children>
问题是我没有得到任何错误,但我什至无法 select/check 我在视图中看到的复选框 table,我无法弄清楚问题 :) 任何想法?提前致谢。
有什么想法吗?
从您的代码中不清楚 Tableview
是否可编辑。假设 TableView
是 missionTable
,试试:
missionTable.setEditable(true);
或者,您可以通过添加 editable="true"
:
Tableview
可编辑
<TableView fx:id="missionTable" editable="true" layoutX="16.0" layoutY="64.0" prefHeight="659.0" prefWidth="1226.0">