JavaFX BorderPane - javafx

i'm trying to set on top of a borderpane an fxml hbox, the borderpane is inside another borderpane that contains all.
The containers are in this form:
Stage-> PincipalBorderPane -> Vbox(Center of MainMenuBorderPane)-> ContentBorderPane(in bottom of Vbox).
Also in the left of my PrincipalBorderPane, I have a sidebar menu that contains buttons, I want to set on the top of ContentBorderPane an fxml document that will be like an small menu to show buttons that would set on the bottom of ContentMenu different panes. The problem is that I don't know how to set an fxml to a borderpane that is inside another borderpane.
public class MenuContentController implements Initializable {
private boolean flag = true;
private boolean flagsc = true;
#FXML
private JFXButton Minbtn;
#FXML
private JFXButton Maxbtn;
#FXML
private JFXButton Closebtn;
#FXML
private JFXButton Sidebarbtn;
#FXML
private JFXTextField Searchtxt;
#FXML
private JFXButton Ingressbtn;
#FXML
private JFXButton Egressbtn;
#FXML
private JFXButton Statusbtn;
#FXML
private BorderPane ContentTools;
#FXML
private void open_sidebar(ActionEvent event) throws IOException {
BorderPane border_pane = (BorderPane) ((Node) event.getSource()).getScene().getRoot();
if (flag) {
//Revisar animación más adelante
Parent sidebar = FXMLLoader.load(getClass().getResource("/app/fxml/Sidebar.fxml"));
sidebar.translateXProperty();
//
border_pane.setLeft(sidebar);
//
Timeline timeline = new Timeline();
KeyValue kv = new KeyValue(sidebar.translateXProperty(),0,Interpolator.EASE_IN);
KeyFrame kf = new KeyFrame(Duration.seconds(10), kv);
timeline.getKeyFrames().add(kf);
timeline.play();
flag = false;
} else {
border_pane.setLeft(null);
flag = true;
}
}
**#FXML
public void open_admintools_priv(ActionEvent event) throws IOException{
ContentTools = new BorderPane();
BorderPane bp = (BorderPane) ContentTools;
if (flagsc) {
Parent admintools = FXMLLoader.load(getClass().getResource("/app/fxml/AdminTools.fxml"));
bp.setTop(admintools);
flagsc=false;
} else {
ContentTools.setTop(null);
flagsc = true;
}
System.out.println(flagsc);**
}
The method open_admintools_priv should to set visible on top of ContentTools(ContentBorderpane) the Admintools.fxml by pressing a button in another fxml(sidebar.fxml) that I set on the left of my PrincipalBorderPane with the method open_sidebar, that I call creating a new controller of MenuContentController in the SidebarController:
public class SidebarController implements Initializable {
private boolean flagsc = true;
#FXML
private JFXButton condominiobtn;
#FXML
private JFXButton adminbtn;
#FXML
private JFXButton finanzasbtn;
#FXML
private JFXButton reportesbtn;
#FXML
private JFXButton confbtn;
#FXML
private void open_admintools(ActionEvent event) throws IOException{
MenuContentController aux = new MenuContentController();
aux.open_admintools_priv(event);
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
Another problem is that by this way, at the moment I create a MenuContentController, the variable flag is always on true, this variable I use it to set the fxml in borderpane when flag is true, or quit it when flag is false. I don't know if creating an instance of a controller is the way to acces to the objects of that fxml, thanks for your time.

Related

Hide checkbox in java with Scenebuilder

I am working with eclipse jee photon and scenebuilder.
What I am trying to do:
I have a choicebox with two labels:
ObservableList labelList = FXCollections.observableArrayList("examlpe01", "example02");
If example01 is chosen, the checkbox should be visible but disabled. If example02 is chosed the checkbox should be visible and it should be possible to check it.
Here is my code:
public class newUser{
ObservableList <String> labelList = FXCollections.observableArrayList("example01", "example02");
#FXML
private TextField name;
#FXML
private TextField lastName;
#FXML
private Button saveUser;
#FXML
private CheckBox adminRights;
#FXML
private TextField emailAdress;
#FXML
private TextField password;
#FXML
private Button userSpeichern;
#FXML
private MenuButton userDropDown;
#FXML
private MenuItem adminpage;
#FXML
private MenuItem example01;
#FXML
private MenuItem example02;
#FXML
private MenuItem logout;
#FXML
private ChoiceBox label;
#FXML
private void initialize() {
label.setItems(labelList);
label.setValue("mm");
}
#FXML
void logout(ActionEvent event) throws IOException {
Parent Login = FXMLLoader.load(getClass().getClassLoader().getResource("fxml/Login.fxml"));
Scene Login_scene = new Scene(Login);
Stage app_stage = (Stage) userDropDown.getScene().getWindow();
app_stage.setScene(Login_scene);
app_stage.show();
}
DbHelper db = new DbHelper();
private void showCheckbox() {
if (label.equals("example02")) {
}
}
#FXML
void saveUserData(ActionEvent event) {
}
Use this:
#FXML
ChoiceBox choiceBox;
#FXML
CheckBox checkBox;
#FXML
private void choiceBoxOnClicked() {
if (choiceBox.getValue().equals("example01")) {
checkBox.setDisable(true);
} else if (choiceBox.getValue().equals("example02")) {
checkBox.setDisable(false);
}
}
So this should work.

How to load image in ImageView dynamically in Java FX

I'm working with FXML and have two different Scene on a single Stage. btnStart is on scene1, and imgBtn is on scene2. When I click btnStart it sets scene2 to stage and loads an image to imageView (this is throwing NullPointerException). But when I click imgBtn on scene2 it is loading image.
My question is how to load image dynamically when I switch to scene2 ?
#FXML private Button imgBtn;
#FXML private Button btnStart;
#FXML public ImageView imageView;
#FXML
public void imgBtnClicked()throws Exception{
imageView.setImage(new Image(new FileInputStream("src/Assets/CardAssets/png-v2/3C.png")));
}
#FXML
public void btnStartClicked()throws Exception{
SetScene2();
imageView.setImage(new Image(new FileInputStream("src/Assets/CardAssets/png-v2/3C.png")));
}
public void SetScene2()throws Exception {
Parent root = FXMLLoader.load(getClass().getResource(fxmlFile2.fxml));
String css=getClass().getResource("myStyle.css").toExternalForm();
Scene scene;
try{
scene=new Scene(root,root.getScene().getWidth(),root.getScene().getHeight());
}
catch(NullPointerException e) {
scene=new Scene(root,stage.getWidth(),stage.getHeight());
}
scene.getStylesheets().add(css);
stage.setScene(scene);
}
The question is not exactly very clear, so I'm going to make some guesses here. The most likely problem is that you have confused which Nodes are on which scene which is on which controller.
The correct structure for this is that you have two sets of each of the following items:
FXML file
Controller class
Scene object.
This is how it should be done:
public class ControllerA {
#FXML private Button btnStart;
#FXML
public void btnStartClicked()throws Exception{
setScene2();
}
public void setScene2()throws Exception {
// You may need to set the controller to an instance of ControllerB,
// depending whether you have done so on the FXML.
Parent root = FXMLLoader.load(getClass().getResource(fxmlFile2.fxml));
String css=getClass().getResource("myStyle.css").toExternalForm();
Scene scene;
try{
scene=new Scene(root,root.getScene().getWidth(),root.getScene().getHeight());
}
catch(NullPointerException e) {
scene=new Scene(root,stage.getWidth(),stage.getHeight());
}
scene.getStylesheets().add(css);
stage.setScene(scene);
}
}
public class ControllerB {
#FXML private ImageView imageView;
#FXML private Button imgBtn;
#FXML public void initialize() {
imageView.setImage(new Image(new FileInputStream("src/Assets/CardAssets/png-v2/3C.png")));
}
#FXML
public void imgBtnClicked()throws Exception{
imageView.setImage(new Image(new FileInputStream("src/Assets/CardAssets/png-v2/3C.png")));
}
}

Setting Combobox value when switching screens

I want to switch between FXML-Files with Radiobuttons without losing the Combobox-Selection. "secSelectionModel()" seems to add a Selection, but its not visually shown.
public class Controller {
static SingleSelectionModel cbxSelection;
#FXML private ComboBox<String> cbx = new ComboBox<>();
#FXML private RadioButton radio1 = new RadioButton();
#FXML private RadioButton radio2 = new RadioButton();
#FXML private void cbxFill(){
cbx.getItems().setAll("eins","zwei");
}
#FXML private void screenSwitch() throws IOException {
Stage stage;
Parent root;
if(radio1.isSelected()){
stage=(Stage) radio1.getScene().getWindow();
root = FXMLLoader.load(getClass().getResource("sample.fxml"));
//cbxFill();
cbx.setSelectionModel(cbxSelection);
}
else{
stage=(Stage) radio2.getScene().getWindow();
root = FXMLLoader.load(getClass().getResource("sample2.fxml"));
//cbxFill();
cbx.setSelectionModel(cbxSelection);
}
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
#FXML private void getcbxSelection(){
cbxSelection=cbx.getSelectionModel();
}
}
If Radio2 is selected, I want to switch to Screen2 without losing the Entered Selection in the Combobox.

How can retrieve the values of a controller in another controller In JavaFX [duplicate]

This question already has answers here:
Passing Parameters JavaFX FXML
(10 answers)
Closed 5 years ago.
I have a TextField and a button in FXMLController and a label in SecondFXMLController
Now I want to get Value of TextField in SecondFXMLController
Note: Two fxml files will be loaded in Same stage
FXMLController:
public class FXMLDocumentController implements Initializable {
#FXML
private Label label;
#FXML
private Button button;
#FXML
private TextField firstField;
#FXML
private AnchorPane first;
private int a;
#FXML
private void handleButtonAction(ActionEvent event) throws IOException {
AnchorPane pane = FXMLLoader.load(getClass().getResource("SecondFXML.fxml"));
first.getChildren().setAll(pane);
}
public String setTo() {
System.out.println("In setTo function");
System.out.println(firstField.getText());
return firstField.getText();
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
SecondFXMLController:
public class SecondFXMLController implements Initializable
{
#FXML
private Label secondField;
private String f;
private AnchorPane pane;
private FXMLDocumentController con;
/**
* Initializes the controller class.
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
FXMLLoader fxml = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
try {
pane = fxml.load();
con = fxml.getController();
f = con.setTo();
} catch (IOException ex) {
Logger.getLogger(SecondFXMLController.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(f);
secondField.setText(f);
}
}
Now problem is that i'am not able to fetch the value of textField in setTo() function so its returning null.
NOTE: Both FXML files should be loaded in the Same Stage
Can Any one please give the Solution
Instead of returning a String from the method, I would suggest to return a StringProperty. Later you can bind this String Property to the textProperty of the Label.
public class FXMLDocumentController implements Initializable {
...
#FXML
private TextField firstField;
...
public StringProperty firstFieldTextProperty() {
return firstField.textProperty();
}
...
}
In SecondFXMLController, just bind the secondLabel's textProperty to the method.
public class SecondFXMLController implements Initializable {
#FXML
private Label secondField;
...
#Override
public void initialize(URL url, ResourceBundle rb) {
...
FXMLLoader fxml = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
pane = fxml.load();
con = fxml.getController();
secondField.textProperty.bind(con.firstFieldTextProperty());
...
}
}

JavaFX 8: change colour of a specific circle with his own id

I ve created this program that when you click on "create" button other two ButtonBar (called newNode) are created.. inside this Button bar there are a button and a circle. When all is created I would like to change the colour of these two circle when i click another Button. I ve coded something but I can just change the colour of the LAST circle.
public class Controller implements Initializable{
#FXML
private Button btnHaveFun;
#FXML
private ButtonBar ReadSMSBar;
#FXML
private ButtonBar GalleryBar;
#FXML
private ButtonBar FileExplorerBar;
#FXML
private ButtonBar SpyCamBar;
#FXML
private ButtonBar TakeAPictureBar;
#FXML
private ButtonBar TakeAScreenshotBar;
#FXML
private ButtonBar RecordAVideoBar;
#FXML
private ButtonBar SpyWhatsappBar;
#FXML
private ButtonBar KeyLoggerBar;
#FXML
private ButtonBar ScreenStreamBar;
#FXML
private ButtonBar SpyMicrophoneBar;
#FXML
private ButtonBar slaveBar;
#FXML
private VBox slaveVbox;
#FXML
private Circle statusSlave;
private ButtonBar newNode = new ButtonBar();
private Circle c= new Circle();
#Override
public void initialize(URL location, ResourceBundle resources) {
// TODO Auto-generated method stub
}
public void creat(String s){
newNode = new ButtonBar();
c= new Circle();
c.setRadius(11);
c.setStrokeWidth(1);
c.setStroke(Paint.valueOf("#ffffff"));
c.setFill(Paint.valueOf("#15ff00"));
c.setId(s);
newNode.getButtons().addAll(new Button("Mavero"),c);
slaveVbox.getChildren().addAll(newNode) ;
}
#FXML
public void newComp(){
creat("id1");
creat("id2");
}
#FXML
public void change(){
c.setFill(Color.BLACK);
}
}
You could use Node.lookup() to get the Circles using a css selector (or Node.lookupAll for multiple nodes):
void change(String oldId, String newId) {
change(oldId, Color.RED);
change(newId, Color.GREEN);
}
void change(String id, Color color){
Circle circle = (Circle) slaveVbox.lookup('#'+id);
circle.setFill(color);
}
Its simple:
Just add All Circles in a List ->
List<Circle> circles = new ArrayList<>();
public void creat(String s) {
newNode = new ButtonBar();
Circle c = new Circle();
c.setRadius(11);
c.setStrokeWidth(1);
c.setStroke(Paint.valueOf("#ffffff"));
c.setFill(Paint.valueOf("#15ff00"));
c.setId(s);
circles.add(c);
newNode.getButtons().addAll(new Button("Mavero"), c);
slaveVbox.getChildren().addAll(newNode);
}
#FXML
public void newComp() {
creat("id1");
creat("id2");
}
#FXML
public void change() {
for (Circle c : circles) {
c.setFill(Color.BLACK);
}
}

Resources