Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.37 KB | None | 0 0
  1. package gui;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.FlowLayout;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.util.List;
  8.  
  9. import javax.swing.DefaultListModel;
  10. import javax.swing.JButton;
  11. import javax.swing.JDialog;
  12. import javax.swing.JPanel;
  13. import javax.swing.border.EmptyBorder;
  14.  
  15. import controller.ProductController;
  16. import model.ProductType;
  17.  
  18. import javax.swing.JScrollPane;
  19. import javax.swing.JList;
  20.  
  21. public class ProductList extends JDialog {
  22.  
  23.     private final JPanel contentPanel = new JPanel();
  24.     private JList<ProductType> productList = new JList<>();
  25.     private DefaultListModel<ProductType> listRepresentation;
  26.     private ProductController prodCtrl = new ProductController();
  27.  
  28.     /**
  29.      * Launch the application.
  30.      */
  31.     public static void main(String[] args) {
  32.         try {
  33.             ProductList dialog = new ProductList();
  34.             dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  35.             dialog.setVisible(true);
  36.         } catch (Exception e) {
  37.             e.printStackTrace();
  38.         }
  39.     }
  40.  
  41.     public ProductList() {
  42.         setBounds(100, 100, 450, 300);
  43.         getContentPane().setLayout(new BorderLayout());
  44.         contentPanel.setLayout(new FlowLayout());
  45.         contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
  46.         getContentPane().add(contentPanel, BorderLayout.CENTER);
  47.         {
  48.         }
  49.         {
  50.             JScrollPane scrollPane = new JScrollPane();
  51.             contentPanel.add(scrollPane);
  52.            
  53.             productList = new JList<>();
  54.             scrollPane.setViewportView(productList);
  55.         }
  56.         {
  57.             JPanel buttonPane = new JPanel();
  58.             buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
  59.             getContentPane().add(buttonPane, BorderLayout.SOUTH);
  60.             {
  61.                 JButton btnRemove = new JButton("Remove");
  62.                 buttonPane.add(btnRemove);
  63.                 btnRemove.addActionListener(new ActionListener() {
  64.                     public void actionPerformed(ActionEvent e) {
  65.                         removeProduct();
  66.                     }
  67.                 });
  68.             }
  69.             {
  70.                 JButton btnShowDetails = new JButton("Show Details");
  71.                 buttonPane.add(btnShowDetails);
  72.                 btnShowDetails.addActionListener(new ActionListener() {
  73.                     public void actionPerformed(ActionEvent e) {
  74.                         showUpdatedDetails();
  75.                     }
  76.                 });
  77.             }
  78.             {
  79.                 JButton btnCancel = new JButton("Cancel");
  80.                 btnCancel.setActionCommand("Cancel");
  81.                 buttonPane.add(btnCancel);
  82.                 btnCancel.addActionListener(new ActionListener() {
  83.                     public void actionPerformed(ActionEvent e) {
  84.                         goBack();
  85.                     }
  86.                 });
  87.                
  88.             }
  89.         }
  90.         init();
  91.     }
  92.     private void init() {
  93.         listRepresentation = new DefaultListModel<>();
  94.            
  95.         List<ProductType> dataList = prodCtrl.getAll();
  96.         for(ProductType p: dataList) {
  97.             listRepresentation.addElement(p);
  98.         }
  99.         productList.setModel(listRepresentation);
  100.        
  101.         productList.setCellRenderer(new AnotherListCell());
  102.     }
  103.    
  104.     private void goBack() {
  105.         this.setVisible(false);
  106.     }
  107.    
  108.     private void removeProduct() {
  109.         int index = productList.getSelectedIndex();
  110.         ProductType pp = productList.getSelectedValue();
  111.         if(index >= 0 && index < listRepresentation.getSize()) {
  112.             listRepresentation.remove(index);
  113.             prodCtrl.deleteProduct(pp);
  114.         }
  115.     }
  116.     private void showUpdatedDetails() {
  117.         ProductType pp = productList.getSelectedValue();
  118.         int index = productList.getSelectedIndex();
  119.        
  120.         ProductView dialog = new ProductView(pp);
  121.         dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  122.         dialog.setVisible(true);
  123.        
  124.         listRepresentation.set(index, pp);
  125.                
  126.     }
  127.  
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement