4
\$\begingroup\$

I would like to review my simple piece of code. I am beginner (I learn java for 1 year) and I would like to find out whether I am on good way.

I have created simple MVC CRUD java app "ProductManager". This app should allow user manage products that are stored in database. I am using JDBC with Derby database.

I am using only one table in SQL - Product. To create this table, here is SQL query

create table "APP".PRODUCT 
(
PRODUCTID INTEGER default AUTOINCREMENT: start 1 increment 1 not null primary key,
PRODUCTORDERNUMBER VARCHAR(100),
PRODUCTDESCRIPTION VARCHAR(200),
PRODUCTWIDTH DOUBLE,
PRODUCTDEPTH DOUBLE,
PRODUCTHEIGHT DOUBLE,
PRODUCTWEIGHT DOUBLE,
PRODUCTPRICE DOUBLE
)

Model:

Product.java

package productmanager.product.model;

import java.util.Objects;

public class Product {

    private Integer productId;
    private String productOrderNumber;
    private String productDescription;
    private Double productWidth;
    private Double productDepth;
    private Double productHeight;
    private Double productWeight;
    private Double productPrice;

    public Integer getProductId() {
        return productId;
    }

    public void setProductId(Integer productId) {
        this.productId = productId;
    }

    public String getProductOrderNumber() {
        return productOrderNumber;
    }

    public void setProductOrderNumber(String productOrderNumber) {
        this.productOrderNumber = productOrderNumber;
    }

    public String getProductDescription() {
        return productDescription;
    }

    public void setProductDescription(String productDescription) {
        this.productDescription = productDescription;
    }

    public Double getProductWidth() {
        return productWidth;
    }

    public void setProductWidth(Double productWidth) {
        this.productWidth = productWidth;
    }

    public Double getProductDepth() {
        return productDepth;
    }

    public void setProductDepth(Double productDepth) {
        this.productDepth = productDepth;
    }

    public Double getProductHeight() {
        return productHeight;
    }

    public void setProductHeight(Double productHeight) {
        this.productHeight = productHeight;
    }

    public Double getProductWeight() {
        return productWeight;
    }

    public void setProductWeight(Double productWeight) {
        this.productWeight = productWeight;
    }

    public Double getProductPrice() {
        return productPrice;
    }

    public void setProductPrice(Double productPrice) {
        this.productPrice = productPrice;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 29 * hash + Objects.hashCode(this.productId);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Product other = (Product) obj;
        if (!Objects.equals(this.productId, other.productId)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Product{" + "productId=" + productId + ", productOrderNumber=" + productOrderNumber + ", productDescription=" + productDescription + ", productWidth=" + productWidth + ", productDepth=" + productDepth + ", productHeight=" + productHeight + ", productWeight=" + productWeight + ", productPrice=" + productPrice + '}';
    }

}

ProductDao.Java

package productmanager.product.model;

import java.util.ArrayList;    

public interface ProductDao {

    public void createProduct(Product product);
    public Product getProductById(int productId);
    public ArrayList<Product> getAllProducts();
    public void updateProduct(Product product);
    public void deleteProduct(int productId);

}

ProductDaoImpl.java

package productmanager.product.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import productmanager.database.ConnectToDatabase;

public class ProductDaoImpl implements ProductDao {

    private final Connection conn = ConnectToDatabase.createConnection();
    private final String SQL_CREATE_PRODUCT = "INSERT INTO product (productOrderNumber, productDescription, productWidth, productDepth, productHeight, productWeight, productPrice) VALUES (?, ?, ?, ?, ?, ?, ?)";
    private final String SQL_GET_PRODUCT_BY_ID = "SELECT * FROM product WHERE productId=?";
    private final String SQL_GET_ALL_PRODUCTS = "SELECT * FROM product";
    private final String SQL_UPDATE_PRODUCT = "UPDATE product SET productOrderNumber=?, productDescription=?, productWidth=?, productDepth=?, productHeight=?, productWeight=?, productPrice=? WHERE productId=?";
    private final String SQL_DELETE_PRODUCT = "DELETE FROM product WHERE productId=?";

    @Override
    public void createProduct(Product product) {
        try (PreparedStatement pstmt = conn.prepareStatement(SQL_CREATE_PRODUCT, Statement.RETURN_GENERATED_KEYS)) {
            pstmt.setString(1, product.getProductOrderNumber());
            pstmt.setString(2, product.getProductDescription());
            pstmt.setDouble(3, product.getProductWidth());
            pstmt.setDouble(4, product.getProductDepth());
            pstmt.setDouble(5, product.getProductHeight());
            pstmt.setDouble(6, product.getProductWeight());
            pstmt.setDouble(7, product.getProductPrice());
            pstmt.executeUpdate();
            try (ResultSet generatedKeys = pstmt.getGeneratedKeys()) {
                if (generatedKeys.next()) {
                    product.setProductId(generatedKeys.getInt(1));
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(ProductDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public Product getProductById(int productId) {
        Product product = new Product();
        try (PreparedStatement pstmt = conn.prepareStatement(SQL_GET_PRODUCT_BY_ID)) {
            pstmt.setInt(1, productId);
            try (ResultSet rs = pstmt.executeQuery()) {
                while (rs.next()) {
                    product.setProductId(rs.getInt(1));
                    product.setProductOrderNumber(rs.getString(2));
                    product.setProductDescription(rs.getString(3));
                    product.setProductWidth(rs.getDouble(4));
                    product.setProductDepth(rs.getDouble(5));
                    product.setProductHeight(rs.getDouble(6));
                    product.setProductWeight(rs.getDouble(7));
                    product.setProductPrice(rs.getDouble(8));
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(ProductDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return product;
    }

    @Override
    public ArrayList<Product> getAllProducts() {
        ArrayList<Product> allProducts = new ArrayList();
        try (PreparedStatement pstmt = conn.prepareStatement(SQL_GET_ALL_PRODUCTS);
                ResultSet rs = pstmt.executeQuery()) {
            while (rs.next()) {
                Product product = new Product();
                product.setProductId(rs.getInt(1));
                product.setProductOrderNumber(rs.getString(2));
                product.setProductDescription(rs.getString(3));
                product.setProductWidth(rs.getDouble(4));
                product.setProductDepth(rs.getDouble(5));
                product.setProductHeight(rs.getDouble(6));
                product.setProductWeight(rs.getDouble(7));
                product.setProductPrice(rs.getDouble(8));
                allProducts.add(product);
            }
        } catch (SQLException ex) {
            Logger.getLogger(ProductDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return allProducts;
    }

    @Override
    public void updateProduct(Product product) {
        try (PreparedStatement pstmt = conn.prepareStatement(SQL_UPDATE_PRODUCT)) {
            pstmt.setString(1, product.getProductOrderNumber());
            pstmt.setString(2, product.getProductDescription());
            pstmt.setDouble(3, product.getProductWidth());
            pstmt.setDouble(4, product.getProductDepth());
            pstmt.setDouble(5, product.getProductHeight());
            pstmt.setDouble(6, product.getProductWeight());
            pstmt.setDouble(7, product.getProductPrice());
            pstmt.setInt(8, product.getProductId());
            pstmt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(ProductDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void deleteProduct(int productId) {
        try (PreparedStatement pstmt = conn.prepareStatement(SQL_DELETE_PRODUCT)) {            
            pstmt.setInt(1, productId);
            pstmt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(ProductDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

ProductService.java

package productmanager.product.model;

import java.util.ArrayList;


public class ProductService {

    private final ProductDaoImpl productDaoImpl = new ProductDaoImpl();

    public void createProduct(Product product) {
        productDaoImpl.createProduct(product);
    }

    public Product getProductById(int productId) {
        return productDaoImpl.getProductById(productId);
    }

    public ArrayList<Product> getAllProducts() {
        return productDaoImpl.getAllProducts();
    }

    public void updateProduct(Product product) {
        productDaoImpl.updateProduct(product);
    }

    public void deleteProduct(int productId) {
        productDaoImpl.deleteProduct(productId);
    }

}

View:

AllProductsTableModel.java

package productmanager.product.view;

import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;
import productmanager.product.model.Product;
import productmanager.product.model.ProductService;


public class AllProductsTableModel extends DefaultTableModel {

    private final String[] columnNames = {"productId", "#", "Order number", "Description", "Width", "Depth", "Height", "Weight", "Price"};
    private final ProductService productService = new ProductService();
    private final ArrayList<Product> allProducts = productService.getAllProducts();

    public AllProductsTableModel() {
        addColumnNames();
        addData();
    }

    public void addColumnNames() {
        for (String columnName : columnNames) {
            super.addColumn(columnName);
        }
    }

    public void addData() {
        for (Product product : allProducts) {
            Object[] productData = {
                product.getProductId(),
                Boolean.FALSE,
                product.getProductOrderNumber(),
                product.getProductDescription(),
                product.getProductWidth(),
                product.getProductDepth(),
                product.getProductHeight(),
                product.getProductWeight(),
                product.getProductPrice()
            };
            super.addRow(productData);
        }
    }

    public void addProductDataToRow(Product product) {
        Object[] productData = {
                product.getProductId(),
                Boolean.FALSE,
                product.getProductOrderNumber(),
                product.getProductDescription(),
                product.getProductWidth(),
                product.getProductDepth(),
                product.getProductHeight(),
                product.getProductWeight(),
                product.getProductPrice()
            };
            super.addRow(productData);
    }

    public void updateProductDataInRow(Product product) {
        Object[] productData = {                
                product.getProductOrderNumber(),
                product.getProductDescription(),
                product.getProductWidth(),
                product.getProductDepth(),
                product.getProductHeight(),
                product.getProductWeight(),
                product.getProductPrice()
            };
        for (int i = 0; i < getRowCount(); i++) {
            if (product.getProductId().equals((int) getValueAt(i, 0))) {
                for (int j = 0; j < productData.length; j++) {
                    super.setValueAt(productData[j], i, j+2);
                }
            }
        }
    }

    @Override
    public boolean isCellEditable(int row, int column) {
        return column == 1;
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
            case 1:
                return Boolean.class;
            default:
                return String.class;
        }
    }  

}

ProductMainPanel.java

package productmanager.product.view;

import java.awt.CardLayout;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import javax.swing.*;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableColumnModel;
import net.miginfocom.swing.MigLayout;


public class ProductMainPanel extends JPanel {

    private final CardLayout cardLayout;

    private JButton createProductBtn;
    private JButton deleteProductBtn;

    private JTable allProductsTable;

    public ProductMainPanel() {
        cardLayout = new CardLayout();
        setLayout(cardLayout);
        add(createMainTablePanel(), "tablePanel");
    }


    private JPanel createMainTablePanel() {
        JPanel mainTablePanel = new JPanel(new MigLayout("wrap", "grow, fill", "[][grow, fill]"));        
        mainTablePanel.add(createControlTablePanel());
        mainTablePanel.add(createTablePanel());        
        return mainTablePanel;
    }

    private JPanel createControlTablePanel() {
        JPanel controlTablePanel = new JPanel(new MigLayout());
        createProductBtn = new JButton("New product");
        controlTablePanel.add(createProductBtn);
        deleteProductBtn = new JButton("Delete");
        deleteProductBtn.setEnabled(false);
        controlTablePanel.add(deleteProductBtn);
        return controlTablePanel;
    }

    private JPanel createTablePanel() {        
        JPanel tablePanel = new JPanel(new MigLayout("", "grow, fill", "grow, fill"));
        AllProductsTableModel allProductsTableModel = new AllProductsTableModel();
        allProductsTable = new JTable(allProductsTableModel);
        TableColumnModel tcm = allProductsTable.getColumnModel();
        tcm.removeColumn(tcm.getColumn(0));
        allProductsTable.setFillsViewportHeight(true);
        JScrollPane tablePane = new JScrollPane(allProductsTable);
        tablePanel.add(tablePane);
        return tablePanel;
    }

    public JButton getCreateProductBtn() {
        return createProductBtn;
    }

    public JButton getDeleteProductBtn() {
        return deleteProductBtn;
    }

    public JTable getAllProductsTable() {
        return allProductsTable;
    }

    public void addCreateProductBtnActionListener(ActionListener al) {
        createProductBtn.addActionListener(al);
    }

    public void addDeleteProductBtnActionListener(ActionListener al) {
        deleteProductBtn.addActionListener(al);
    }

    public void addDeleteBtnEnabler(TableModelListener tm) {
        allProductsTable.getModel().addTableModelListener(tm);
    }

    public void addDoubleClickListenerToAllProductsTable(MouseAdapter ma) {
        allProductsTable.addMouseListener(ma);
    }

    public void addPanelToMainPanel(JPanel panel, String panelKey) {
        add(panel, panelKey);        
    }

    public void changePanel(String panelKey) {
        cardLayout.show(this, panelKey);
    }

}

ProductNewPanel.java

package productmanager.product.view;

import java.awt.event.ActionListener;
import javax.swing.*;
import net.miginfocom.swing.MigLayout;

public class ProductNewPanel extends JPanel {

    private JTextField productOrderNumberField;
    private JTextField productDescriptionField;
    private JTextField productWidthField;
    private JTextField productDepthField;
    private JTextField productHeightField;
    private JTextField productWeightField;
    private JTextField productPriceField;

    private JButton saveProductBtn;
    private JButton saveAndNewProductBtn;
    private JButton saveAndCloseProductBtn;
    private JButton closeProductBtn;

    public ProductNewPanel() {
        setLayout(new MigLayout("wrap", "grow, fill", ""));
        add(createTitlePanel());
        add(createProductFormPanel());
        add(createProductFormButtonsPanel());
    }

    private JPanel createTitlePanel() {
        JPanel titlePanel = new JPanel(new MigLayout("center"));
        JLabel titleLabel = new JLabel("New product:");        
        titleLabel.setFont(titleLabel.getFont().deriveFont(20f));
        titlePanel.add(titleLabel);
        return titlePanel;
    }

    private JPanel createProductFormPanel() {
        JPanel productFormPanel = new JPanel(new MigLayout("wrap 2", "[30%][grow, fill]", ""));
        productFormPanel.add(new JLabel("Order number:"));
        productOrderNumberField = new JTextField();
        productFormPanel.add(productOrderNumberField);
        productFormPanel.add(new JLabel("Description:"));
        productDescriptionField = new JTextField();
        productFormPanel.add(productDescriptionField);
        productFormPanel.add(new JLabel("Width:"));
        productWidthField = new JTextField();
        productFormPanel.add(productWidthField);
        productFormPanel.add(new JLabel("Depth:"));
        productDepthField = new JTextField();
        productFormPanel.add(productDepthField);
        productFormPanel.add(new JLabel("Height:"));
        productHeightField = new JTextField();
        productFormPanel.add(productHeightField);
        productFormPanel.add(new JLabel("Weight:"));
        productWeightField = new JTextField();
        productFormPanel.add(productWeightField);
        productFormPanel.add(new JLabel("Price:"));
        productPriceField = new JTextField();
        productFormPanel.add(productPriceField);
        return productFormPanel;
    }

    private JPanel createProductFormButtonsPanel() {
        JPanel productFormButtonsPanel = new JPanel(new MigLayout("center"));
        saveProductBtn = new JButton("Save");
        productFormButtonsPanel.add(saveProductBtn);
        saveAndNewProductBtn = new JButton("Save and new");
        productFormButtonsPanel.add(saveAndNewProductBtn);
        saveAndCloseProductBtn = new JButton("Save and close");
        productFormButtonsPanel.add(saveAndCloseProductBtn);
        closeProductBtn = new JButton("Close");
        productFormButtonsPanel.add(closeProductBtn);
        return productFormButtonsPanel;
    }

    public String getProductOrderNumberFieldText() {
        return productOrderNumberField.getText();
    }

    public String getProductDescriptionFieldText() {
        return productDescriptionField.getText();
    }

    public String getProductWidthFieldText() {
        return productWidthField.getText();
    }

    public String getProductDepthFieldText() {
        return productDepthField.getText();
    }

    public String getProductHeightFieldText() {
        return productHeightField.getText();
    }

    public String getProductWeightFieldText() {
        return productWeightField.getText();
    }

    public String getProductPriceFieldText() {
        return productPriceField.getText();
    }

    public void addSaveProductBtnActionListener(ActionListener al) {
        saveProductBtn.addActionListener(al);
    }

    public void addSaveAndNewProductBtnActionListener(ActionListener al) {
        saveAndNewProductBtn.addActionListener(al);
    }

    public void addSaveAndCloseProductBtnActionListener(ActionListener al) {
        saveAndCloseProductBtn.addActionListener(al);
    }

    public void addCloseProductBtnActionListener(ActionListener al) {
        closeProductBtn.addActionListener(al);
    }

}

ProductDetailPanel.java

package productmanager.product.view;

import java.awt.event.ActionListener;
import javax.swing.*;
import net.miginfocom.swing.MigLayout;
import productmanager.product.model.Product;

public class ProductDetailPanel extends JPanel {

    private final Product product;

    private JTextField productOrderNumberField;
    private JTextField productDescriptionField;
    private JTextField productWidthField;
    private JTextField productDepthField;
    private JTextField productHeightField;
    private JTextField productWeightField;
    private JTextField productPriceField;

    private JButton saveProductBtn;
    private JButton saveAndNewProductBtn;
    private JButton saveAndCloseProductBtn;
    private JButton closeProductBtn;

    public ProductDetailPanel(Product product) {
        this.product = product;
        setLayout(new MigLayout("wrap", "grow, fill", ""));
        add(createTitlePanel());
        add(createProductFormPanel());
        add(createProductFormButtonsPanel());
    }

    private JPanel createTitlePanel() {
        JPanel titlePanel = new JPanel(new MigLayout("center"));
        JLabel titleLabel = new JLabel("Product detail:");        
        titleLabel.setFont(titleLabel.getFont().deriveFont(20f));
        titlePanel.add(titleLabel);
        return titlePanel;
    }

    private JPanel createProductFormPanel() {
        JPanel productFormPanel = new JPanel(new MigLayout("wrap 2", "[30%][grow, fill]", ""));
        productFormPanel.add(new JLabel("Order number:"));
        productOrderNumberField = new JTextField();
        productOrderNumberField.setText(product.getProductOrderNumber());
        productFormPanel.add(productOrderNumberField);
        productFormPanel.add(new JLabel("Description:"));
        productDescriptionField = new JTextField();
        productDescriptionField.setText(product.getProductDescription());
        productFormPanel.add(productDescriptionField);
        productFormPanel.add(new JLabel("Width:"));
        productWidthField = new JTextField();
        productWidthField.setText(String.valueOf(product.getProductWidth()));
        productFormPanel.add(productWidthField);
        productFormPanel.add(new JLabel("Depth:"));
        productDepthField = new JTextField();
        productDepthField.setText(String.valueOf(product.getProductDepth()));
        productFormPanel.add(productDepthField);
        productFormPanel.add(new JLabel("Height:"));
        productHeightField = new JTextField();
        productHeightField.setText(String.valueOf(product.getProductHeight()));
        productFormPanel.add(productHeightField);
        productFormPanel.add(new JLabel("Weight:"));
        productWeightField = new JTextField();
        productWeightField.setText(String.valueOf(product.getProductWeight()));
        productFormPanel.add(productWeightField);
        productFormPanel.add(new JLabel("Price:"));
        productPriceField = new JTextField();
        productPriceField.setText(String.valueOf(product.getProductPrice()));
        productFormPanel.add(productPriceField);
        return productFormPanel;
    }

    private JPanel createProductFormButtonsPanel() {
        JPanel productFormButtonsPanel = new JPanel(new MigLayout("center"));
        saveProductBtn = new JButton("Save");
        productFormButtonsPanel.add(saveProductBtn);
        saveAndNewProductBtn = new JButton("Save and new");
        productFormButtonsPanel.add(saveAndNewProductBtn);
        saveAndCloseProductBtn = new JButton("Save and close");
        productFormButtonsPanel.add(saveAndCloseProductBtn);
        closeProductBtn = new JButton("Close");
        productFormButtonsPanel.add(closeProductBtn);
        return productFormButtonsPanel;
    }

    public String getProductOrderNumberFieldText() {
        return productOrderNumberField.getText();
    }

    public String getProductDescriptionFieldText() {
        return productDescriptionField.getText();
    }

    public String getProductWidthFieldText() {
        return productWidthField.getText();
    }

    public String getProductDepthFieldText() {
        return productDepthField.getText();
    }

    public String getProductHeightFieldText() {
        return productHeightField.getText();
    }

    public String getProductWeightFieldText() {
        return productWeightField.getText();
    }

    public String getProductPriceFieldText() {
        return productPriceField.getText();
    }

    public void addSaveProductBtnActionListener(ActionListener al) {
        saveProductBtn.addActionListener(al);
    }

    public void addSaveAndNewProductBtnActionListener(ActionListener al) {
        saveAndNewProductBtn.addActionListener(al);
    }

    public void addSaveAndCloseProductBtnActionListener(ActionListener al) {
        saveAndCloseProductBtn.addActionListener(al);
    }

    public void addCloseProductBtnActionListener(ActionListener al) {
        closeProductBtn.addActionListener(al);
    }

}

Controller:

ProductMainController.java

package productmanager.product.controller;

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import productmanager.product.model.Product;
import productmanager.product.model.ProductService;
import productmanager.product.view.AllProductsTableModel;
import productmanager.product.view.ProductDetailPanel;
import productmanager.product.view.ProductMainPanel;
import productmanager.product.view.ProductNewPanel;

public class ProductMainController {

    private final ProductMainPanel productMainPanel;

    public ProductMainController(ProductMainPanel productMainPanel) {
        this.productMainPanel = productMainPanel;
        ControlTablePanelController controlTablePanelController  = new ControlTablePanelController();
        TablePanelController tablePanelController = new TablePanelController();
    }

    private class ControlTablePanelController {

        public ControlTablePanelController() {
            productMainPanel.addDeleteBtnEnabler(new DeleteBtnEnabler());
            productMainPanel.addCreateProductBtnActionListener(new ControlTablePanelBtnsCotroller(0));
            productMainPanel.addDeleteProductBtnActionListener(new ControlTablePanelBtnsCotroller(1));
        }

        private class ControlTablePanelBtnsCotroller implements ActionListener {

            private final int actionListenerNum;

            public ControlTablePanelBtnsCotroller(int actionListenerNum) {
                this.actionListenerNum = actionListenerNum;
            }

            @Override
            public void actionPerformed(ActionEvent e) {                
                switch (actionListenerNum) {
                    case 0:
                        ProductNewPanel productNewPanel = new ProductNewPanel();
                        productMainPanel.addPanelToMainPanel(productNewPanel, "productNewPanel");
                        productMainPanel.changePanel("productNewPanel");
                        ProductNewController productNewController = new ProductNewController(productNewPanel, productMainPanel);
                        break;
                    case 1:
                        JTable allProductsTable = productMainPanel.getAllProductsTable();
                        AllProductsTableModel allProductsTableModel = (AllProductsTableModel) allProductsTable.getModel();
                        for (int i = allProductsTableModel.getRowCount() - 1; i >= 0; i--) {
                            boolean checked = (boolean) allProductsTableModel.getValueAt(i, 1);
                            if (checked) {
                                int productId = (int) allProductsTableModel.getValueAt(i, 0);
                                ProductService productService = new ProductService();
                                productService.deleteProduct(productId);
                                allProductsTableModel.removeRow(i);
                            }
                        }
                        break;
                }

            }

        }

        private class DeleteBtnEnabler implements TableModelListener {

            @Override
            public void tableChanged(TableModelEvent e) {
                JButton deleteProductBtn = productMainPanel.getDeleteProductBtn();
                if (isAnyRowChecked()) {
                    deleteProductBtn.setEnabled(true);
                } else {
                    deleteProductBtn.setEnabled(false);
                }
            }

            private boolean isAnyRowChecked() {
                JTable allProductsTable = productMainPanel.getAllProductsTable();
                AllProductsTableModel allProductsTableModel = (AllProductsTableModel) allProductsTable.getModel();
                for (int i = 0; i < allProductsTableModel.getRowCount(); i++) {
                    boolean checked = (boolean) allProductsTableModel.getValueAt(i, 1);
                    if (checked) {
                        return true;
                    }
                }
                return false;
            }

        }

    }

    private class TablePanelController extends MouseAdapter{

        public TablePanelController() {
            productMainPanel.addDoubleClickListenerToAllProductsTable(this);
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            JTable allProductsTable = productMainPanel.getAllProductsTable();
            Point p = e.getPoint();
            int row = allProductsTable.rowAtPoint(p);
            int column = allProductsTable.columnAtPoint(p);
            if (row != -1 && column !=0 && e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e)) {
                AllProductsTableModel allProductsTableModel = (AllProductsTableModel) allProductsTable.getModel();
                int productId = (int) allProductsTableModel.getValueAt(row, 0);
                ProductService productService = new ProductService();
                Product product = productService.getProductById(productId);
                ProductDetailPanel productDetailPanel = new ProductDetailPanel(product);
                productMainPanel.addPanelToMainPanel(productDetailPanel, "productDetailPanel");
                productMainPanel.changePanel("productDetailPanel");
                ProductDetailController productDetailController = new ProductDetailController(product, productDetailPanel, productMainPanel);
            }
        }        

    }
}

ProductNewController.java

package productmanager.product.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTable;
import productmanager.product.model.Product;
import productmanager.product.model.ProductService;
import productmanager.product.view.AllProductsTableModel;
import productmanager.product.view.ProductDetailPanel;
import productmanager.product.view.ProductMainPanel;
import productmanager.product.view.ProductNewPanel;

public class ProductNewController {

    private final ProductNewPanel productNewPanel;
    private final ProductMainPanel productMainPanel;

    public ProductNewController(ProductNewPanel productNewPanel, ProductMainPanel productMainPanel) {
        this.productNewPanel = productNewPanel;
        this.productMainPanel = productMainPanel;
        ProductFormButtonsPanelController productFormButtonsPanelController = new ProductFormButtonsPanelController();
    }

    private class ProductFormButtonsPanelController {

        public ProductFormButtonsPanelController() {
            productNewPanel.addSaveProductBtnActionListener(new FormSaveButtonsActionListener(0));
            productNewPanel.addSaveAndNewProductBtnActionListener(new FormSaveButtonsActionListener(1));
            productNewPanel.addSaveAndCloseProductBtnActionListener(new FormSaveButtonsActionListener(2));
            productNewPanel.addCloseProductBtnActionListener(new FormCloseButtonActionListener());
        }

        private class FormSaveButtonsActionListener implements ActionListener {

            private final int actionListenerNum;

            public FormSaveButtonsActionListener(int actionListenerNum) {
                this.actionListenerNum = actionListenerNum;
            }

            @Override
            public void actionPerformed(ActionEvent e) {
                Product product = new Product();
                product.setProductOrderNumber(productNewPanel.getProductOrderNumberFieldText());
                product.setProductDescription(productNewPanel.getProductDescriptionFieldText());
                product.setProductWidth(Double.parseDouble(productNewPanel.getProductWidthFieldText()));
                product.setProductDepth(Double.parseDouble(productNewPanel.getProductDepthFieldText()));
                product.setProductHeight(Double.parseDouble(productNewPanel.getProductHeightFieldText()));
                product.setProductWeight(Double.parseDouble(productNewPanel.getProductWeightFieldText()));
                product.setProductPrice(Double.parseDouble(productNewPanel.getProductPriceFieldText()));
                ProductService productService = new ProductService();
                productService.createProduct(product);
                JTable allProductsTable = productMainPanel.getAllProductsTable();
                AllProductsTableModel allProductsTableModel = (AllProductsTableModel) allProductsTable.getModel();
                allProductsTableModel.addProductDataToRow(product);
                switch(actionListenerNum) {
                    case 0:
                        ProductDetailPanel productDetailPanel = new ProductDetailPanel(product);
                        productMainPanel.addPanelToMainPanel(productDetailPanel, "productDetailPanel");
                        productMainPanel.changePanel("productDetailPanel");
                        ProductDetailController productDetailController = new ProductDetailController(product, productDetailPanel, productMainPanel);
                        break;
                    case 1:
                        ProductNewPanel productNewPanel = new ProductNewPanel();
                        productMainPanel.addPanelToMainPanel(productNewPanel, "productNewPanel");
                        productMainPanel.changePanel("productNewPanel");
                        ProductNewController productNewController = new ProductNewController(productNewPanel, productMainPanel);
                        break;
                    case 2:
                        productMainPanel.changePanel("tablePanel");
                        break;
                }
            }

        }

        private class FormCloseButtonActionListener implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent e) {
                productMainPanel.changePanel("tablePanel");
            }

        }

    }

}

ProductDetailController.java

package productmanager.product.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTable;
import productmanager.product.model.Product;
import productmanager.product.model.ProductService;
import productmanager.product.view.AllProductsTableModel;
import productmanager.product.view.ProductDetailPanel;
import productmanager.product.view.ProductMainPanel;
import productmanager.product.view.ProductNewPanel;

public class ProductDetailController {

    private final Product product;
    private final ProductDetailPanel productDetailPanel;
    private final ProductMainPanel productMainPanel;

    public ProductDetailController(Product product, ProductDetailPanel productDetailPanel, ProductMainPanel productMainPanel) {
        this.product = product;
        this.productDetailPanel = productDetailPanel;
        this.productMainPanel = productMainPanel;
        ProductFormButtonsPanelController productFormButtonsPanelController = new ProductFormButtonsPanelController();
    }

    private class ProductFormButtonsPanelController {

        public ProductFormButtonsPanelController() {
            productDetailPanel.addSaveProductBtnActionListener(new FormSaveButtonsActionListener(0));
            productDetailPanel.addSaveAndNewProductBtnActionListener(new FormSaveButtonsActionListener(1));
            productDetailPanel.addSaveAndCloseProductBtnActionListener(new FormSaveButtonsActionListener(2));
            productDetailPanel.addCloseProductBtnActionListener(new FormCloseButtonActionListener());
        }

        private class FormSaveButtonsActionListener implements ActionListener {

            private final int actionListenerNum;

            public FormSaveButtonsActionListener(int actionListenerNum) {
                this.actionListenerNum = actionListenerNum;
            }

            @Override
            public void actionPerformed(ActionEvent e) {                
                product.setProductOrderNumber(productDetailPanel.getProductOrderNumberFieldText());
                product.setProductDescription(productDetailPanel.getProductDescriptionFieldText());
                product.setProductWidth(Double.parseDouble(productDetailPanel.getProductWidthFieldText()));
                product.setProductDepth(Double.parseDouble(productDetailPanel.getProductDepthFieldText()));
                product.setProductHeight(Double.parseDouble(productDetailPanel.getProductHeightFieldText()));
                product.setProductWeight(Double.parseDouble(productDetailPanel.getProductWeightFieldText()));
                product.setProductPrice(Double.parseDouble(productDetailPanel.getProductPriceFieldText()));
                ProductService productService = new ProductService();
                productService.updateProduct(product);
                JTable allProductsTable = productMainPanel.getAllProductsTable();
                AllProductsTableModel allProductsTableModel = (AllProductsTableModel) allProductsTable.getModel();
                allProductsTableModel.updateProductDataInRow(product);
                switch (actionListenerNum) {     
                    case 0:

                        break;
                    case 1:
                        ProductNewPanel productNewPanel = new ProductNewPanel();
                        productMainPanel.addPanelToMainPanel(productNewPanel, "productNewPanel");
                        productMainPanel.changePanel("productNewPanel");
                        ProductNewController productNewController = new ProductNewController(productNewPanel, productMainPanel);
                        break;
                    case 2:
                        productMainPanel.changePanel("tablePanel");
                        break;
                }
            }

        }

        private class FormCloseButtonActionListener implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent e) {
                productMainPanel.changePanel("tablePanel");
            }

        }

    }

}

And here is how I connect to Derby database:

ConnectToDatabase.java

package productmanager.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;


public class ConnectToDatabase {

    public static Connection createConnection() {
        String dbUrl = "jdbc:derby://localhost:1527/ProductManagerDb";
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(dbUrl);
        } catch (SQLException ex) {
            Logger.getLogger(ConnectToDatabase.class.getName()).log(Level.SEVERE, null, ex);
        }
        return conn;
    }

}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.