我在JavaFX中有一个这样的表:
@FXML私有表视图表EF;
例如,当我按下一个按钮或更改ComboBox中的值时,如何将它从GUI中完全隐藏起来,当我按下另一个按钮或再次更改ComboBox中的值后,如何使它再次可见?
编辑:
public class AllController {
private RaportPDF wrpdf;
@FXML private Pagination pagination1;
@FXML private Pagination pagination2;
@FXML private Pagination pagination3;
public void updateSarcina(Observable<Sarcina> observable)
{
SarcinaService service = (SarcinaService)observable;
smodel.setAll(service.getAllSarcinas());
}
public void updatePost(Observable<Post> observable)
{
PostService service = (PostService)observable;
pmodel.setAll(service.getAllPosts());
}
public void updateFisa(Observable<Elementfisa> observable)
{
FisaService service = (FisaService)observable;
fmodel.setAll(service.getAllFisa());
}
public Observer<Sarcina> getSarcinaObserver()
{
return new Observer<Sarcina>()
{
@Override
public void update(Observable<Sarcina> observable)
{
updateSarcina(observable);
}
};
}
public Observer<Post> getPostObserver()
{
return new Observer<Post>()
{
@Override
public void update(Observable<Post> observable)
{
updatePost(observable);
}
};
}
public Observer<Elementfisa> getFisaObserver()
{
return new Observer<Elementfisa>()
{
@Override
public void update(Observable<Elementfisa> observable)
{
updateFisa(observable);
}
};
}
@SuppressWarnings("rawtypes")
@FXML private TableView allTable;
@FXML private TableView<Post> tableP;
@FXML private TableView<Sarcina> tableS;
@FXML private TableView<Elementfisa> tableEF;
@FXML private TableColumn<Sarcina, String> sFirstColumn;
@FXML private TableColumn<Sarcina, String> sSecondColumn;
@FXML private TableColumn<Post, String> pFirstColumn;
@FXML private TableColumn<Post, String> pSecondColumn;
@FXML private TableColumn<Elementfisa, String> fFirstColumn;
@FXML private TableColumn<Elementfisa, String> fSecondColumn;
@FXML private ComboBox<String> ComboObject;
@FXML private Label firstLabel;
@FXML private Label secondLabel;
@FXML private Label thirdLabel;
@FXML private TextField firstTextField;
@FXML private TextField secondTextField;
@FXML private TextField thirdTextField;
@FXML private TextField filterTextField;
@FXML private RadioButton radioButtonFirst;
@FXML private RadioButton radioButtonSecond;
@FXML private Button addButton;
@FXML private Button updateButton;
@FXML private Button deleteButton;
@FXML private Button clearFieldsButton;
@FXML private Button raportButton;
@FXML private Pagination pagination ;
SarcinaService sservice;
PostService pservice;
FisaService fservice;
ObservableList<Sarcina> smodel;
ObservableList<Post> pmodel;
ObservableList<Elementfisa> fmodel;
private String currentComboBoxString;
private Boolean isSelectedFC;
private Boolean isSelectedSC;
ToggleGroup toggleRadioGroup = new ToggleGroup();
public AllController()
{
}
private int intValidate(String e)
{
for(int i = 0; i < e.length(); i++)
{
if(i == 0 && e.charAt(i) == '-')
{
if(e.length() == 1)
{
showErrorMessage("Numar invalid !");
return -1;
}
else continue;
}
if(Character.digit(e.charAt(i), 10) < 0)
{
showErrorMessage("Numar invalid !");
return -1;
}
}
return Integer.parseInt(e);
}
private void fillItemsOnTable(boolean justColumns)
{
ObservableList<Sarcina> localModel1 = null;
ObservableList<Post> localModel2 = null;
ObservableList<Elementfisa> localModel3 = null;
if (currentComboBoxString.equals("Sarcina"))
{
tableP.setVisible(false);
tableEF.setVisible(false);
tableS.setVisible(true);
tableS.getColumns().clear();
tableS.getColumns().add(sFirstColumn);
tableS.getColumns().add(sSecondColumn);
localModel1 = this.smodel;
}
else if (currentComboBoxString.equals("Post"))
{
tableP.setVisible(true);
tableEF.setVisible(false);
tableS.setVisible(false);
tableP.getColumns().clear();
tableP.getColumns().add(pFirstColumn);
tableP.getColumns().add(pSecondColumn);
localModel2 = this.pmodel;
}
else if (currentComboBoxString.equals("Fisa"))
{
tableP.setVisible(false);
tableEF.setVisible(true);
tableS.setVisible(false);
tableEF.getColumns().clear();
tableEF.getColumns().add(fFirstColumn);
tableEF.getColumns().add(fSecondColumn);
localModel3 = this.fmodel;
}
if (!justColumns)
{
if (localModel1!=null)
{
tableS.setItems(localModel1);
tableP.setVisible(false);
tableEF.setVisible(false);
tableS.setVisible(true);
}
else
if (localModel2!=null)
{
tableP.setItems(localModel2);
tableP.setVisible(true);
tableEF.setVisible(false);
tableS.setVisible(false);
}
else
{
tableEF.setItems(localModel3);
tableP.setVisible(false);
tableEF.setVisible(true);
tableS.setVisible(false);
}
}
}
@FXML public void handleRaport()
{
if (isSelectedFC)
{
ObservableList<Sarcina> model = FXCollections.observableArrayList(fservice.filterRapoarte());
ComboObject.setValue("Sarcina");
this.fillItemsOnTable(true);
tableS.setItems(model);
wrpdf.addPdf(model);
}
}
public void setService(SarcinaService sservice, PostService pservice, FisaService fservice)
{
this.sservice = sservice;
this.pservice = pservice;
this.fservice = fservice;
this.smodel = FXCollections.observableArrayList(sservice.getAllSarcinas());
this.pmodel = FXCollections.observableArrayList(pservice.getAllPosts());
this.fmodel = FXCollections.observableArrayList(fservice.getAllFisa());
this.fillItemsOnTable(false);
}
@FXML private void onActionComboBox(ActionEvent event)
{
String current = ComboObject.getSelectionModel().getSelectedItem();
if (current.compareTo(currentComboBoxString) != 0)
{
currentComboBoxString = current;
if (current.equals("Sarcina"))
{
secondLabel.setText("Desc: ");
radioButtonSecond.setText("By Desc");
thirdLabel.setVisible(false);
radioButtonFirst.setVisible(false);
thirdTextField.setVisible(false);
}
else if (current.equals("Post"))
{
secondLabel.setText("Name: ");
thirdLabel.setText("Type: ");
radioButtonFirst.setText("By Name");
radioButtonSecond.setText("By Type");
thirdLabel.setVisible(true);
radioButtonFirst.setVisible(true);
thirdTextField.setVisible(true);
}
else if (current.equals("Fisa"))
{
secondLabel.setText("Sarcina ID: ");
thirdLabel.setText("Post ID: ");
radioButtonFirst.setText("By Sarcina");
radioButtonSecond.setText("By Post");
thirdLabel.setVisible(true);
radioButtonFirst.setVisible(true);
thirdTextField.setVisible(true);
}
this.fillItemsOnTable(false);
}
}
@FXML private void initialize()
{
pagination1.setPageFactory(this::createPageP);
pagination2.setPageFactory(this::createPageS);
pagination3.setPageFactory(this::createPageEF);
ComboObject.getItems().addAll
(
"Sarcina",
"Post",
"Fisa"
);
currentComboBoxString = "Sarcina";
ComboObject.setValue("Sarcina");
thirdLabel.setVisible(false);
radioButtonFirst.setVisible(false);
thirdTextField.setVisible(false);
isSelectedFC = true;
isSelectedSC = false;
radioButtonFirst.setToggleGroup(toggleRadioGroup);
radioButtonSecond.setToggleGroup(toggleRadioGroup);
radioButtonFirst.setSelected(true);
if (currentComboBoxString.equals("Post"))
{
tableP.getSelectionModel().selectedItemProperty().addListener((obs, oldSelection, newSelection) -> {
if (newSelection != null)
{
if (currentComboBoxString.equals("Post"))
{
firstTextField.setText(((Post) newSelection).getId().toString());
secondTextField.setText(((Post) newSelection).getNume());
thirdTextField.setText(((Post) newSelection).getTip());
}
}
});
}
else
if (currentComboBoxString.equals("Sarcina"))
{
tableS.getSelectionModel().selectedItemProperty().addListener((obs, oldSelection, newSelection) -> {
if (newSelection != null)
{
if (currentComboBoxString.equals("Sarcina"))
{
firstTextField.setText(((Sarcina) newSelection).getId().toString());
secondTextField.setText(((Sarcina) newSelection).getDesc());
}
}
});
}
else
if (currentComboBoxString.equals("Fisa"))
{
tableEF.getSelectionModel().selectedItemProperty().addListener((obs, oldSelection, newSelection) -> {
if (newSelection != null)
{
if (currentComboBoxString.equals("Fisa"))
{
firstTextField.setText(((Elementfisa) newSelection).getId().toString());
secondTextField.setText(((Elementfisa) newSelection).getSarcina().getId().toString());
thirdTextField.setText(((Elementfisa) newSelection).getPost().getId().toString());
}
}
});
}
}
public int itemsPerPage()
{
return 1;
}
public int rowsPerPage()
{
return 7;
}
@SuppressWarnings("unchecked")
public VBox createPageS(int pageIndex)
{
int lastIndex = 0;
int displace = smodel.size() % rowsPerPage();
if (displace > 0) {
lastIndex = smodel.size() / rowsPerPage();
} else {
lastIndex = smodel.size() / rowsPerPage() - 1;
}
VBox box = new VBox(7);
int page = pageIndex * itemsPerPage();
for (int i = page; i < page + itemsPerPage(); i++) {
TableView<Sarcina> tableS = new TableView<Sarcina>();
sFirstColumn.setCellValueFactory(
new PropertyValueFactory<Sarcina, String>("Id"));
sFirstColumn.setMinWidth(20);
sSecondColumn.setCellValueFactory(
new PropertyValueFactory<Sarcina, String>("desc"));
sSecondColumn.setMinWidth(160);
tableS.getColumns().addAll(sFirstColumn, sSecondColumn);
if (lastIndex == pageIndex) {
tableS.setItems(FXCollections.observableArrayList(smodel.subList(pageIndex * rowsPerPage(), pageIndex * rowsPerPage() + displace)));
} else {
tableS.setItems(FXCollections.observableArrayList(smodel.subList(pageIndex * rowsPerPage(), pageIndex * rowsPerPage() + rowsPerPage())));
}
box.getChildren().add(tableS);
}
pagination2.setPageCount(smodel.size()/7+1);
return box;
}
@SuppressWarnings("unchecked")
public VBox createPageP(int pageIndex)
{
int lastIndex = 0;
int displace = pmodel.size() % rowsPerPage();
if (displace > 0) {
lastIndex = pmodel.size() / rowsPerPage();
} else {
lastIndex = pmodel.size() / rowsPerPage() - 1;
}
VBox box = new VBox(7);
int page = pageIndex * itemsPerPage();
for (int i = page; i < page + itemsPerPage(); i++) {
TableView<Post> tableP = new TableView<Post>();
pFirstColumn.setCellValueFactory(
new PropertyValueFactory<Post, String>("nume"));
pFirstColumn.setMinWidth(20);
pSecondColumn.setCellValueFactory(
new PropertyValueFactory<Post, String>("tip"));
pSecondColumn.setMinWidth(160);
tableP.getColumns().addAll(pFirstColumn, pSecondColumn);
if (lastIndex == pageIndex)
{
tableP.setItems(FXCollections.observableArrayList(pmodel.subList(pageIndex * rowsPerPage(), pageIndex * rowsPerPage() + displace)));
} else {
tableP.setItems(FXCollections.observableArrayList(pmodel.subList(pageIndex * rowsPerPage(), pageIndex * rowsPerPage() + rowsPerPage())));
}
pagination1.setPageCount(pmodel.size()/7+1);
box.getChildren().add(tableP);
}
return box;
}
public VBox createPageEF(int pageIndex)
{
int lastIndex = 0;
int displace = fmodel.size() % rowsPerPage();
if (displace > 0) {
lastIndex = fmodel.size() / rowsPerPage();
} else {
lastIndex = fmodel.size() / rowsPerPage() - 1;
}
VBox box = new VBox(7);
int page = pageIndex * itemsPerPage();
for (int i = page; i < page + itemsPerPage(); i++)
{
fFirstColumn.setCellValueFactory(new Callback<CellDataFeatures<Elementfisa, String>, ObservableValue<String>>()
{
public ObservableValue<String> call(CellDataFeatures<Elementfisa, String> p)
{
if (p.getValue() != null && p.getValue().getSarcina() != null)
{
return new SimpleStringProperty(p.getValue().getSarcina().getDesc());
} else
{
return new SimpleStringProperty("Empty");
}
}
});
fSecondColumn.setCellValueFactory(new Callback<CellDataFeatures<Elementfisa, String>, ObservableValue<String>>()
{
public ObservableValue<String> call(CellDataFeatures<Elementfisa, String> p)
{
if (p.getValue() != null && p.getValue().getPost() != null)
{
return new SimpleStringProperty(p.getValue().getPost().getNume());
} else
{
return new SimpleStringProperty("Empty");
}
}
});
fFirstColumn.setMinWidth(20);
fSecondColumn.setMinWidth(160);
if (lastIndex == pageIndex) {
tableEF.setItems(FXCollections.observableArrayList(fmodel.subList(pageIndex * rowsPerPage(), pageIndex * rowsPerPage() + displace)));
} else if (lastIndex == pageIndex && lastIndex!=0 && pageIndex!=0) {
tableEF.setItems(FXCollections.observableArrayList(fmodel.subList(pageIndex * rowsPerPage(), pageIndex * rowsPerPage() + rowsPerPage())));
}
pagination3.setPageCount(fmodel.size()/7+1);
box.getChildren().add(tableEF);
}
return box;
}
private void clearFields()
{
firstTextField.setText("");
secondTextField.setText("");
thirdTextField.setText("");
}
@FXML public void handleAdd()
{
String id = firstTextField.getText();
String first = secondTextField.getText();
String sec = thirdTextField.getText();
int vid = intValidate(id);
if (vid == -1)
return;
if (currentComboBoxString.equals("Sarcina"))
{
Sarcina s = new Sarcina(Integer.parseInt(id), first);
try {
if (sservice.findOne(s.getId()) == null)
{
sservice.save(s);
showMessage(Alert.AlertType.INFORMATION, "Salvare cu succes", "Sarcina a fost adaugat!");
clearFields();
}
else
showErrorMessage("Exista deja o sarcina cu acest id !");
}catch(ValidatorException e)
{
showErrorMessage(e.getMessage());
}
}
else if (currentComboBoxString.equals("Post"))
{
Post p = new Post(Integer.parseInt(id), first, sec);
try {
if (pservice.findOne(p.getId()) == null)
{
pservice.save(p);
showMessage(Alert.AlertType.INFORMATION, "Salvare cu succes", "Post-ul a fost adaugat!");
clearFields();
}
else
showErrorMessage("Exista deja un post cu acest id !");
}catch(ValidatorException e)
{
showErrorMessage(e.getMessage());
}
}
else if (currentComboBoxString.equals("Fisa"))
{
try {
Sarcina s = sservice.findOne(Integer.parseInt(first));
Post p = pservice.findOne(Integer.parseInt(sec));
if (s == null || p == null)
{
showErrorMessage("Id-ul sarcinii sau postului nu exista !");
return;
}
Elementfisa f = new Elementfisa(Integer.parseInt(id), p, s);
if (fservice.findOne(f.getId()) == null)
{
fservice.save(f);
showMessage(Alert.AlertType.INFORMATION, "Salvare cu succes", "Fisa a fost adaugat!");
clearFields();
}
else
showErrorMessage("Exista deja o fisa cu acest id !");
} catch (ValidatorException e)
{
showErrorMessage(e.getMessage());
}
}
}
@FXML public void handleUpdate()
{
String id = firstTextField.getText();
String first = secondTextField.getText();
String sec = thirdTextField.getText();
int vid = intValidate(id);
if (vid == -1)
return;
if (currentComboBoxString.equals("Sarcina"))
{
Sarcina s = new Sarcina(Integer.parseInt(id), first);
try {
Sarcina updated = sservice.update(s);
if (updated != null)
{
showMessage(Alert.AlertType.INFORMATION, "Actualizare cu succes", "Sarcina a fost actualizata!");
clearFields();
}
else
showErrorMessage("Eroare la actualizare !");
} catch (ValidatorException e)
{
showErrorMessage(e.getMessage());
}
}
else if (currentComboBoxString.equals("Post"))
{
Post p = new Post(Integer.parseInt(id), first, sec);
try {
Post updated = pservice.update(p);
if (updated != null)
{
showMessage(Alert.AlertType.INFORMATION, "Actualizare cu succes", "Postul a fost actualizat!");
clearFields();
}
else
showErrorMessage("Eroare la actualizare !");
} catch (ValidatorException e)
{
showErrorMessage(e.getMessage());
}
}
else if (currentComboBoxString.equals("Fisa"))
{
try {
Sarcina s = sservice.findOne(Integer.parseInt(first));
Post p = pservice.findOne(Integer.parseInt(sec));
if (s == null || p == null)
{
showErrorMessage("Id-ul sarcinii sau postului nu exista !");
return;
}
Elementfisa f = new Elementfisa(Integer.parseInt(id), p, s);
Elementfisa updated = fservice.update(f);
if (updated != null)
{
showMessage(Alert.AlertType.INFORMATION, "Actualizare cu succes", "Fisa a fost actualizata!");
clearFields();
}
else
showErrorMessage("Eroare la actualizare !");
}catch (ValidatorException e)
{
showErrorMessage(e.getMessage());
}
}
}
@FXML public void handleDelete()
{
if (currentComboBoxString.equals("Sarcina"))
{
Sarcina s = (Sarcina) tableS.getSelectionModel().getSelectedItem();
try
{
sservice.delete(s);
showMessage(Alert.AlertType.INFORMATION, "Stergere cu succes", "Sarcina a fost stersa !");
clearFields();
} catch (ValidatorException e) {
showErrorMessage(e.getMessage());
}
}
else if (currentComboBoxString.equals("Post"))
{
Post p = (Post) tableP.getSelectionModel().getSelectedItem();
try
{
pservice.delete(p);
showMessage(Alert.AlertType.INFORMATION, "Stergere cu succes", "Postul a fost sters !");
clearFields();
} catch (ValidatorException e)
{
showErrorMessage(e.getMessage());
}
}
else if (currentComboBoxString.equals("Fisa"))
{
Elementfisa f = (Elementfisa) tableEF.getSelectionModel().getSelectedItem();
try
{
fservice.delete(f);
showMessage(Alert.AlertType.INFORMATION, "Stergere cu succes", "Fisa a fost stersa !");
clearFields();
} catch (ValidatorException e)
{
showErrorMessage(e.getMessage());
}
}
}
@FXML public void handleClearFields()
{
clearFields();
}
@FXML public void handleToggleButton()
{
isSelectedFC = radioButtonFirst.isSelected();
isSelectedSC = radioButtonSecond.isSelected();
}
@FXML public void handleFilterColumn()
{
String what = filterTextField.getText();
try
{
if (currentComboBoxString.equals("Sarcina"))
{
if (what.equals(""))
{
tableS.setItems(smodel);
return;
}
if (isSelectedFC)
{
showErrorMessage("N/A for Sarcina !");
return;
}
else if (isSelectedSC)
{
ObservableList<Sarcina> model = FXCollections.observableArrayList(sservice.filterSarcinaDesc(what));
tableS.setItems(model);
}
}
else if (currentComboBoxString.equals("Post"))
{
if (what.equals(""))
{
tableP.setItems(pmodel);
return;
}
if (isSelectedFC)
{
ObservableList<Post> model = FXCollections.observableArrayList(pservice.filterPostNume(what));
tableP.setItems(model);
}
else if (isSelectedSC)
{
ObservableList<Post> model = FXCollections.observableArrayList(pservice.filterPostTip(what));
tableP.setItems(model);
}
}
else if (currentComboBoxString.equals("Fisa"))
{
if (what.equals(""))
{
ComboObject.setValue("Fisa");
tableS.setItems(smodel);
return;
}
int vid = intValidate(what);
if (vid == -1)
return;
if (isSelectedFC)
{
Sarcina s = sservice.findOne(Integer.parseInt(what));
ObservableList<Sarcina> model = FXCollections.observableArrayList(fservice.filterElementSarcina(s));
ComboObject.setValue("Sarcina");
this.fillItemsOnTable(true);
tableS.setItems(model);
}
else if (isSelectedSC)
{
Post p = pservice.findOne(Integer.parseInt(what));
ObservableList<Post> model = FXCollections.observableArrayList(fservice.filterElementPost(p));
ComboObject.setValue("Fisa");
this.fillItemsOnTable(true);
tableP.setItems(model);
}
}
}
catch(ValidatorException e)
{
showErrorMessage(e.getMessage());
}
}
static void showMessage(Alert.AlertType type, String header, String text)
{
Alert message=new Alert(type);
message.setHeaderText(header);
message.setContentText(text);
message.showAndWait();
}
static void showErrorMessage(String text)
{
Alert message=new Alert(Alert.AlertType.ERROR);
message.setTitle("Mesaj eroare");
message.setContentText(text);
message.showAndWait();
}
}
Edit2:问题“JavaFX - setVisible不会”隐藏“元素”不是一个解决方案,因为对他来说,setInvisible是有效的,因为它使vBox不可见,只是不会在它的位置上移动另一个元素。另外,我已经尝试了那里提出的解决方案,没有任何效果。
另外,我注意到表Fisha(tableEF)在我更改ComboBox中的值时会隐藏自己。
private final BooleanProperty tableHidden;
public AllController()
{
this.tableHidden = new SimpleBooleanProperty(false);
this.tableEF.visibleProperty().bind(this.tableHiddenProperty());
this.tableEF.managedProperty().bind(this.tableHiddenProperty().not());
// ...
}
// Standard FX property setter/getter...
public void makeTableHidden()
{
this.setTableHidden(true);
// ...
}
当然,您可以选择没有属性并直接设置TableView
的可见
属性和managedProperty
,但我认为这样做更整洁(个人意见)。
这些是 JavaFX 属性的 getters/setters 的标准命名约定(参见示例 1)。
表格的可见性也是这种属性的一个例子;它的< code>private属性(您无法看到)称为< code>visible,它的< code>public版本(您可以看到)是< code>visibleProperty,它的getter/setter分别是< code>isVisible和< code>setVisible。
这是您需要的额外代码(我假设您已经知道如何自己生成一个)。
public final BooleanProperty tableHiddenProperty()
{
return this.tableHidden;
}
public final boolean isTableHidden()
{
return this.tableHiddenProperty().get;
}
public final void setTableHidden(final boolean value)
{
this.tableHiddenProperty().set(value);
}
问题内容: 如何隐藏HBox中的项目,并使该项目使用的空间可用于其他项目。 在上面的代码中,当图例窗格隐藏时,我希望图表节点使用所有可用空间。 问题答案: 在调用legendPane.setVisible之前,请调用: 所述Node.managed特性防止一个节点在一个场景影响其他场景节点的布局。
默认情况下,当所有窗口在JavaFX中关闭时,应用程序终止。然而,我使用了,让应用程序保留。现在,当应用程序被激活时,我将如何显示该应用程序?当应用程序从Mac Dock Bar激活时,是否有任何事件监听器可供我收听? 或者,当单击系统关闭按钮时,是否有要侦听的事件,以便我可以隐藏stage应用程序将在激活时显示?
我使用本教程在JavaFX2中创建了自己的TreeView。但是,我想隐藏我的根节点。 现在看起来是什么样子: null null null null 谢谢,木木
因此,目前我正在处理一个项目,其中我需要在一个方法中启动一个快速javafx窗口。由于我不想让类扩展应用程序,我的解决方案是创建一个扩展应用程序的内部类,并通过创建内部类的实例和调用内部类中的launch在方法中启动javafx窗口,如下所示。
假设我在JavaFX中有一个舞台。我没有按X关闭按钮来关闭窗口,而是隐藏窗口,或者说切换到计算机上的其他应用程序。无论何时我隐藏窗口或切换到PC的任何其他窗口,我都希望舞台自动关闭。 我尝试了这三种方法,但所有这些方法仅在我自己关闭窗口时激活,而不是在我隐藏窗口时激活。 任何帮助将不胜感激。谢谢。
问题内容: 我想要一个不会在单击时自动隐藏的MenuItem(更具体地说是CheckMenuItem)。我知道CustomMenuItem具有此功能,但它应该是CheckMenuItem。 问题答案: 在构造函数中使用CustomMenuItem,setHideOnClick和CheckBox。 编辑: 我只是注意到JavaFX 8u40中将其弄乱了。菜单项的文本颜色与背景颜色相同,因此看不到任何