描述:
想实现这种效果:点击 ListView 自定义的 listcell 中的 Label,触发 Label 的 MouseClicked 事件。
但点击之后出现以下效果。也没有触发 Label 的点击事件。
解决方法:
1. 设置使用 ListView 的 setFocusTraversable() 方法设置焦点 traverable。
ListView_log.setFocusTraversable(true);
//这里的 ListView_log 是我的ListView的变量名
2. 设置 ListView 以及内部 listcell 的样式
/* ListView整体样式 */
#ListView_log{
-fx-background-color: #fafdff;
-fx-background-radius: 10;
-fx-border-radius: 10;
-fx-border-color: rgb(215, 215, 215);
-fx-border-width: 1.5;
}
/* listcell的样式 */
#ListView_log .list-cell{
-fx-background-color: #fafdff;
}
/* listcell选中的样式,与未选中一样 */
#ListView_log .list-cell:selected {
-fx-background-color: #fafdff;
}
/* 选中时名字Label的样式,默认白色,需要单独设置 */
#ListView_log .list-cell:selected #Labelname {
-fx-text-fill: black;
}
Ok,检验成果~