使用MVC在自定义列表视图中的按钮上添加侦听器

时间:2015-07-06 10:06:14

标签: listview javafx custom-controls

我创建的自定义控件属于我的类型list view" KMLFile ",每个单元格都包含{ {1}},Hbox和2 Label的{​​1}}。

这是我如何做的代码:

定义CheckBoxButton

的类
HBox

我的CellKML是在经典控件/皮肤类中创建的:

public class CellKML {

    private KMLFile myKMLFile;
    private HBox cellHBox;
    private Label cellFileName;
    private CheckBox cellFileCheckBox;
    private Button cellFileRefresh;
    private Button cellFileClose;

    public CellKML(KMLFile kmlFile){
        this.myKMLFile = kmlFile;
    }

    public HBox createCell(){
        cellHBox = new HBox();
        cellFileName = new Label(myKMLFile.getFileName());
        cellFileCheckBox = new CheckBox();
        cellFileCheckBox.setSelected(true);
        cellFileRefresh = new Button("R");
        cellFileClose = new Button("X");

        cellHBox.getChildren().add(cellFileName);
        cellHBox.getChildren().add(cellFileCheckBox);
        cellHBox.getChildren().add(cellFileRefresh);
        cellHBox.getChildren().add(cellFileClose);



        return cellHBox;
    }

我的目标是在Custom ListView和2 public class FileViewGridSkin extends SkinBase<FileViewGrid>{ private FileViewGrid control; private ListView<KMLFile> listView; private boolean invalidListView = true; public FileViewGridSkin(FileViewGrid control) { super(control); this.control = control; } public void createRootNode(){ if(listView != null){ getChildren().remove(listView); } listView = new ListView<KMLFile>(); listView.setCellFactory(new Callback<ListView<KMLFile>, ListCell<KMLFile>>(){ @Override public ListCell<KMLFile> call(ListView<KMLFile> p) { ListCell<KMLFile> cell = new ListCell<KMLFile>(){ @Override protected void updateItem(KMLFile kmlFile, boolean bln) { super.updateItem(kmlFile, bln); if (isEmpty()) { setText(null); setGraphic(null); } else { if (kmlFile != null) { CellKML cellKML = new CellKML(kmlFile); setGraphic(cellKML.createCell()); } } } }; return cell; } }); listView.itemsProperty().bind(control.getList()); //Here bind the listview items to your control list getChildren().add(listView); } @Override protected void layoutChildren(double contentX, double contentY, double contentWidth, double contentHeight) { if(invalidListView){ createRootNode(); invalidListView = false; } layoutInArea(listView, contentX, contentY, contentWidth, contentHeight, -1,new Insets(1.0), HPos.CENTER, VPos.CENTER); } public ListView<KMLFile> getListView() { return listView; } 添加一个监听器,它将与&#34; CheckBox&#34; 。 我可以在课堂Button 中轻松完成,但我在 MVC模式中,我无法控制我的< EM>查看

所以我设置了一个 Controller ,可以访问我的KMLFile,但我不知道如何访问我的CheckBox 和Button,用于设置监听器。

0 个答案:

没有答案