首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用controlsfx包含GridPane列表

使用controlsfx包含GridPane列表
EN

Stack Overflow用户
提问于 2016-01-03 05:26:41
回答 1查看 4.1K关注 0票数 3

我正在学习JavaFx,并构建了一个示例应用程序,它可以以滚动的方式显示餐厅菜单项列表。我想出了最好的方法,就是使用controlsfx中的GridView控件,因为即使有了大量的菜单项,滚动也会工作得很快。下面是我正在努力使其工作的示例代码:

代码语言:javascript
复制
public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            VBox root = new VBox();
            Scene scene = new Scene(root,400,400);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);

            ObservableList<GridPane> list = FXCollections.<GridPane>observableArrayList();
            GridView<GridPane> gridView = new GridView<>(list);

            gridView.setCellFactory(new Callback<GridView<GridPane>, GridCell<GridPane>>() {
                 public GridCell<GridPane> call(GridView<GridPane> gridView) {
                     return new GridCell<GridPane>();
                 }
             });

            Label lblName1 = new Label("Name");
            GridPane grid = new GridPane();
            grid.addRow(0, lblName1);

            Label lblName2 = new Label("Price");
            grid.addRow(1, lblName2);

            list.add(grid);

            root.getChildren().add(gridView);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

当我运行上面的代码时,它只显示没有项的VBox。我在使用GridView (http://controlsfx.bitbucket.org/org/controlsfx/control/GridView.html)时尝试复制controlsfx网站上给出的示例代码。

任何帮助/建议都将不胜感激。谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-04 08:10:13

找出问题,这是正确的代码。我需要编写自定义GridPane (用于dsplaying菜单项)和单元工厂来生成自定义对象。

代码语言:javascript
复制
//Main Class
package application;

import java.util.Random;

import org.controlsfx.control.GridCell;
import org.controlsfx.control.GridView;
import org.controlsfx.control.cell.ColorGridCell;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Orientation;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.util.Callback;


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            VBox root = new VBox();
            Scene scene = new Scene(root,400,400);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);

            GridView<MenuItem> menuItems = new GridView<>();

            for(int i = 0; i < 10000; i++) {
                menuItems.getItems().addAll(new MenuItem(i));
            }

            menuItems.setCellFactory(new MenuItemCellFactory());

            root.getChildren().add(menuItems);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

/**
 * Custom Menu Item
 */
package application;

import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;

/**
 * @author Rahul
 *
 */
public class MenuItem extends GridPane {
    private Integer name = null;

    public MenuItem(int i) {
        // TODO Auto-generated constructor stub
        this.name = i;
    }

    public Integer getName() {
        return name;
    }

    public void setName(Integer name) {
        this.name = name;
    }
}


//Menu Item Cell Factory
package application;

import org.controlsfx.control.GridCell;
import org.controlsfx.control.GridView;

import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.GridPane;
import javafx.util.Callback;

public class MenuItemCellFactory implements Callback<GridView<MenuItem>, GridCell<MenuItem>> {

    @Override
    public GridCell<MenuItem> call(GridView<MenuItem> listview) {
        return new MenuItemCell();
    }
}


//Menu Item Cell
package application;

import org.controlsfx.control.GridCell;

import javafx.scene.control.ListCell;
import javafx.scene.layout.GridPane;

public class MenuItemCell extends GridCell<MenuItem> {

    @Override
    protected void updateItem(MenuItem item, boolean empty) {
        // TODO Auto-generated method stub
        super.updateItem(item, empty);
        if (empty || item == null) {
            setText(null);
            setGraphic(null);
        } else {
            setText(item.getName().toString());
            setStyle("-fx-background-color: #ffffff; -fx-background-radius: 15; -fx-border-radius: 15; -fx-border-width: 0; -fx-padding: 10; -fx-pref-width: 145; -fx-max-width: 145; -fx-max-width: 145; -fx-pref-height: 130; -fx-max-height: 130; -fx-effect: dropshadow(three-pass-box, #93948d, 10, 0, 0, 0);");
        }
    }
}

这是一个使用GridView的示例代码,其中包含使用单元工厂的GridPane列表。请根据您的要求修改它。基本情况将保持不变。

欢迎提出建议和反馈意见。谢谢。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34573634

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档