搜索
JavaFX的ListView实现问题
怪我咯
怪我咯 2017-04-17 14:23:06
[Java讨论组]

在JavaFX中,是否可以用ListView实现类似下面的效果。
也就是说每一格不只有一个标题,另外,我需要这个ListView从一个ObservableList<CustomObject>里获取数据,每一个<CustomObject>包含了名称,日期,状态等属性。
最后,还需要在ListView上绑上监听器...
由于这里水平不高,所以还希望讲解尽量详细一些,附上一些代码什么的

真的是十分感谢!

怪我咯
怪我咯

走同样的路,发现不同的人生

全部回复(2)
黄舟
ListView.setCellFactory( new Callback<ListView<T>, ListCell<T>>() {
    @Override 
    public ListCell<T> call(ListView<T> listView) {
        return new ListCell<T>() {
            @Override 
            public void updateItem(T item, boolean empty) {
            super.updateItem(item, empty);
            this.setGraphic(...);
            this..setText(...);
            }
        };
    })
PHP中文网
/* MyApplication.java */

import org.junit.Test;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class MyApplication extends javafx.application.Application {
    
    /**
     * Java main for when running without JavaFX launcher
     * 
     * <pre>
     * launch(args);
     * </pre>
     * 
     * @param args
     *            Command-line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
    /**
     * @see javafx.application.Application#start(javafx.stage.Stage)
     */
    @Override
    public void start(Stage primaryStage) throws Exception {
        ListView<CustomObject> list = new ListView<CustomObject>(getItems());
        list.setCellFactory(e -> new ListCell<CustomObject>() {
            @Override
            public void updateItem(CustomObject item, boolean empty) {
                if (!empty && item != null) {
                    BorderPane cell = new BorderPane();
                    cell.setBackground(new Background(new BackgroundFill(Color.grayRgb(54), null, null)));
                    
                    Text name = new Text(item.name());
                    name.setFont(Font.font(14));
                    name.setFill(Color.WHITE);
                    cell.setTop(name);
                    
                    Text date = new Text(item.date());
                    date.setFont(Font.font(10));
                    date.setFill(Color.WHITE);
                    cell.setLeft(date);
                    
                    Text status = new Text(item.status());
                    status.setFont(Font.font(10));
                    status.setFill(Color.WHITE);
                    cell.setRight(status);
                    
                    setGraphic(cell);
                }
                super.updateItem(item, empty);
            }
            
            @Override
            public void updateSelected(boolean selected) {
                super.updateSelected(selected);
                ((BorderPane) getGraphic()).setBackground(new Background(new BackgroundFill(selected ? Color.grayRgb(32) : Color.grayRgb(54), null, null)));
            }
        });
        list.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
        list.getSelectionModel().selectFirst();
        list.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            // Listener
            System.out.println("You are selecting " + newValue);
        });
        primaryStage.setScene(new Scene(new BorderPane(list)));
        primaryStage.setTitle("ListView Example");
        primaryStage.show();
    }
    
    public ObservableList<CustomObject> getItems() {
        return FXCollections.observableArrayList(
                new CustomObject("Hello World", "2015/5/18", CustomObject.Status.CHANGED),
                new CustomObject("Computer Progra...", "2015/4/1", CustomObject.Status.SYNCED),
                new CustomObject("Read Me", "2014/12/4", CustomObject.Status.CHANGED),
                new CustomObject("WDPartitionTable", "2014/7/28", CustomObject.Status.SYNCED),
                new CustomObject("Social Study Review", "2015/5/15", CustomObject.Status.SYNCED)
        );
    }
}

/* CustomObject.java */

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CustomObject {
    public static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy/m/d");
    
    private String name;
    private Date date;
    private Status status;
    
    public CustomObject(String name, String date, Status status) {
        this.name = name;
        try {
            this.date = DATE_FORMAT.parse(date);
        } catch (ParseException exception) {
            throw new IllegalArgumentException(exception);
        }
        this.status = status;
    }
    
    /**
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return name();
    }
    
    public String name() {
        return name;
    }
    
    public String date() {
        return DATE_FORMAT.format(date);
    }
    
    public Date rawDate() {
        return date;
    }
    
    public String status() {
        return status.toString().toLowerCase();
    }
    
    public Status rawStatus() {
        return status;
    }
    
    public enum Status {
        CHANGED, SYNCED
    }
}

我也是新手,只能做成这样了

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号