What is the counterpart of gridPane in Java swing? I'm trying to answer the below question but
Question:
What is the counterpart of gridPane in Java swing?
I'm trying to answer the below question but the answer that was provided to me used javafx (which I'm not familiar with):
Create a GUI application to manage product file. For each product following information is kept:
- Product id (integer and unique) Product name Product price (double and positive)
User should be able to perform the following operations:
- Add new product, only if all the valid information is provided Modify existing product using product id
Is it possible to create a very simple application? I would need to use an array list and just display all the info in the GUI, nothing very fancy.
And I use Java swing.
Many thanks!
The answer provided to me:
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */
import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.HPos; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.GridPane; import javafx.scene.text.Font; import javafx.scene.text.FontWeight; import javafx.stage.Stage;
/** * * @author HP */ public class ProductManager extends Application { //Array List for saving data private List productsList = new ArrayList<>();
/** * * @param primaryStage * @throws Exception */ @Override public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Manage Product List Java Application");
// product form grid pane GridPane productsGridPane = productFormPane();
// add controls to the product form grid pane addControls(productsGridPane);
// scene with product form grid pane as the root node also the form dimensions are provided as parameters here Scene scene = new Scene(productsGridPane, 440, 320); // set the scene in primary primaryStage primaryStage.setScene(scene); //show primaryStage primaryStage.show(); }
private GridPane productFormPane() {
// instantiate a new grid pane object GridPane gridPane = new GridPane();
// position the pane at the center gridPane.setAlignment(Pos.CENTER); gridPane.setPadding(new Insets(40, 40, 40, 40)); gridPane.setHgap(10); gridPane.setVgap(10);
return gridPane; }
private void addControls(GridPane gridPane) {
// control header using label Label header = new Label("Product Details GUI APP"); header.setFont(Font.font("Arial", FontWeight.BOLD, 24)); gridPane.add(header, 0, 0, 2, 1); //Horizontal alignment. GridPane.setHalignment(header, HPos.CENTER); GridPane.setMargin(header, new Insets(20, 0, 20, 0));
// control product id Label idLabel = new Label("Product ID: "); gridPane.add(idLabel, 0, 1); TextField idField = new TextField(); idField.setPrefHeight(40); gridPane.add(idField, 1, 1);
// control product name Label nameLabel = new Label("Product Name: "); gridPane.add(nameLabel, 0, 2); TextField nameField = new TextField(); nameField.setPrefHeight(40); gridPane.add(nameField, 1, 2);
// control product price Label priceLabel = new Label("Product Price: "); gridPane.add(priceLabel, 0, 3); TextField priceField = new TextField(); priceField.setPrefHeight(40); gridPane.add(priceField, 1, 3);
// button add new product Button addButtonObject = new Button("Add New Product"); gridPane.add(addButtonObject, 0, 4, 1, 1); GridPane.setHalignment(addButtonObject, HPos.CENTER); GridPane.setMargin(addButtonObject, new Insets(10, 0, 10, 0));
// button modify product Button modifyButtonObject = new Button("Modify Product"); gridPane.add(modifyButtonObject, 1, 4, 1, 1); GridPane.setHalignment(modifyButtonObject, HPos.CENTER); GridPane.setMargin(modifyButtonObject, new Insets(10, 0, 10, 0));
// control error message Label errorLabel = new Label(); gridPane.add(errorLabel, 0, 5);
// action for add new product button addButtonObject.setOnAction(new EventHandler() { @Override public void handle(ActionEvent event) { try { // get text field values int productId = Integer.parseInt(idField.getText()); String productName = nameField.getText(); double productPrice = Double.parseDouble(priceField.getText());
// count product count by id via list streams int countProduct = (int) productsList.stream().filter(p -> p.getProductId() == productId).count();
// add new product if doesn't exists if (countProduct == 0) { // add product to the productsList list productsList.add(new Product(productId, productName, productPrice)); errorLabel.setText("Successfully Added!"); } else errorLabel.setText("Already exists."); } catch (Exception e) { errorLabel.setText("Invalid Input!"); return; } } });
// action for modify product button modifyButtonObject.setOnAction(new EventHandler() { @Override public void handle(ActionEvent event) { try { // get text field values int productId = Integer.parseInt(idField.getText()); String productName = nameField.getText(); double productPrice = Double.parseDouble(priceField.getText());
// count product count by id via list streams int countProduct = (int) productsList.stream().filter(p -> p.getProductId() == productId).count();
// modify product if already exists if (countProduct == 1) { // get the existing product via streams List product = productsList.stream().filter(p -> p.getProductId() == productId) .collect(Collectors.toList()); // find the index of product int index = productsList.indexOf(product.get(0)); // update the list item and set modified product productsList.set(index, new Product(productId, productName, productPrice)); errorLabel.setText("Successfully Modified!"); } else errorLabel.setText("Doesn't exists."); } catch (Exception e) { errorLabel.setText("Invalid Input!"); return; } } });
}
/** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } }
Project Management The Managerial Process
ISBN: 9781260570434
8th Edition
Authors: Eric W Larson, Clifford F. Gray