问题
I just started with javafx and I wanted to create a TableView
with 3 columns where I can display some values.
I created the TableView
and the columns with the scene editor as fxml file.
Then I created a class named Values
with the special properties I matched to the columns where they should fit in.
Finally I set the observable list with the "Value" objects in it as items of the table. When I start the application, it only shows me an empty table.
I looked like 4 hours in the internet now and still not found an answer why this is not working for me.
Here my code:
Value Class:
public class Values {
public SimpleDoubleProperty PSI = new SimpleDoubleProperty(0);
public SimpleDoubleProperty ALPHA = new SimpleDoubleProperty(0);
public SimpleDoubleProperty DELTA = new SimpleDoubleProperty(0);
public Values(Double _PSI, Double _ALPHA, Double _DELTA) {
setPSI(_PSI);
setALPHA(_ALPHA);
setDELTA(_DELTA);
}
private void setPSI(Double p){
PSI.set(p);
}
private void setALPHA(Double p){
ALPHA.set(p);
}
private void setDELTA(Double p){
DELTA.set(p);
}
}
Controller:
@FXML Label psi;
@FXML Label alpha;
@FXML Label delta;
@FXML TextField betafield;
@FXML TextField lambdafield;
@FXML TextField lambdasatfield;
@FXML TableView<Values> table;
@FXML ObservableList<Values> oblist;
@FXML TableColumn <Values,Double> psicolumn;
@FXML TableColumn <Values,Double> alphacolumn;
@FXML TableColumn <Values,Double> deltacolumn;
@Override
public void initialize(URL location, ResourceBundle resources) {
psicolumn.setCellValueFactory(new PropertyValueFactory<Values, Double>("PSI"));
alphacolumn.setCellValueFactory(new PropertyValueFactory<Values, Double>("ALPHA"));
deltacolumn.setCellValueFactory(new PropertyValueFactory<Values, Double>("DELTA"));
}
@FXML
protected void buttonpressed(){
try {
Calculation cal = new Calculation(Double.parseDouble(betafield.getText()), Double.parseDouble(lambdafield.getText()), Double.parseDouble(lambdasatfield.getText()));
alpha.setText("Alpha: " + " " + cal.calculateAlpha());
delta.setText("Delta:"+ " " + cal.calculateDelta());
psi.setText("Psi:"+ " " + cal.calculatePSI());
table.setItems(FXCollections.observableArrayList(cal.calculateEvaluaiontable()));
}catch (NullPointerException e){
e.printStackTrace();
}
}
And my FXML:
<Tab text="tab" fx:id="tabe">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<TableView layoutX="4.0" layoutY="4.0" prefHeight="192.0" prefWidth="370.0" fx:id="table">
<columns>
<TableColumn prefWidth="120.0" text="PSI" fx:id="psicolumn" />
<TableColumn prefWidth="120.0" text="ALPHA" fx:id="alphacolumn" />
<TableColumn prefWidth="120.0" text="DELTA" fx:id="deltacolumn" />
</columns>
</TableView>
</children>
</AnchorPane>
</content>
</Tab>
Thanks for your help!
回答1:
Let <name>
denote the constructor parameter of PropertyValueFactory and let <Name>
denote the same String
, but with an uppercase first letter.
PropertyValueFactory
can get the value from one of the following sources:
- The property getter, i.e. the a method
SomeObservableValue <name>Property()
. - The getter method, i.e. the method
SomeType get<Name>()
.
None of those exist in your Values
class.
For the psicolumn
to work, Values
needs a DoubleProperty PSIProperty()
method or a double getPSI()
method. (Same issue with the other columns)
来源:https://stackoverflow.com/questions/37810176/javafx-fill-a-table-view-not-possible