Question

In: Computer Science

i need a javafx code 1. first create menu bar with option open and save 2....

i need a javafx code
1. first create menu bar with option open and save
2. when user click on open it opens the file only image file
3. when user click on save it saves as new file.

Solutions

Expert Solution

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.paint.Color;
import javafx.scene.control.TextArea;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ScrollPane.ScrollBarPolicy;
import javafx.scene.control.Label;
import javafx.scene.control.Button;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class SaveFileChooserExample
                extends Application {

        private Text actionStatus;
        private Stage savedStage;
        private TextArea txtArea;
        private static final String titleTxt = "JavaFX File Chooser Example 2";
        private static final String defaultFileName = "MyFile.txt";

        public static void main(String [] args) {

                Application.launch(args);
        }

        @Override
        public void start(Stage primaryStage) {
        
                primaryStage.setTitle(titleTxt);        

                // Window label
                Label label = new Label("Save File Chooser");
                label.setTextFill(Color.DARKBLUE);
                label.setFont(Font.font("Calibri", FontWeight.BOLD, 36));
                HBox labelHb = new HBox();
                labelHb.setAlignment(Pos.CENTER);
                labelHb.getChildren().add(label);
                
                // Text area in a scrollpane and label
                Label txtAreaLabel = new Label("Enter text and save as a file:");
                txtAreaLabel.setFont(Font.font("Calibri", FontWeight.NORMAL, 20));
                txtArea = new TextArea();
                txtArea.setWrapText(true);
                ScrollPane scroll = new ScrollPane();
                scroll.setContent(txtArea);
                scroll.setFitToWidth(true);
                scroll.setFitToHeight(true);
                scroll.setPrefHeight(150);
                scroll.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
                scroll.setVbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);
                
                VBox txtAreaVbox = new VBox(5);
                txtAreaVbox.setPadding(new Insets(5, 5, 5, 5));
                txtAreaVbox.getChildren().addAll(txtAreaLabel, scroll);

                // Button
                Button btn1 = new Button("Save as file...");
                btn1.setOnAction(new SaveButtonListener());
                HBox buttonHb1 = new HBox(10);
                buttonHb1.setAlignment(Pos.CENTER);
                buttonHb1.getChildren().addAll(btn1);

                // Status message text
                actionStatus = new Text();
                actionStatus.setFont(Font.font("Calibri", FontWeight.NORMAL, 20));
                actionStatus.setFill(Color.FIREBRICK);

                // Vbox
                VBox vbox = new VBox(30);
                vbox.setPadding(new Insets(25, 25, 25, 25));
                vbox.getChildren().addAll(labelHb, txtAreaVbox, buttonHb1, actionStatus);

                // Scene
                Scene scene = new Scene(vbox, 600, 400); // w x h
                primaryStage.setScene(scene);
                primaryStage.show();

                savedStage = primaryStage;
        }

        private class SaveButtonListener implements EventHandler<ActionEvent> {

                @Override
                public void handle(ActionEvent e) {

                        showSaveFileChooser();
                }
        }

        private void showSaveFileChooser() {

                FileChooser fileChooser = new FileChooser();
                fileChooser.setTitle("Save file");
                fileChooser.setInitialFileName(defaultFileName);
                File savedFile = fileChooser.showSaveDialog(savedStage);

                if (savedFile != null) {

                        try {
                                saveFileRoutine(savedFile);
                        }
                        catch(IOException e) {
                        
                                e.printStackTrace();
                                actionStatus.setText("An ERROR occurred while saving the file!" +
                                                savedFile.toString());
                                return;
                        }
                        
                        actionStatus.setText("File saved: " + savedFile.toString());
                }
                else {
                        actionStatus.setText("File save cancelled.");
                }
        }

    private void saveFileRoutine(File file)
                        throws IOException{
                // Creates a new file and writes the txtArea contents into it
                String txt = txtArea.getText();
                file.createNewFile();
                FileWriter writer = new FileWriter(file);
                writer.write(txt);
                writer.close();
        }
}

Related Solutions

I need to create a code in C++ that first has a menu animation of game...
I need to create a code in C++ that first has a menu animation of game Pacman, a score label in the map, and a bar that have the lives of pacman in the map.
I need the output of the code like this in java First we create a new...
I need the output of the code like this in java First we create a new building and display the result: This building has no apartments. Press enter to continue......................... Now we add some apartments to the building and display the result: This building has the following apartments: Unit 1 3 Bedroom Rent $450 per month Currently unavailable Unit 2 2 Bedroom Rent $400 per month Currently available Unit 3 4 Bedroom Rent $1000 per month Currently unavailable Unit 4...
1. WHEN I RUN THE CODE AT FIRST AND I SELECT THE RETURN OPTION, IT BREAKS....
1. WHEN I RUN THE CODE AT FIRST AND I SELECT THE RETURN OPTION, IT BREAKS. BUT WHEN I SELCT THE RETURN OPTION AFTER I BORROW A BOOK, IT WORKS 2. WHY IS MY ARRAYLIST NO SAVING? WHAT I MEAN IS THAT AFTER I BORROW A BOOK AND I END THE SYSTEM, WHEN I RESTART IT, THE BOOK I BORROWED IS STILL AVAILABLE 3.ALSO ANY IDEAS ON WHAT MY TEST CASES COULD BE? /* * To change this license header,...
For the following code I need to open the file of a persons choice which I...
For the following code I need to open the file of a persons choice which I did. I can get it to read the file, but I need help with counting lines and characters feel like i have the code I just cant make it work. Either way, the program should then ask the user Analyze another file (y/n)? and repeat the process again (asking for a file and analyzing its meta-data). This should continue repeating until the user chooses...
Java Please comment code Create an Interactive JavaFX Application Create an application with the following controls:...
Java Please comment code Create an Interactive JavaFX Application Create an application with the following controls: A Label control with the following text displayed: "First Number:" A Label control with the following text displayed: "Second Number:" An empty TextField control beside the First Number label. An empty TextField control beside the Second Number label. Five buttons: Button 1 labeled + Button 2 labeled - Button 3 labeled * Button 4 labeled / Button 5 labeled = An empty Label control...
JAVA PROGRAMMING Hi! I need to create a calculator using do/while loop that calls a menu...
JAVA PROGRAMMING Hi! I need to create a calculator using do/while loop that calls a menu switch where the user choice the calculation type. In the switch each case calls a method to each type of calculation (addition/subtraction/division/multiply/ change set of numbers in the array) those methods sends back the result to be displayed in another method. This application needs to hold the user input's numbers as an array. Thanks for your time. It is a great help!
Part 1: Create a character array and save your first and last name in it
PROGRAMMING IN C:Part 1:Create a character array and save your first and last name in itNote: You can assign the name directly or you can use the scanf function.Display your name on the screen.Display the address (memory location) in hexadecimal notation of the array. (hint: use %p)Use a for loop to display each letter of your name on a separate line.Part 2:Create a one dimensional array and initialize it with 10 integers of your choice.Create a function and pass the...
I NEED TO CREATE A GUI INTERFACE USING TKINTER CONTAINING : Colors, Image, Button, Title bar,...
I NEED TO CREATE A GUI INTERFACE USING TKINTER CONTAINING : Colors, Image, Button, Title bar, and a Menu bar FOR THE QUESTION BELOW: PLEASE HELP PROGRAMMING LANGUAGE IS PYTHON Write a simple quiz game that has a list of ten questions and a list of answers to those questions. The game should give the player four randomly selected questions to answer. It should ask the questions one-by-one, and tell the player whether they got the question right or wrong....
I haves code on bottom. what do i need to edit? Create a subdirectory called proj1.  For...
I haves code on bottom. what do i need to edit? Create a subdirectory called proj1.  For this project you need to create at least two files: proj1.cpp, and makefile. Both files should be placed in the proj1 directory. The file proj1.cpp should contain the main function, int main(). In the main() function, the program should read the input until it reaches the end, counting the number of times each word, number, and character is used. A word is defined as a sequence of letters ('a'..'z' or 'A'..'Z')....
I need these written in shell code 1.nested loop. e.g. 1*2 + 2*3 + 3*4 +...
I need these written in shell code 1.nested loop. e.g. 1*2 + 2*3 + 3*4 + ...(n-1)*n. (Only nested loops) 2.Fibonacci numbers.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT