not saving .txt file inside the folder using javafx - javafx

I have created a small application but not txt file is not saving inside the folder .. it saving outside folder by the name of the folder... code is as follows.
public class FXMLDocumentController implements Initializable {
#FXML
private Label label;
#FXML
private Label label1;
#FXML
private Button button;
#FXML
private TextField txt;
#FXML
private Button save;
Stage primaryStage;
#FXML
private void handleButtonAction(ActionEvent event) {
// System.out.println("You clicked me!");
//label.setText("Hello World!");
DirectoryChooser dc= new DirectoryChooser();
File chosenDir = dc.showDialog(primaryStage);
System.out.println(""+chosenDir);
}
#FXML
private void save(ActionEvent event) throws IOException {
DirectoryChooser dc= new DirectoryChooser();
File chosenDir = dc.showDialog(primaryStage);
StringBuilder sb= new StringBuilder();
sb.append(txt.getText().toString());
File f= new File(chosenDir+"user.txt");
FileWriter f1=new FileWriter(f);
f1.write(sb.toString());
f1.close();
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}

Related

JavaFX BorderPane

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.

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")));
}
}

Get two Database Table Values to One JavaFX TreeTable

I have two classes Customer and Product.i stored this class values in database as a two table one for customer other for product. i use inner join for retrieve this data.i want to show this data in JavaFX TreeTable.there is plenty of tutorial but they only works for one class.this my code so far but it not working.im not javaFX pro.
Connection con;
PreparedStatement ps;
ResultSet rs;
#FXML
private JFXButton close;
#FXML
private JFXTreeTableView<Product> detailsTable;
#FXML
private JFXComboBox<String> searchStatus;
#FXML
private JFXComboBox<String> searchLocation;
#FXML
private TreeTableColumn<Customer, String> cname;
#FXML
private TreeTableColumn<Customer, String> cnic;
#FXML
private TreeTableColumn<Customer, String> cphone;
#FXML
private TreeTableColumn<Product, String> pbrand;
#FXML
private TreeTableColumn<Product, String> ptype;
#FXML
private TreeTableColumn<Product, Integer> prcc;
#FXML
private TreeTableColumn<Product, String> plocation;
#FXML
private TreeTableColumn<Product, String> pshoptype;
#FXML
private TreeTableColumn<String, Date> pdate;
#FXML
void OnClickSearch(KeyEvent event) {
}
#FXML
void searchByLocation(ActionEvent event) {
}
#FXML
void searchByStatus(ActionEvent event) {
}
private ObservableList<Customer> cData;
private ObservableList<Product> pData;
#Override
public void initialize(URL url, ResourceBundle rb) {
#Override
public void initialize(URL url, ResourceBundle rb) {
con= DBConnect.connect();
cData=FXCollections.observableArrayList();
pData= FXCollections.observableArrayList();
try {
rs=con.createStatement().executeQuery("SELECT c.customerName,c.customerNic,c.customerPhone,p.productBrand,p.productType,p.productRcc,p.productLocation,p.productShopType,p.productDate From customer as c INNER JOIN product as p ON c.customerId=p.customerId ");
} catch (SQLException e) {
System.out.println("GET DATA TO TABLE ERROR");
e.printStackTrace();
}
try {
while(rs.next()){
//pData.add(rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getInt(6),rs.getString(7),rs.getString(8),rs.getDate(9));
cData.add(new Customer(rs.getString(1),rs.getString(2),rs.getString(3)));
pData.add(new Product (rs.getString(4),rs.getString(5),rs.getInt(6),rs.getString(7),rs.getString(8),rs.getDate(9)));
}
} catch (SQLException e) {
System.out.println("RS.Next() Error");
e.printStackTrace();
}
cname.setCellValueFactory(new PropertyValueFactory<Customer,String>("customerNam"));
cnic.setCellValueFactory(new PropertyValueFactory<Customer,String>("nic"));
cphone.setCellValueFactory(new PropertyValueFactory<Customer,String>("phone"));
pbrand.setCellValueFactory(new PropertyValueFactory<Product,String>("brand"));
ptype.setCellValueFactory(new PropertyValueFactory<Product,String>("type"));
prcc.setCellValueFactory(new PropertyValueFactory<Product,Integer>("rccNo"));
plocation.setCellValueFactory(new PropertyValueFactory<Product,String>("location"));
pshoptype.setCellValueFactory(new PropertyValueFactory<Product,String>("shopType"));
pdate.setCellValueFactory(new PropertyValueFactory<Customer,Date>("date"));
System.out.println("For Testing");
}

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());
...
}
}

Close the login page in javafx when open a new stage

Hi I am creating a login page and a dashboard page I want to close or hide my login page and Show Dashboard When I Click on Login Button. but its not working
public class LoginController implements Initializable {
#FXML
private TextField txtUser;
#FXML
private PasswordField txtPassword;
#FXML
private Label message;
#FXML
private Label lblUser;
#FXML
private Label lblPassword;
#FXML
private void OpenDashBoard(ActionEvent event) {
try {
FxmlMethods object = new FxmlMethods();
// showFxml method usd for open a new window named DashBoard.fxml
object.showFxml("/DashBoard/DashBoard.fxml", "/ DashBoard/DashboardStyle.css", "Dash Board");
} catch (Exception exception) {
exception.printStackTrace();
}
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
And My Main Class is :
public class DATACOLLECTION extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));
stage.getIcons().add(new Image("/images/icon.png"));
Scene scene = new Scene(root);
stage.setTitle("Login");
stage.setScene(scene);
scene.getStylesheets().addAll(this.getClass().getResource("Login.css").toExternalForm());
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
please help me
I am using this and its working Thanks to all for your Response.
#FXML
private void CloseAction(ActionEvent event) {
Stage stage = (Stage) Close.getScene().getWindow();
stage.close();
}

Resources