[英]Initialize Map of EnumMap
I need to initialize the following private member in a JavaFX application where I am trying to organize gui widgets, however I do not know the correct syntax, could someone please let me know the correct syntax: 我需要在尝试组织gui小部件的JavaFX应用程序中初始化以下私有成员,但是我不知道正确的语法,有人可以让我知道正确的语法:
Here is the enum I am using for my EnumMap 这是我用于我的EnumMap的枚举
enum Connection {
Connection1,
Connection2,
Connection3,
Connection4;
}
Here are a selection of the widgets that I am trying to organize with this map or EnumMaps based on a service name key, so the following list of 4 checkboxes and labels belong together, (I have similar JavaFX widgets for service 2 and so forth. 这是我尝试根据服务名称键与此地图或EnumMaps进行组织的小部件的选择,因此以下4个复选框和标签的列表属于同一列表(我为服务2具有类似的JavaFX小部件,依此类推。
@FXML
private CheckBox mService1CheckBox1;
@FXML
private CheckBox mService1CheckBox2;
@FXML
private CheckBox mService1CheckBox3;
@FXML
private CheckBox mService1CheckBox4;
@FXML
private Label mService1Label1;
@FXML
private Label mService1Label1;
@FXML
private Label mService1Label1;
@FXML
private Label mService1Label1;
This is the private Member I am trying to initialize with JavaFX widgets 这是我尝试使用JavaFX小部件初始化的私有成员
private Map<String, EnumMap<Connection, AbstractMap.SimpleEntry<Label,
CheckBox>>> mWidgetInfo;
I can initialize the top level empty mServiceWidgetMap with: 我可以使用以下方法初始化顶级空mServiceWidgetMap:
mWidgetInfo= new HashMap<>();
And I know that I need to initialize the EnumMaps as new EnumMap<>(Connection.class); 而且我知道我需要将EnumMaps初始化为新的EnumMap <>(Connection.class); but I also need to put pairs of widgets in the value size of these EnumMaps and I am confused as to how to do this.
但是我还需要将成对的小部件放置在这些EnumMap的值大小中,我对如何执行此操作感到困惑。
However I don't know how to initialize the enumMap value pairs. 但是我不知道如何初始化enumMap值对。 Syntax help much appreciated.
语法帮助非常感谢。
EDIT After struggling for a while I came up with the following, but surely there has got to be a simpler approach like double brace initialization or some other less verbose approach. 编辑经过一段时间的努力,我想到了以下内容,但是肯定有一种更简单的方法,例如双括号初始化或其他一些不太冗长的方法。
private void initializeServiceHeartbeatTab() {
// @JC Todo - dynamically create base on CSV rows
// Map<String, EnumMap<Connection, SimpleEntry<Label, CheckBox>>>
EnumMap<Connection, SimpleEntry<Label, CheckBox>> service1Info =
new EnumMap<>(Connection.class);
SimpleEntry<Label, CheckBox> pair1 = new SimpleEntry<>(
mService1Label1, mService1CheckBox1);
SimpleEntry<Label, CheckBox> pair2 = new SimpleEntry<>(
mService1Label2, mService1CheckBox2);
SimpleEntry<Label, CheckBox> pair3 = new SimpleEntry<>(
mService1Label3, mService1CheckBox3);
SimpleEntry<Label, CheckBox> pair4 = new SimpleEntry<>(
mService1Label4, mService1CheckBox4);
service1Info.put(Connection.Connection1, pair1);
service1Info.put(Connection.Connection1, pair2);
service1Info.put(Connection.Connection1, pair3);
service1Info.put(Connection.Connection1, pair4);
EnumMap<Connection, SimpleEntry<Label, CheckBox>> service2Info =
new EnumMap<>(Connection.class);
pair1 = new SimpleEntry<>(mService2Label1, mService2CheckBox1);
pair2 = new SimpleEntry<>(mService2Label2, mService2CheckBox2);
pair3 = new SimpleEntry<>(mService2Label3, mService2CheckBox3);
pair4 = new SimpleEntry<>(mService2Label4, mService2CheckBox4);
service2Info.put(Connection.Connection1, pair1);
service2Info.put(Connection.Connection1, pair2);
service2Info.put(Connection.Connection1, pair3);
service2Info.put(Connection.Connection1, pair4);
EnumMap<Connection, SimpleEntry<Label, CheckBox>> service2Info =
new EnumMap<>(Connection.class);
pair1 = new SimpleEntry<>(mService3Label1, mService3CheckBox1);
pair2 = new SimpleEntry<>(mService3Label2, mService3CheckBox2);
pair3 = new SimpleEntry<>(mService3Label3, mService3CheckBox3);
pair4 = new SimpleEntry<>(mService3Label4, mService3CheckBox4);
service3Info.put(Connection.Connection1, pair1);
service3Info.put(Connection.Connection1, pair2);
service3Info.put(Connection.Connection1, pair3);
service3Info.put(Connection.Connection1, pair4);
mWidgetInfo = new HashMap<>();
mWidgetInfo.put("albf", service1Info);
mWidgetInfo.put("fms1", service2Info);
mWidgetInfo.put("fms2", service3Info);
}
I finally figured out how to initialize the above Map. 我终于想出了如何初始化以上Map。 The trick to initializing it without having all those un-necessary temporaries was to use nested double brace initialization as follows:
在没有所有那些不必要的临时属性的情况下进行初始化的技巧是使用嵌套的双括号初始化 ,如下所示:
// Map<String, EnumMap<Connection, SimpleEntry<Label, CheckBox>>>
mWidgetInfo = new HashMap() {{
put("service1", new EnumMap(Connection.class) {{
put(Connection.Connection1, new SimpleEntry<>(mService1Label1, mService1CheckBox1));
put(Connection.Connection2, new SimpleEntry<>(mService1Label2, mService1CheckBox2));
put(Connection.Connection3, new SimpleEntry<>(mService1Label3, mService1CheckBox3));
put(Connection.Connection4, new SimpleEntry<>(mService1Label4, mService1CheckBox4));
}});
put("service2", new EnumMap(Connection.class) {{
put(Connection.Connection1, new SimpleEntry<>(mService2Label1, mService2CheckBox1));
put(Connection.Connection2, new SimpleEntry<>(mService2Label2, mService2CheckBox2));
put(Connection.Connection3, new SimpleEntry<>(mService2Label3, mService2CheckBox3));
put(Connection.Connection4, new SimpleEntry<>(mService2Label4, mService2CheckBox4));
}});
put("service3", new EnumMap(Connection.class) {{
put(Connection.Connection1, new SimpleEntry<>(mService3Label1, mService3CheckBox1));
put(Connection.Connection2, new SimpleEntry<>(mService3Label2, mService3CheckBox2));
put(Connection.Connection3, new SimpleEntry<>(mService3Label3, mService3CheckBox3));
put(Connection.Connection4, new SimpleEntry<>(mService3Label4, mService3CheckBox4));
}});
}};
Then later I can iterate over all the widgets in the collection - for example initializing the tooltip help and setting a default label as follows: 然后,稍后我可以遍历集合中的所有小部件-例如,初始化工具提示帮助并按如下所示设置默认标签:
// initialize the tooltips & default labels
mWidgetInfo.forEach((k, v)-> {
// no need to switch on the service
// disable click events on the checkboxes
// as they should be read only
v.forEach((k1, widgetPair)-> {
widgetPair.getKey().setTooltip(
new Tooltip("Connect ID"));
CheckBox checkBox = widgetPair.getValue();
checkBox.setTooltip(new Tooltip("Servicing Connection"));
checkBox.setOpacity(1);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.