Answer to Question #273355 in Java | JSP | JSF for Brendin

Question #273355

Develop a Java GUI application that will display the ink cartridge usage of three different printers with a time span of three years.

Q.1.1 On the form, create two combo boxes, to allow a user to select between three different printers and years. When a user has selected a printer and year and clicked the submit button, display the amount of ink cartridges used for that year. Use the following table for the printers and yearly cartridges used:


2019 Epson K750 15 22 12

2020 HP Deskjesk 650 10 35 20 Epson K750 15 22 12


Q.1.2 You are also required to create a menu system which will allow the user to exit the application under the file menu. Under the tool’s menu, allow the form submission and an option to display the yearly ink cartridges used for each printer. The layout of the form is left to your discretion. Marks will be allocated to the presentation and effectiveness of the layout, but the following layout is displayed for your convenience


1
Expert's answer
2021-11-30T00:36:51-0500
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        int[][] inkUsage = {{15, 22, 12}, {10, 35, 20}};
        ComboBox<String> printersComboBox = new ComboBox<>(FXCollections.observableArrayList("Epson K750", "HP Deskjesk 650"));
        ComboBox<String> yearsComboBox = new ComboBox<>(FXCollections.observableArrayList("2018", "2019", "2020"));
        TextField usageTextField = new TextField();
        usageTextField.setMaxWidth(50);
        usageTextField.setEditable(false);
        Button submitButton = new Button("Submit");
        submitButton.setOnMouseReleased((MouseEvent me) -> {
            if (me.getButton() == MouseButton.PRIMARY) {
                int printerIndex = printersComboBox.getSelectionModel().getSelectedIndex();
                int yearIndex = yearsComboBox.getSelectionModel().getSelectedIndex();
                if (printerIndex != -1 && yearIndex != -1) {
                    usageTextField.setText(Integer.toString(inkUsage[printerIndex][yearIndex]));
                }
            }
        });

        MenuItem exitMenuItem = new MenuItem("Exit");
        exitMenuItem.setOnAction((ActionEvent event) -> {
            System.exit(0);

        });
        MenuItem submissionMenuItem = new MenuItem("Submission");
        submissionMenuItem.setOnAction((ActionEvent event) -> {

        });
        MenuItem yearlyUsageMenuItem = new MenuItem("Yearly usage");
        yearlyUsageMenuItem.setOnAction((ActionEvent event) -> {
            StringBuilder builder = new StringBuilder();
            String[] years = {"2018", "2019", "2020"};
            for (int i = 0; i < inkUsage.length; i++) {
                int total = 0;
                for (int j = 0; j < inkUsage[i].length; j++) {
                    total += inkUsage[i][j];
                }
                builder.append(years[i]).append(" ").append(total).append('\n');
            }

            new Alert(Alert.AlertType.INFORMATION, builder.toString(), ButtonType.OK).show();
        });

        Menu fileMenu = new Menu("File");
        fileMenu.getItems().add(exitMenuItem);

        Menu toolsMenu = new Menu("Tools");
        toolsMenu.getItems().addAll(submissionMenuItem, yearlyUsageMenuItem);

        MenuBar menuBar = new MenuBar(fileMenu, toolsMenu);

        VBox comboBoxesVBox = new VBox(10, printersComboBox, yearsComboBox, usageTextField, submitButton);
        comboBoxesVBox.setAlignment(Pos.CENTER);

        BorderPane borderPane = new BorderPane();

        borderPane.setTop(menuBar);
        borderPane.setCenter(comboBoxesVBox);

        primaryStage.setScene(new Scene(borderPane, 800, 600));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS