JavaFX填充表视图是不可能的 [英] JavaFX fill a table view not possible

查看:161
本文介绍了JavaFX填充表视图是不可能的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用javafx,我想用3列创建一个 TableView ,我可以在其中显示一些值。
我创建了 TableView 以及带有场景编辑器的列作为fxml文件。
然后我创建了一个名为 Values 的类,其中包含我们应该适合的列的特殊属性。
最后我设置了observable列表其中的Value对象作为表的项目。当我启动应用程序时,它只显示一个空表。
我现在在互联网上看起来好像只有4个小时,仍然没有找到答案为什么这对我不起作用。

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.

这里我的代码:

价值等级:

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);
    }

}

控制器:

@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();
    }
}

我的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>

感谢您的帮助!

推荐答案

< name> 表示 PropertyValueFactory 并让< ;名称> 表示相同的字符串,但使用大写的第一个字母。

Let <name> denote the constructor parameter of PropertyValueFactory and let <Name> denote the same String, but with an uppercase first letter.

PropertyValueFactory 可以从以下某个来源获取值:

PropertyValueFactory can get the value from one of the following sources:


  1. 属性getter ,即a方法 SomeObservableValue< name> Property()

  2. getter方法,即方法 SomeType get< Name>()

  1. The property getter, i.e. the a method SomeObservableValue <name>Property().
  2. The getter method, i.e. the method SomeType get<Name>().

值 class。

要使用 psicolumn 需要 DoubleProperty PSIProperty()方法或 double ge tPSI()方法。 (与其他列相同的问题)

For the psicolumn to work, Values needs a DoubleProperty PSIProperty() method or a double getPSI() method. (Same issue with the other columns)

这篇关于JavaFX填充表视图是不可能的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆