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...
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 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.
First, launch NetBeans and close any previous projects that may be open (at the top menu...
First, launch NetBeans and close any previous projects that may be open (at the top menu go to File ==> Close All Projects). Then create a new Java application called "AverageWithMethods" (without the quotation marks) according to the following guidelines. The program prompts the user for five to ten numbers, all on one line, and separated by spaces. Then the user calculates the average of those numbers, and displays the numbers and their average to the user. The program uses...
First, launch NetBeans and close any previous projects that may be open (at the top menu...
First, launch NetBeans and close any previous projects that may be open (at the top menu go to File ==> Close All Projects). Then create a new Java application called "AtmSimulator" (without the quotation marks) (not ATMSimluator!) that simulates a simple one-transaction ATM according to the following guidelines. The program should start with an initial account balance, which you can set to any legitimate double value. All output of currency values should include a leading dollar sign and use two...
We need to create basic program that will simply display a menu and allow the user...
We need to create basic program that will simply display a menu and allow the user to select one of the choices. The menu should be displayed such that each option will be numbered, as well as have one capital letter so as to indicate that either the number or the designated letter can be entered to make their choice. Once the choice is made, the sub-menu for that selection should be displayed. Colors with an odd number of letters...
I ONLY need requirements 9&10 on the first set of requirements and then 1 &2 on...
I ONLY need requirements 9&10 on the first set of requirements and then 1 &2 on the second set of requirements please. Thank you! Endless Mountain Company manufactures a single product that is popular with outdoor recreation enthusiasts. The company sells its product to retailers throughout the northeastern quadrant of the United States. It is in the process of creating a master budget for 2019 and reports a balance sheet at December 31, 2018 as follows: Endless Mountain Company Balance...
Option A – For 30 days, I give you an amount of money to save. On...
Option A – For 30 days, I give you an amount of money to save. On day 1, I will give you $0.01. On day 2, I will double that and give you $0.02. You now have $0.03. On day 3, I will double the previous day and give you $0.04. You now have $0.07. This pattern will continue until 30 days have passed. Option B – For 30 days, I will give you $10,000 to save. Each day I...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT