我想从数据库中填充32列的动态tableview,第一列包含员工的名称,剩下的31列用于标记员工从第1天到第31天的注意事项(复选框)。但是我可以用数据库填充tableview使用get和set方法的列(名称,复选框)。这是我的代码
CREATE FUNCTION [dbo].[udf-Str-Parse] (@String varchar(max),@Delimiter varchar(10))
Returns Table
As
Return (
Select RetSeq = Row_Number() over (Order By (Select null))
,RetVal = LTrim(RTrim(B.i.value('(./text())[1]', 'varchar(max)')))
From (Select x = Cast('<x>'+ replace((Select @String as [*] For XML Path('')),@Delimiter,'</x><x>')+'</x>' as xml).query('.')) as A
Cross Apply x.nodes('x') AS B(i)
);
--Select * from [dbo].[udf-Str-Parse]('Dog,Cat,House,Car',',')
--Select * from [dbo].[udf-Str-Parse]('John Cappelletti was here',' ')
--Select * from [dbo].[udf-Str-Parse]('this,is,<test>,for,< & >',',')
这是 User.java
String sql ="SELECT * FROM attendence";
pst = (PreparedStatement) con.prepareStatement(sql);
rs=pst.executeQuery();
while (rs.next()) {
//get string from db,whichever way
String name=rs.getString(3);
int day=rs.getInt(6);
data.add(new User(name,day!=0)); //converting integer to boolean and storing on data(Observable list)
}
etname.setCellValueFactory(new PropertyValueFactory<>("name"));
col.setCellValueFactory(new PropertyValueFactory<>("day1));
col.setCellFactory(new Callback<TableColumn<User, Boolean>, TableCell<User, Boolean>>() {
public TableCell<User, Boolean> call(TableColumn<User, Boolean> p) {
return new CheckBoxTableCell<User, Boolean>();
}
});
jTable.getColumns().add(etname,col);
jTable.setItems(data);
}
CheckBoxTableCell.java
public class User {
private final SimpleStringProperty ename;
private BooleanProperty day1;
User(String Ename,boolean day1)
{
this.ename = new SimpleStringProperty(Ename);
this.day1 = new SimpleBooleanProperty(day1);
this.day1.addListener(new ChangeListener<Boolean>() {
public void changed(ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) {
System.out.println(enameProperty().get() + " invited: " + t1);
System.out.println();
}
});
}
public String getEname() {
return ename.get();
}
public void setEname(String Ename) {
ename.set(Ename);
}
public BooleanProperty day1Property() {
return day1;
}
public StringProperty enameProperty() {
return ename;
}
但不知道我如何处理32列。 因此,对于我的两个问题,我需要任何人的大力帮助。 1)如何在数据库中填充动态tableview,复选框 2)当我按下按钮时,应该读取所有名称以及复选框状态(isSelected或not selected),如< / p>
玫瑰假假真......
george true true true false
答案将不胜感激。谢谢你提前。 .. ..
答案 0 :(得分:0)
尝试使用此代码。
private static final List<String> groups = Arrays.asList("Group 1", "Group 2", "Group 3", "Group 4"); //declared an array
TableView<AttributeRow> attributeTable = new TableView<>(); //new Table
for (String group : groups) { //Creating dynamic column
TableColumn<AttributeRow, Boolean> groupColumn = new TableColumn<>(group);
groupColumn.setCellFactory(CheckBoxTableCell.forTableColumn(groupColumn));
groupColumn.setCellValueFactory(cellData -> cellData.getValue().activeProperty(group));
attributeTable.getColumns().add(groupColumn);
}
和AttributeRow类
class AttributeRow {
private final Map<String, BooleanProperty> activeByGroup = new HashMap<>();
public AttributeRow(List<String> companyGroups) {
for (String group : companyGroups) {
activeByGroup.put(group, new SimpleBooleanProperty()) ;
}
}
public final BooleanProperty activeProperty(String group) {
return activeByGroup.get(group) ;
}
public final boolean isActive(String group) {
return activeProperty(group).get();
}
public final void setActive(String group, boolean active) {
activeProperty(group).set(active);
}
对于加载复选框数据(true或false),我们可以使用以下方法
String sql ="SELECT * FROM attendence";
pst = (PreparedStatement) con.prepareStatement(sql);
rs=pst.executeQuery();
while (rs.next()) {
AttributeRow row = new AttributeRow(groups);
for(int j=0;j<5;j++) //here j<5 because array size =5
{
int i=j+6;
int day=rs.getInt(i); //getting Integer value(note:sqlite does not support boolean datatype so i stored as integer with 0 or 1)
row.setActive(day!=0); //converted into boolean useing day!=0
}
jTable.getItems().add(row);
}