gpt4 book ai didi

user-interface - 如何在JavaFX中创建多列组合框?

转载 作者:行者123 更新时间:2023-12-01 23:16:35 26 4
gpt4 key购买 nike

我正在尝试创建一个在下拉菜单中显示多列的ComboBox

这是一个屏幕截图,显示了我想要的外观:

MultiColumn ComboBox

有什么建议吗?

我想到的唯一解决方案是通过扩展 ComboBox 并使用多个列对其进行自定义来创建自定义容器。

但是 JavaFX 是否为我提供了创建自定义 UI 容器的选项?

如何创建自定义 UI 容器以及如何在 FXML 中使用它?

最佳答案

您不需要扩展ComboBox来创建类似的布局。相反,您只需要提供您自己的 CellFactory 实现。

通过创建自定义 CellFactory,您可以通过提供自己的 Listcell(该项目在您的 ComboBox 中显示)来控制如何显示实际上可以在下拉菜单中选择)。

我确信有很多方法可以实现此目的,但对于本示例,我将使用 GridPane 作为 ListCell 的根布局。

下面的完整示例也包含注释:

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

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

@Override
public void start(Stage primaryStage) {

// Simple Interface
VBox root = new VBox(10);
root.setAlignment(Pos.CENTER);
root.setPadding(new Insets(10));

// List of sample Persons
ObservableList<Person> persons = FXCollections.observableArrayList();
persons.addAll(
new Person("Maria Anders", "Sales Representative", "Zurich"),
new Person("Ana Trujillo", "Owner", "Sydney"),
new Person("Thomas Hardy", "Order Administrator", "Dallas")
);

// Create a simple ComboBox of Persons
ComboBox<Person> cboPersons = new ComboBox<>();
cboPersons.setItems(persons);

// We need a StringConverter in order to ensure the selected item is displayed properly
// For this sample, we only want the Person's name to be displayed
cboPersons.setConverter(new StringConverter<Person>() {
@Override
public String toString(Person person) {
return person.getName();
}

@Override
public Person fromString(String string) {
return null;
}
});

// Provide our own CellFactory to control how items are displayed
cboPersons.setCellFactory(cell -> new ListCell<Person>() {

// Create our layout here to be reused for each ListCell
GridPane gridPane = new GridPane();
Label lblName = new Label();
Label lblTitle = new Label();
Label lblLocation = new Label();

// Static block to configure our layout
{
// Ensure all our column widths are constant
gridPane.getColumnConstraints().addAll(
new ColumnConstraints(100, 100, 100),
new ColumnConstraints(100, 100, 100),
new ColumnConstraints(100, 100, 100)
);

gridPane.add(lblName, 0, 1);
gridPane.add(lblTitle, 1, 1);
gridPane.add(lblLocation, 2, 1);

}

// We override the updateItem() method in order to provide our own layout for this Cell's graphicProperty
@Override
protected void updateItem(Person person, boolean empty) {
super.updateItem(person, empty);

if (!empty && person != null) {

// Update our Labels
lblName.setText(person.getName());
lblTitle.setText(person.getTitle());
lblLocation.setText(person.getLocation());

// Set this ListCell's graphicProperty to display our GridPane
setGraphic(gridPane);
} else {
// Nothing to display here
setGraphic(null);
}
}
});

// Add the ComboBox to the scene
root.getChildren().addAll(
new Label("Select Person:"),
cboPersons
);

// Show the stage
primaryStage.setScene(new Scene(root));
primaryStage.setTitle("Sample");
primaryStage.show();
}
}

// Simple Person class to represent our...Persons
class Person {

private final StringProperty name = new SimpleStringProperty();
private final StringProperty title = new SimpleStringProperty();
private final StringProperty location = new SimpleStringProperty();

Person(String name, String title, String location) {
this.name.set(name);
this.title.set(title);
this.location.set(location);
}

public String getName() {
return name.get();
}

public void setName(String name) {
this.name.set(name);
}

public StringProperty nameProperty() {
return name;
}

public String getTitle() {
return title.get();
}

public void setTitle(String title) {
this.title.set(title);
}

public StringProperty titleProperty() {
return title;
}

public String getLocation() {
return location.get();
}

public void setLocation(String location) {
this.location.set(location);
}

public StringProperty locationProperty() {
return location;
}
}

The Results:

screenshot

关于user-interface - 如何在JavaFX中创建多列组合框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58286227/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com