我想包括对我的组合框的检查,以限制对某些值的"访问".我可以从列表中删除那些无法访问的项目,是的,但是我希望用户看到其他选项,即使他还不能选择它们.
问题:在changelistener中选择另一个值会导致IndexOutOfBoundsException,我不知道为什么.
这是一个SSCCE.它创建一个具有整数值的ComboBox,默认情况下会选择第一个.然后我试着保持它很容易:每次更改值都被视为"错误",我将选择更改回第一个元素.但是,IndexOutOfBounds:
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.stage.Stage;
public class Tester extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
ComboBox<Integer> box = new ComboBox<Integer>();
ObservableList<Integer> vals= FXCollections.observableArrayList(0,1,2,3);
box.setItems(vals);
box.getSelectionModel().select(0);
/*box.valueProperty().addListener((observable, oldValue, newValue) -> {
box.getSelectionModel().select(0);
});*/
/*box.getSelectionModel().selectedItemProperty().addListener((observable,oldValue,newValue)->{
System.out.println(oldValue+","+newValue);
box.getSelectionModel().select(0);
});*/
box.getSelectionModel().selectedIndexProperty().addListener((observable,oldValue,newValue)->{
System.out.println(oldValue+","+newValue);
box.getSelectionModel().select(0);
});
Scene scene = new Scene(new Group(box),500,500);
stage.setScene(scene);
stage.show();
}
}
Run Code Online (Sandbox Code Playgroud)
我用valueProperty,selectedItemProperty和selectedIndexProperty测试了它,以及所有这些:
box.getSelectionModel().select(0);
box.getSelectionModel().selectFirst();
box.getSelectionModel().selectPrevious();
box.setValue(0);
if (oldValue.intValue() …Run Code Online (Sandbox Code Playgroud) 假设 JSON 结构具有多个可选字段。通过课程,你可以做类似的事情
public static final class Foo {
@JsonProperty("x")
private int x = 1;
@JsonProperty("y")
private int y = 2;
@JsonProperty("z")
private int z = 3;
}
Run Code Online (Sandbox Code Playgroud)
它定义了字段的默认值,以防它不存在于提供的 json 中。这也可以用记录来完成吗?
public record Foo(int x, int y, int z) {
}
Run Code Online (Sandbox Code Playgroud)
构造函数重载显然不是一个选项,据我所知,无论如何你只能有一个@JsonCreator注释。
自定义反序列化器应该可以解决这个问题,但是有没有其他方法,比如提供一个默认值的注释,以在记录的构造函数中使用,以防 json 中未提供该值?
我希望我的 tabpane 中的选项卡适合它们所在 tabpane 的完整大小。所以基本上应该没有可见的标题区域,所有内容都应该被选项卡覆盖。
我在这里有两个问题:
问候
给定一些扩展共同特征的案例类,我希望能够根据它们的类型定义一个排序(实际上不一定,目标是以这种方式对它们进行排序)。例如:
sealed trait Element
case class A(x: Int) extends Element
case class B(x: Int) extends Element
case class C(x: Int) extends Element
case class D(x: Int) extends Element
case class E(x: Int) extends Element
case class F(x: Int) extends Element
val elements: List[Element] = List(
A(5), F(3), E(1), C(19), A(3), F(1)
)
Run Code Online (Sandbox Code Playgroud)
排序为F -> A -> all other cases,因此结果列表为List(F(3), F(1), A(5), A(3), E(1), C(19))。相同类型元素之间的顺序无关紧要。
我想出了多种不同的解决方案,但它们看起来都很复杂,我只是觉得我错过了一些明显的方法来实现这一点。这就是我使用排序实现它的方式:
val sorted = elements.sorted{(a: Element, b: Element) => (a, b) match …Run Code Online (Sandbox Code Playgroud) sorting functional-programming scala pattern-matching case-class