gpt4 book ai didi

java - 设置自定义javafx ComboBox的值

转载 作者:行者123 更新时间:2023-12-01 12:58:18 25 4
gpt4 key购买 nike

编辑:我已经包含了给我错误的示例文件我正在尝试实现一个 ComboBox,根据 KeyStrokes 过滤项目,遵循 here 中的配方。 。我想列出世界上的国家/地区

我有FilterComboBox.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.collections.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import persondetails2.controller.*?>

<fx:root type="javafx.scene.control.ComboBox" fx:id="countries"
xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2"
fx:controller="persondetails2.controller.FilterComboBox">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="Austria" />
<String fx:value="Denmark" />
<String fx:value="France" />
<String fx:value="Germany" />
<String fx:value="Italy" />
<String fx:value="Portugal" />
<String fx:value="Spain" />
</FXCollections>
</items>
</fx:root>

FilterComboBox.java 如配方中所示。来源如下:

    /*
* Follows: https://stackoverflow.com/questions/13362607/combobox-jump-to-typed-char
*/

package persondetails2.controller;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.SingleSelectionModel;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

/**
*
* @author DRY
*/
public class FilterComboBox<T> extends ComboBox<T> {
//implements Initializable{
private final FilterComboBox<T> fcbo = this;

private ObservableList<T> items;
private ObservableList<T> filter;
private String s;
private Object selection;

private class KeyHandler implements EventHandler<KeyEvent> {

private SingleSelectionModel<T> sm;

public KeyHandler() {
sm = getSelectionModel();
s = "";
System.err.println("Initialized keyhandler");
}

@Override
public void handle(KeyEvent event) {
filter.clear();
// handle non alphanumeric keys like backspace, delete etc
if (event.getCode() == KeyCode.BACK_SPACE && s.length() > 0) {
s = s.substring(0, s.length() - 1);
} else {
s += event.getText();
}

if (s.length() == 0) {
fcbo.setItems(items);
sm.selectFirst();
return;
}
//System.out.println(s);
if (event.getCode().isLetterKey()) {
for (T item : items) {
if (item.toString().toUpperCase().startsWith(s.toUpperCase())) {

filter.add(item);
System.out.println(item);

fcbo.setItems(filter);

//sm.clearSelection();
//sm.select(item);

}
}
sm.select(0);
}

}
}

public FilterComboBox(final ObservableList<T> items) {
super(items);
this.items = items;
this.filter = FXCollections.observableArrayList();

setOnKeyReleased(new KeyHandler());

this.focusedProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue observable, Object oldValue, Object newValue) {
if ((boolean)newValue == false) {
s = "";
fcbo.setItems(items);
fcbo.getSelectionModel().select((T)selection);
}

}

});

this.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue observable, Object oldValue, Object newValue) {
if (newValue != null) {
selection = (Object) newValue;
}

}
});
}

}

我的主视图名为PersonDetails.fxml

<?xml version="1.0" encoding="UTF-8"?>


<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.collections.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>


<AnchorPane xmlns:fx="http://javafx.com/fxml/1" id="AnchorPane" prefHeight="200" prefWidth="320" fx:controller="persondetails2.controller.PersonDetailsController">
<ScrollPane id="personalData" fx:id="personaldata" fitToHeight="false" focusTraversable="false" maxHeight="-Infinity" prefHeight="-1.0" prefViewportHeight="1440.0" prefWidth="569.0" AnchorPane.bottomAnchor="2.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="-2.0">
<content>
<AnchorPane id="Content" focusTraversable="false" maxWidth="-1.0" minHeight="0.0" minWidth="0.0" prefHeight="-1.0" prefWidth="600.0">
<children>
<VBox focusTraversable="false" prefHeight="-1.0" prefWidth="-1.0" spacing="2.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<Label prefHeight="21.0001220703125" text="Personal Details">
<font>
<Font name="System Bold" size="14.0" fx:id="x1" />
</font>
</Label>

<GridPane id="GridPane" focusTraversable="false" hgap="3.0" prefWidth="354.0" vgap="4.0">
<children>
<Label text="Name:" GridPane.columnIndex="0" GridPane.rowIndex="0">
<labelFor>
<TextField fx:id="name" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="0" />
</labelFor>
</Label>
<fx:reference source="name" />
<Label text="Surname:" GridPane.columnIndex="0" GridPane.rowIndex="1" />
<TextField fx:id="surname" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label text="Country:" GridPane.columnIndex="0" GridPane.rowIndex="2" />
<fx:include source="FilterComboBox.fxml" GridPane.columnIndex="1" GridPane.rowIndex="2" />
</children>
</GridPane>
</children>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
</VBox>
</children>
<padding>
<Insets left="5.0" right="5.0" />
</padding>
</AnchorPane>
</content>
</ScrollPane>
</AnchorPane>

相应的 Controller 是PersonDetailsController.java

package persondetails2.controller;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;

/**
*
* @author DRY
*/
public class PersonDetailsController implements Initializable {

@FXML
private Label label;

@FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Hello World!");
}

@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}

}

此配置现在给我一个java.lang.InstantiationException: persondetails2.controller.FilterComboBox。我想读取 Controller 初始化中的数据,然后能够设置文本字段中的值以及组合框中选定的值。

关于如何继续的任何想法?

最佳答案

由于您要在 FXML 文件中设置组合框的项目,因此我将 FilterComboBox 构造函数重构为无参数,并添加了加载 FXML 文件的代码:

public class FilterComboBox<T> extends ComboBox<T> {

private final ObservableList<T> items;
private final ObservableList<T> filter;
private String s;
private Object selection;

private class KeyHandler implements EventHandler<KeyEvent> {

private SingleSelectionModel<T> sm;

public KeyHandler() {
sm = getSelectionModel();
s = "";
System.err.println("Initialized keyhandler");
}

@Override
public void handle(KeyEvent event) {
filter.clear();
// handle non alphanumeric keys like backspace, delete etc
if (event.getCode() == KeyCode.BACK_SPACE && s.length() > 0) {
s = s.substring(0, s.length() - 1);
} else {
s += event.getText();
}

if (s.length() == 0) {
setItems(items);
sm.selectFirst();
return;
}
//System.out.println(s);
if (event.getCode().isLetterKey()) {
for (T item : items) {
if (item.toString().toUpperCase().startsWith(s.toUpperCase())) {

filter.add(item);
System.out.println(item);

setItems(filter);

//sm.clearSelection();
//sm.select(item);
}
}
sm.select(0);
}

}
}

public FilterComboBox() {

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("FilterComboBox.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}

items = getItems();
this.filter = FXCollections.observableArrayList();

setOnKeyReleased(new KeyHandler());

this.focusedProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue observable, Object oldValue, Object newValue) {
if ((boolean) newValue == false) {
s = "";
setItems(items);
getSelectionModel().select((T) selection);
}
}
});

this.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue observable, Object oldValue, Object newValue) {
if (newValue != null) {
selection = (Object) newValue;
}
}
});
}

}

接下来,将 FilterComboBox.fxml 更改为:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.collections.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import persondetails2.controller.*?>

<fx:root type="FilterComboBox"
xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="Austria" />
<String fx:value="Denmark" />
<String fx:value="France" />
<String fx:value="Germany" />
<String fx:value="Italy" />
<String fx:value="Portugal" />
<String fx:value="Spain" />
</FXCollections>
</items>
</fx:root>

并将 PersonDetails 中的 fx:include 替换为:

<?import persondetails2.controller.FilterComboBox ?>
...

<FilterComboBox GridPane.columnIndex="1" GridPane.rowIndex="2" />

关于java - 设置自定义javafx ComboBox的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23720357/

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