首页 > JavaFX的ListView实现问题

JavaFX的ListView实现问题

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

真的是十分感谢!


/* 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
    }
}

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


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(...);
            }
        };
    })
【热门文章】
【热门文章】