问题
A relative Java newbie question.
I'm cleaning up my code and came across two unchecked warnings ("unchecked method invocation" and "unchecked conversion") while programmatically selecting a cell in a TableView
. I was able to resolve the warnings but don't understand what caused them.
This was how I initially coded the select()
:
TablePosition<?, ?> previousPos;
//...
table.getSelectionModel().select
(previousPos.getRow(), previousPos.getTableColumn());
and I resolved the warnings by changing it to this.
table.getSelectionModel().select
(previousPos.getRow(), table.getColumns().get(previousPos.getColumn()));
I didn't understand the difference so I had a look at the Java source code. If I'm interpreting it correctly, the select()
method in TableView.java expects TableColumn<S, ?>
. However, getTableColumn()
in TablePosition.java returns TableColumn<S,T>
while getColumns()
in TableView.java returns an ObservableList
of type TableColumn(S, ?)
.
I guess that's why table.getColumns().get(...)
compiles clean and previousPos.getTableColumn()
generates errors.
But what is the difference, then, between TableColumn<S, ?>
and TableColumn<S,T>
from the compiler's point of view? Why doesn't it resolve (is that the correct term?) the T
to ?
.
If it helps, here is the MVCE I was playing with to try and figure it out, but the answer is beyond my current Java knowledge. The select()
is in moveToPreviousPos()
.
I'm using JavaFX8 (JDK1.8.0_181), NetBeans 8.2 and Scene Builder 8.3.
package test27;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TablePosition;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Test27 extends Application {
TableView<TestModel> table = new TableView<>();
//Both of these stop the "unchecked conversion" error on line #51 (previousPos = table.getFocusModel().getFocusedCell();)
// TablePosition previousPos;
TablePosition<?, ?> previousPos;
//but using the following generates the "unchecked conversion" error
// TablePosition<TestModel, ?> previousPos;
private Parent createContent() {
ObservableList<TestModel> olTestModel = FXCollections.observableArrayList();
olTestModel.add(new TestModel("A"));
olTestModel.add(new TestModel("B"));
olTestModel.add(new TestModel("C"));
table.setItems(olTestModel);
TableColumn<TestModel, String> colText = new TableColumn<>("text");
colText.setCellValueFactory(cellData -> cellData.getValue().textProperty());
colText.setCellFactory(TextFieldTableCell.forTableColumn());
table.getSelectionModel().setCellSelectionEnabled(true);
table.setEditable(true);
table.getColumns().add(colText);
Button btnGetTablePosition = new Button("get table position");
btnGetTablePosition.setOnAction(event -> {
//TableView.java: getFocusedCell returns TablePosition<S, ?>
previousPos = table.getFocusModel().getFocusedCell(); //Line #51
outputPreviousPos(previousPos);
});
Button btnMoveToPreviousPos = new Button("move to previous pos");
btnMoveToPreviousPos.setOnAction(event -> {
moveToPreviousPos(previousPos);
});
BorderPane content = new BorderPane(table);
HBox hb = new HBox();
hb.getChildren().addAll(btnGetTablePosition, btnMoveToPreviousPos);
content.setTop(hb);
return content;
}
public void outputPreviousPos(TablePosition previousPos){
System.out.println("previousPos = " + previousPos);
}
public void moveToPreviousPos(TablePosition previousPos) {
//select() in TableView.java expects TableColumn<S, ?>
//getTableColumn() in TablePosition.java returns TableColumn<S,T>
//getColumns() in TableView.java returns an ObservableList of type TableColumn(S, ?)
//Is that why the following line generates "unchecked method invocation" and "unchecked conversion" errors
//table.getSelectionModel().select
// (previousPos.getRow(), previousPos.getTableColumn());
//but the following line compiles clean?
table.getSelectionModel().select
(previousPos.getRow(), table.getColumns().get(previousPos.getColumn()));
}
public class TestModel {
private StringProperty text;
public TestModel() {
this("");
}
public TestModel(
String text
) {
this.text = new SimpleStringProperty(text);
}
public String getText() {
return text.get();
}
public void setText(String text) {
this.text.set(text);
}
public StringProperty textProperty() {
return text;
}
}
@Override
public void start(Stage stage) throws Exception {
stage.setScene(new Scene(createContent()));
stage.setTitle("Test");
stage.setWidth(500);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
回答1:
First of all, I would say that it was a design flaw that TableView.getFocusModel().getFocusedCell()
(or focusedCellProperty()
) uses the raw TablePosition
type (raw TablePosition
can only be cast to TablePositon<?, ?>
without generating warning). The more suitable version could have been TablePosition<S, ?>
.
Since TablePosition
represents a particular cell anywhere in the TableView
, then for TableView.getFocusModel().focusedCellProperty()
:
- The type of
S
is fixed forTableView<S>
(i.e. the type is known). - The type of
T
refers to the type of a particularTableColumn
, which can change depending on which cell has been focused. You cannot determine the type at compile time because you have no idea whichTableColumn<S, T>
would be selected.
Now to the second point.
But what is the difference, then, between
TableColumn<S, ?>
andTableColumn<S,T>
from the compiler's point of view? Why doesn't it resolve (is that the correct term?) theT
to?
.
This is because ?
in TableColumn<S, ?>
is a wildcard. Since TableViewFocusModel<S>
does not have the type T
and that it does not operate based on a fixed column, so it has to use ?
here (see the first point that I explained).
On the other hand, TableView.getSelectionModel().select()
expects an int
for row
and a TableColumn<S, ?>
for column. Since you had TablePosition<?, ?> previousPos
, so previousPos
would return a TableColumn<?, ?>
as well. TableColumn<S, ?
and TableColumn<?, ?>
are very different - you could use the TableColumn
from another TableView
if TableColumn<?, ?>
is allowed.
Finally, if the API for TableView.getFocusModel().getFocusedCell()
is fixed, then there would not be a problem when you use select()
. Until then, you could manually cast it to TablePosition<TestModel, ?>
with a suppression, or use TableView.getColumns().get(index)
like what you did.
来源:https://stackoverflow.com/questions/52693823/why-does-the-compiler-generate-unchecked-warnings-when-using-tablecolumns-t-r