[英]How to set and get the cell value of JavaFX Table like swing JTable
[英]How to Get First Column Value on click in JavaFX TableView like JTable in swing?
我想获取我们可以使用Swing在Jtable中实现的第一列值。 下面是我的jtable代码和图像。
String Table_Clicked = jTable1.getModel().getValueAt(row, 0).toString();
正如您在图像中看到的那样,当我单击“ 名称”列值八时,它给了我第一列值,如8 。 但我选择“ 名称列”
因此,如何使用TableView Componenet在JavaFX中实现此目标。
我从TableView中获得了选定的Value,如下面的代码所示。
tableview.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue observableValue, Object oldValue, Object newValue) {
if(tableview.getSelectionModel().getSelectedItem() != null)
{
TableViewSelectionModel selectionModel = tableview.getSelectionModel();
ObservableList selectedCells = selectionModel.getSelectedCells();
TablePosition tablePosition = (TablePosition) selectedCells.get(0);
Object val = tablePosition.getTableColumn().getCellData(newValue);
System.out.println("Selected value IS :" + val);
}
}
});
因此,我希望在Tableview第一列数据中获得与在Jtable中获得的相同? 所以如何获得该NO值..使用上面的代码,我获得了选定的Cell的值,该值在控制台中为8 。.但我想获得First Column Value。帮助我继续前进。
谢谢..
TABLEVIEW的数据填充代码的更新
PreparedStatement psd = (PreparedStatement) conn.prepareStatement("SELECT No,name FROM FieldMaster");
psd.execute();
ResultSet rs = psd.getResultSet();
for(int i=0 ; i<rs.getMetaData().getColumnCount(); i++){
//We are using non property style for making dynamic table
final int j = i;
namecol = new TableColumn(rs.getMetaData().getColumnName(i+1));
namecol.setCellValueFactory(new Callback<CellDataFeatures<ObservableList, String>, ObservableValue<String>>()
{
@Override
public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param)
{
return new SimpleStringProperty(param.getValue().get(j).toString());
}
});
tableview.getColumns().addAll(namecol);
System.out.println("Column ["+i+"] ");
}
while(rs.next())
{
//Iterate Row
ObservableList<String> row = FXCollections.observableArrayList();
for(int i=1 ; i<=rs.getMetaData().getColumnCount(); i++)
{
//Iterate Column
row.add(rs.getString(i));
}
System.out.println("Row [1] added "+row );
data.add(row);
}
tableview.setItems(data);
conn.close();
解
您可以通过在与所选行相对应的模型对象上调用getter来检索相关字段。
在newValue.getId()
下面的代码中,调用是关键。
没有Java 8 Lambda:
final Label selected = new Label();
table.getSelectionModel().selectedItemProperty().addListener(
new ChangeListener<IdentifiedName>() {
@Override
public void changed(
ObservableValue<? extends IdentifiedName> observable,
IdentifiedName oldValue,
IdentifiedName newValue
) {
if (newValue == null) {
selected.setText("");
return;
}
selected.setText("Selected Number: " + newValue.getId());
}
}
);
使用Java 8 Lambda:
final Label selected = new Label();
table.getSelectionModel().selectedItemProperty().addListener(
(observable, oldValue, newValue) -> {
if (newValue == null) {
selected.setText("");
return;
}
selected.setText("Selected Number: " + newValue.getId());
}
);
样例代码
import javafx.application.Application;
import javafx.collections.*;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.*;
import javafx.stage.Stage;
public class TableViewSample extends Application {
private TableView<IdentifiedName> table = new TableView<>();
private final ObservableList<IdentifiedName> data =
FXCollections.observableArrayList(
new IdentifiedName(3, "three"),
new IdentifiedName(4, "four"),
new IdentifiedName(7, "seven"),
new IdentifiedName(8, "eight"),
new IdentifiedName(9, "nineses")
);
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
TableColumn<IdentifiedName, Integer> idColumn = new TableColumn<>("No");
idColumn.setMinWidth(100);
idColumn.setCellValueFactory(
new PropertyValueFactory<>("id")
);
TableColumn<IdentifiedName, String> nameColumn = new TableColumn<>("Name");
nameColumn.setMinWidth(100);
nameColumn.setCellValueFactory(
new PropertyValueFactory<>("name")
);
table.setItems(data);
table.getColumns().setAll(idColumn, nameColumn);
table.setPrefHeight(180);
final Label selected = new Label();
table.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
if (newValue == null) {
selected.setText("");
return;
}
selected.setText("Selected Number: " + newValue.getId());
});
final VBox layout = new VBox(10);
layout.setPadding(new Insets(10));
layout.getChildren().addAll(table, selected);
VBox.setVgrow(table, Priority.ALWAYS);
stage.setScene(new Scene(layout));
stage.show();
}
public static class IdentifiedName {
private final int id;
private final String name;
private IdentifiedName(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}
}
回答其他问题
检查我更新的问题,以便我不能使用它?
因此,在您的更新中,您可以看到每行数据的类型是ObservableList<String>
,而在我的回答中,类型是IdentifiedName
。 为了使发布的解决方案适用于您的数据类型,更改是微不足道的。 newValue.getId()
的等效项是newValue.get(0)
,以返回列表中所选行的第一项。
final Label selected = new Label();
table.getSelectionModel().selectedItemProperty().addListener(
(observable, oldValue, newValue) -> {
if (newValue == null) {
selected.setText("");
return;
}
selected.setText("Selected Number: " + newValue.get(0));
}
);
还是可以使用identifiedname类? 那怎么办
是的,可以,但是您必须对数据库的获取代码进行大量更改才能将创建的数据加载到IdentifiedName
类而不是ObservableList<String>
并且这样做会丢失数据库加载代码的通用性。
我将您的代码实现到我的项目中……
java.lang.ClassCastException:
您需要为数据类型正确设置表和列的类型,而不是我为用例提供的示例类型。
替换以下类型:
TableView<IdentifiedName>
TableColumn<IdentifiedName, Integer>
使用这些类型:
TableView<ObservableList<String>>
TableColumn<ObservableList<String>, String>
小建议
我建议您通过阅读Java Generics Trail来复习。 JavaFX中的表在使用泛型时非常复杂,但是在表代码中使用正确的泛型可以使编写起来更容易(只要您使用的是良好的IDE,它可以在需要时猜测泛型)。
您可能还想提供一个最小,完整,经过测试且易于理解的示例,其中包含此类将来的问题(并非所有问题)。 构建一个可以帮助您更快地解决问题。 同样,确保代码具有一致的缩进,使其更易于阅读。
您必须看一下底层的ObservableList
。 对我有用的代码是:
tableView.getItems().get(tableView.getSelectionModel().getSelectedIndex())
在我的测试中,它返回一个Person
对象(我写的POJO),其中必须包含get()
方法。
我可以使用以下代码获取第一列值:
tableview.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue observableValue, Object oldValue, Object newValue) {
//Check whether item is selected and set value of selected item to Label
if (tableview.getSelectionModel().getSelectedItem() != null) {
TableView.TableViewSelectionModel selectionModel = tableview.getSelectionModel();
ObservableList selectedCells = selectionModel.getSelectedCells();
TablePosition tablePosition = (TablePosition) selectedCells.get(0);
tablePosition.getTableView().getSelectionModel().getTableView().getId();
//gives you selected cell value..
Object GetSinglevalue = tablePosition.getTableColumn().getCellData(newValue);
getbothvalue = tableview.getSelectionModel().getSelectedItem().toString();
//gives you first column value..
Finalvaluetablerow = getbothvalue.toString().split(",")[0].substring(1);
System.out.println("The First column value of row.." + Finalvaluetablerow);
}
}
});
谢谢..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.