JavaFx之ScrollPane滚动板面、CheckBox复选框(二十四)
多个复选框时可能会超出屏幕,需要使用滚动版面。 布局方式:ScrollPane 包括VBox 、VBox 包括多个 CheckBox ,效果如下:
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.util.HashSet;
import java.util.List;
public class SelectGeneratorTable {
HashSet<String> select = new HashSet();
public SelectGeneratorTable(List<String> allTable,Button button) {
Stage stage = new Stage();
stage.setTitle("选择要生成的表");
stage.setWidth(280);
stage.setHeight(400);
ScrollPane scrollPane = new ScrollPane();
VBox box = new VBox();
for (String t : allTable) {
CheckBox cb = new CheckBox(t);
cb.selectedProperty().addListener(new ChangeListener<Boolean>() {
public void changed(ObservableValue<? extends Boolean> ov,
Boolean old_val, Boolean new_val) {
if (cb.isSelected()) {
select.add(t);
} else {
select.remove(t);
}
}
});
box.getChildren().add(cb);
box.setMargin(cb,new Insets(10,10,0,10));
}
box.getChildren().add(button);
box.setMargin(button,new Insets(10,10,0,10));
scrollPane.setContent(box);
Scene scene = new Scene(scrollPane);
stage.setScene(scene);
stage.show();
}
public HashSet<String> getSelect() {
return select;
}
}
main
finalSql = getFinalSql();
List<String> allTable = finalSql.nativeSelect(getAllTableSql(), new ResultCallback<String>() {
@Override
public String callback(ResultSet result) throws SQLException {
return result.getString(1);
}
});
Button button = new Button("执行生成");
new SelectGeneratorTable(allTable, button);
|