我有一个javafx应用程序,它基本上将发票保存在数据库中并打印出来,可以发给客户。
本质上它是形式,它接受输入并按下保存按钮保存记录并使表格只读,直到按打印按钮打印。但是,禁用组合框似乎是使其成为只读的唯一方法,这会导致颜色变化并使其在打印时难以阅读。我尝试设置组合框样式如下,但没有帮助。有什么建议吗?
duration.setDisable(true);
duration.setStyle("-fx-opacity: 1");
duration.setStyle("-fx-text-fill: black");
duration.setStyle("-fx-background-color: white");
答案 0 :(得分:4)
setStyle()
是JavaFX Node
的style属性的setter方法。
通过连续三次调用此方法,只有最后一次调用才会被覆盖。
因此,如果你想应用所有三种风格,你应该写:
duration.setStyle("-fx-opacity: 1; -fx-text-fill: black;-fx-background-color: white");
但这足以显示ComboBox
处于默认状态,即使它已被禁用:
duration.setStyle("-fx-opacity: 1;");
编辑#1:示例
public void start(Stage primaryStage) {
ComboBox<String> combobox1 = new ComboBox<>();
ComboBox<String> combobox2 = new ComboBox<>();
combobox2.setDisable(true);
combobox2.setStyle("-fx-opacity: 1;");
BorderPane root = new BorderPane();
root.setPadding(new Insets(15));
root.setTop(combobox1);
root.setBottom(combobox2);
Scene scene = new Scene(root, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
这将产生以下输出,没有明显的区别:
编辑#2 CSS
我忘记了ComboBox
中的列表单元格,所以我们需要更多的CSS来重置不透明度:
.combo-box-base:disabled,
.list-cell:disabled
{
-fx-opacity: 1;
}
您必须将此CSS规则添加到css文件,然后添加到场景中,例如:
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
答案 1 :(得分:0)
以下代码应模拟问题
public class JavaFXApplication8 extends Application {
private ObservableList<String> comboboxdata = FXCollections.observableArrayList();
@Override
public void start(Stage primaryStage) {
ComboBox<String> combobox1 = new ComboBox<>();
comboboxdata.add("Monthly");
comboboxdata.add("Quarterly");
comboboxdata.add("HalfYearly");
comboboxdata.add("Yearly");
combobox1.setItems(comboboxdata);
combobox1.getSelectionModel().select("Yearly");
ComboBox<String> combobox2 = new ComboBox<>();
combobox2.setItems(comboboxdata);
combobox2.getSelectionModel().select("Yearly");
combobox2.setDisable(true);
combobox2.setStyle("-fx-opacity: 1;");
BorderPane root = new BorderPane();
// root.setPadding(new Insets(15));
root.setTop(combobox1);
root.setBottom(combobox2);
Scene scene = new Scene(root, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
这就是我解决问题的方法。我没有禁用Combobox,而是删除了我不感兴趣的Combobox中的所有项目。一旦用户做了他需要做的事情,我就重新初始化了将这些项目带回Combobox。肮脏的解决方案,但它的工作原理。
public class JavaFXApplication8 extends Application {
private ObservableList<String> comboboxdata = FXCollections.observableArrayList();
ComboBox<String> combobox1 = new ComboBox<>();
ComboBox<String> combobox2 = new ComboBox<>();
@Override
public void start(Stage primaryStage) {
setEnableMode();
BorderPane root = new BorderPane();
root.setTop(combobox1);
root.setBottom(combobox2);
Scene scene = new Scene(root, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
setPrintMode();
}
private void setPrintMode() {
String temp = combobox2.getSelectionModel().getSelectedItem();
combobox2.getItems().removeAll(combobox2.getItems());
combobox2.setValue(temp);
// {Do what you have to do here}
setEnableMode();
}
private void setEnableMode() { addComboData(); }
private void addComboData() {
comboboxdata.add("Monthly");
comboboxdata.add("Quarterly");
comboboxdata.add("HalfYearly");
comboboxdata.add("Yearly");
combobox2.setItems(comboboxdata);
combobox1.setItems(comboboxdata);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}