import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.ScrollToEvent;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.cell.TextFieldListCell;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
//数据源 可观察列表
ObservableList<String> obslist = FXCollections.observableArrayList();
obslist.add("data - a");
obslist.add("data - b");
obslist.add("data - c");
obslist.add("data - d");
obslist.add("data - e");
obslist.add("data - f");
obslist.add("data - g");
obslist.add("data - h");
obslist.add("data - i");
obslist.add("data - j");
obslist.add("data - k");
obslist.add("data - l");
obslist.add("data - m");
obslist.add("data - n");
obslist.add("data - o");
obslist.add("data - p");
obslist.add("data - q");
obslist.add("data - r");
ListView<String> listView = new ListView<String>(obslist);
listView.setPrefHeight(200);
Button bu = new Button("按钮");
AnchorPane an = new AnchorPane();
an.getChildren().addAll(listView,bu);
AnchorPane.setTopAnchor(listView,100.0);
AnchorPane.setLeftAnchor(listView,100.0);
//单选或多选
listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);//多选
//监听 项
listView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
System.out.println("项 = " + newValue);
}
});
//监听 索引
listView.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
System.out.println("索引 = " + newValue.intValue());
}
});
//滚动事件 listView.scrollTo()执行可以触发
listView.setOnScrollTo(new EventHandler<ScrollToEvent<Integer>>() {
@Override
public void handle(ScrollToEvent<Integer> event) {
System.out.println(event.getScrollTarget());
}
});
//双击可编辑 - 修改了原始数据
listView.setEditable(true);
//工厂方法
listView.setCellFactory(TextFieldListCell.forListView());
Scene scene = new Scene(an);
primaryStage.setScene(scene);
primaryStage.setWidth(800);
primaryStage.setHeight(800);
primaryStage.setTitle("JavaFX");
primaryStage.show();
bu.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
//单选
// listView.getSelectionModel().select("data - g");
// listView.scrollTo("data - g");//滚动到
// listView.requestFocus();//获得焦点
//多选
// listView.getSelectionModel().selectIndices(1,2,4);//按钮按下后选中下标为1,2,4的项
// listView.requestFocus();//获得焦点
// listView.getSelectionModel().getSelectedItems().forEach(System.out::println);
obslist.forEach(System.out::println);
}
});
}
}
【JavaFX】ListView
最新推荐文章于 2024-04-15 02:14:59 发布