JavaFX 表格 View 不显示按钮

标签 java javafx javafx-2 tableview javafx-8

我试图关注this example在桌面 View 上。

这是SSCCE :

摘要.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.control.cell.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.shape.*?>

<fx:root maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" type="javafx.scene.layout.AnchorPane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
    <children>
        <TableView fx:id="table" layoutY="50.0" prefHeight="350.0" prefWidth="600.0">
        <columns>
            <TableColumn prefWidth="79.5" text="Date">
                <cellValueFactory><PropertyValueFactory property="date" />
                </cellValueFactory>
            </TableColumn>
            <TableColumn prefWidth="78" text="Label">
                <cellValueFactory><PropertyValueFactory property="label" />
                </cellValueFactory>
            </TableColumn>
            <TableColumn prefWidth="81" text="NumSlices">
                <cellValueFactory><PropertyValueFactory property="numSlices" />
                </cellValueFactory>
            </TableColumn>
        </columns>
        </TableView>
    </children>
</fx:root>

这是类(class):

摘要.java

package sum;

import java.io.IOException;
import java.net.URL;
import java.util.Comparator;
import java.util.ResourceBundle;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.AnchorPane;
import javafx.util.Callback;

public class Summary extends AnchorPane implements Initializable {
    @FXML
    private TableView<SummaryElement> table;
    //ObservableList<SummaryElement> data = FXCollections.observableArrayList(); 
    public Summary() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(
                "/res/summary.fxml"));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);
        try {
            fxmlLoader.load();
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        ObservableList<SummaryElement> data = table.getItems();
        data.add(new SummaryElement("2009/12/12", "T1", 23));
        data.add(new SummaryElement("2006/12/12", "T1", 2));
        table.getItems().add(new SummaryElement("2011/12/12", "T2",23));
          TableColumn<SummaryElement,SummaryElement> btnCol = new TableColumn<>("btnCol");
          btnCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<SummaryElement,SummaryElement>,ObservableValue<SummaryElement>>(){

            @SuppressWarnings("rawtypes")
            @Override
            public ObservableValue<SummaryElement> call(
                    javafx.scene.control.TableColumn.CellDataFeatures<SummaryElement, SummaryElement> features) {
                    return new ReadOnlyObjectWrapper(features.getValue());
            }

          });
          btnCol.setComparator(new Comparator<SummaryElement>() {
            @Override public int compare(SummaryElement p1, SummaryElement p2) {
              return p1.getLabel().compareTo(p2.getLabel());
            }
          });
          btnCol.setCellFactory(new Callback<TableColumn<SummaryElement, SummaryElement>, TableCell<SummaryElement, SummaryElement>>() {
            @Override public TableCell<SummaryElement, SummaryElement> call(TableColumn<SummaryElement, SummaryElement> btnCol) {
              return new TableCell<SummaryElement, SummaryElement>() {
                final Button button = new Button(); {
                  button.setMinWidth(130);
                }
                @Override public void updateItem(final SummaryElement SummaryElement, boolean empty) {
                  super.updateItem(SummaryElement, empty);
                  if (SummaryElement != null) {
                    button.setText("Buy coffee" + SummaryElement.getLabel());
                    button.setOnAction(new EventHandler<ActionEvent>() {
                      @Override public void handle(ActionEvent event) {
                       // actionTaken.setText("Bought " + SummaryElement.getLikes().toLowerCase() + " for: " + SummaryElement.getFirstName() + " " + SummaryElement.getLastName());
                      }
                    });
                  } else {
                    setGraphic(null);
                  }
                }
              };
            }
          });
          table.getColumns().add(btnCol);

    }
}

以及一个调用应用程序的类:

package sum;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;



public class CustomDemo extends Application {
        @Override
        public void start(Stage stage) throws Exception {
            Summary custom = new Summary();

            Scene scene = new Scene(custom);
            stage.setScene(scene);
            stage.show();
        }
        /**
         * The main() method is ignored in correctly deployed JavaFX application.
         * main() serves only as fallback in case the application can not be
         * launched through deployment artifacts, e.g., in IDEs with limited FX
         * support. NetBeans ignores main().
         *
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            launch(args);
        }
}

summaryElement 就是这样:

package sum;

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
public class SummaryElement {
    private final SimpleStringProperty date = new SimpleStringProperty("");
    private final SimpleStringProperty label = new SimpleStringProperty("");
    private final IntegerProperty numSlices = new SimpleIntegerProperty(this, "numSlices", 0);



    public SummaryElement(String date, String label, int numSlices){
           setDate(date);
           setLabel(label);
           setNumSlices(numSlices);


    }
    public void setDate(String gDate){
        date.set(gDate);
    }
    public String getDate(){
        return date.get();
    }
    public void setLabel(String gLabel){
        label.set(gLabel);
    }
    public String getLabel(){
        return label.getValue();
    }
    public void setNumSlices(int gNumSlices){
        numSlices.set(gNumSlices);
    }
    public int getNumSlices(){
        return numSlices.get();
    }

}

当我在 Eclipse 中进行调试时,在 Summary.java 中的第 77 行设置断点,我可以在“变量”窗口中看到它加载了 SummaryElement,但是当我单击进入下一步时,它会显示错误消息:“未找到源”。

然后它会在 btnCol 上显示没有任何按钮的 tableview,即使它进入 if (SummaryElement != null) 几次。

如果这篇文章看起来像是一篇帮助吸血鬼的文章,我很抱歉,但我尝试了 3 天,但没有成功。

最佳答案

TableCellupdateItem(...) 方法中,在 if (SummaryElement != null) 子句中,您需要setGraphic(按钮);

关于JavaFX 表格 View 不显示按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24087085/

相关文章:

java - 如何在 FXML (JavaFX) 中为按钮定义列

java - 为什么 HBase 中的 Bytes.toBytes() 无法返回我在我的情况下搜索的最后一行?

java - 如何在java代码中达到所需的点?

java - 如何使用另一个组合框中的选择来填充组合框? JavaFX

java - 从另一个窗口javafx设置TextField的值

javafx - 在CellFactory中有条件地提供不同的TreeCell

java - int 对象到整数转换的问题

java - Android 循环任务在后台运行

JavaFX 矩形转圆形动画

java - 当 JavaFX 中选定的选项卡更改时如何聚焦特定节点?