Skip to content

Commit 8d3cb66

Browse files
authored
将 TableView 中的 CheckBox 替换为 JFXCheckBox (#4964)
1 parent 0613b34 commit 8d3cb66

File tree

3 files changed

+77
-4
lines changed

3 files changed

+77
-4
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Hello Minecraft! Launcher
3+
* Copyright (C) 2025 huangyuhui <huanghongxun2008@126.com> and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
package org.jackhuang.hmcl.ui.construct;
19+
20+
import com.jfoenix.controls.JFXCheckBox;
21+
import javafx.beans.binding.Bindings;
22+
import javafx.beans.property.BooleanProperty;
23+
import javafx.scene.control.TableCell;
24+
import javafx.scene.control.TableColumn;
25+
import javafx.util.Callback;
26+
27+
/// @author Glavo
28+
public final class JFXCheckBoxTableCell<S, T> extends TableCell<S, T> {
29+
public static <S> Callback<TableColumn<S, Boolean>, TableCell<S, Boolean>> forTableColumn(
30+
final TableColumn<S, Boolean> column) {
31+
return list -> new JFXCheckBoxTableCell<>();
32+
}
33+
34+
private final JFXCheckBox checkBox = new JFXCheckBox();
35+
private BooleanProperty booleanProperty;
36+
37+
public JFXCheckBoxTableCell() {
38+
this.getStyleClass().add("jfx-checkbox-table-cell");
39+
}
40+
41+
@Override
42+
protected void updateItem(T item, boolean empty) {
43+
super.updateItem(item, empty);
44+
45+
if (empty) {
46+
setText(null);
47+
setGraphic(null);
48+
checkBox.disableProperty().unbind();
49+
} else {
50+
setGraphic(checkBox);
51+
52+
if (booleanProperty != null) {
53+
checkBox.selectedProperty().unbindBidirectional(booleanProperty);
54+
}
55+
if (getTableColumn().getCellObservableValue(getIndex()) instanceof BooleanProperty obsValue) {
56+
booleanProperty = obsValue;
57+
checkBox.selectedProperty().bindBidirectional(booleanProperty);
58+
}
59+
60+
checkBox.disableProperty().bind(Bindings.not(
61+
getTableView().editableProperty().and(
62+
getTableColumn().editableProperty()).and(
63+
editableProperty())
64+
));
65+
}
66+
}
67+
68+
}

HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/ModUpdatesPage.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,15 @@
1818
package org.jackhuang.hmcl.ui.versions;
1919

2020
import com.jfoenix.controls.JFXButton;
21+
import com.jfoenix.controls.JFXCheckBox;
2122
import javafx.beans.property.*;
2223
import javafx.beans.value.ObservableValue;
2324
import javafx.collections.FXCollections;
2425
import javafx.collections.ObservableList;
2526
import javafx.geometry.Insets;
2627
import javafx.geometry.Pos;
27-
import javafx.scene.control.CheckBox;
2828
import javafx.scene.control.TableColumn;
2929
import javafx.scene.control.TableView;
30-
import javafx.scene.control.cell.CheckBoxTableCell;
3130
import javafx.scene.layout.BorderPane;
3231
import javafx.scene.layout.HBox;
3332
import org.jackhuang.hmcl.mod.LocalModFile;
@@ -38,6 +37,7 @@
3837
import org.jackhuang.hmcl.task.Task;
3938
import org.jackhuang.hmcl.ui.Controllers;
4039
import org.jackhuang.hmcl.ui.FXUtils;
40+
import org.jackhuang.hmcl.ui.construct.JFXCheckBoxTableCell;
4141
import org.jackhuang.hmcl.ui.construct.MessageDialogPane;
4242
import org.jackhuang.hmcl.ui.construct.PageCloseEvent;
4343
import org.jackhuang.hmcl.ui.decorator.DecoratorPage;
@@ -72,9 +72,10 @@ public ModUpdatesPage(ModManager modManager, List<LocalModFile.ModUpdate> update
7272
getStyleClass().add("gray-background");
7373

7474
TableColumn<ModUpdateObject, Boolean> enabledColumn = new TableColumn<>();
75-
CheckBox allEnabledBox = new CheckBox();
75+
var allEnabledBox = new JFXCheckBox();
76+
enabledColumn.setStyle("-fx-alignment: CENTER;");
7677
enabledColumn.setGraphic(allEnabledBox);
77-
enabledColumn.setCellFactory(CheckBoxTableCell.forTableColumn(enabledColumn));
78+
enabledColumn.setCellFactory(JFXCheckBoxTableCell.forTableColumn(enabledColumn));
7879
setupCellValueFactory(enabledColumn, ModUpdateObject::enabledProperty);
7980
enabledColumn.setEditable(true);
8081
enabledColumn.setMaxWidth(40);

HMCL/src/main/resources/assets/css/root.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,10 @@
813813
-jfx-unchecked-color: transparent;
814814
}
815815

816+
.table-view .jfx-check-box .jfx-rippler {
817+
-jfx-rippler-disabled: true;
818+
}
819+
816820
/*******************************************************************************
817821
* *
818822
* JFX Progress Bar *

0 commit comments

Comments
 (0)