SHARE
TWEET

Untitled

a guest Jun 17th, 2019 104 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package notesapplication;
  2.  
  3. import dal.User;
  4. import dal.UserRepository;
  5. import java.awt.BorderLayout;
  6. import java.awt.GridLayout;
  7. import java.sql.SQLException;
  8. import java.util.ArrayList;
  9. import javafx.scene.control.PasswordField;
  10. import javax.swing.JButton;
  11. import javax.swing.JDialog;
  12. import javax.swing.JLabel;
  13. import javax.swing.JPanel;
  14. import javax.swing.JPasswordField;
  15. import javax.swing.JScrollPane;
  16. import javax.swing.JTable;
  17. import javax.swing.JTextField;
  18. import javax.swing.table.AbstractTableModel;
  19.  
  20. public class UsersDialog extends JDialog {
  21.  
  22.     private JTextField usernameTextField;
  23.     private JPasswordField passwordField;
  24.     private JPanel buttonsPanel;
  25.     private JPanel contentPanel;
  26.     private JPanel inputPanel;
  27.  
  28.     private JButton newButton;
  29.     private JButton editButton;
  30.     private JButton deleteButton;
  31.     private JButton saveButton;
  32.  
  33.     private JTable table;
  34.     private JScrollPane scrollPane;
  35.     private ArrayList<User> users;
  36.     private UsersTableModel usersTableModel;
  37.  
  38.     public UsersDialog() throws SQLException {
  39.         setModalityType(DEFAULT_MODALITY_TYPE);
  40.         setTitle("Users");
  41.  
  42.         //Dialog
  43.         buttonsPanel = new JPanel();
  44.         add(buttonsPanel, BorderLayout.NORTH);
  45.         contentPanel = new JPanel();
  46.         contentPanel.setLayout(new BorderLayout());
  47.         add(contentPanel, BorderLayout.CENTER);
  48.  
  49.         //Content panel
  50.         inputPanel = new JPanel();
  51.         inputPanel.setLayout(new GridLayout(2, 2));
  52.         contentPanel.add(inputPanel, BorderLayout.NORTH);
  53.  
  54.         UserRepository userRepository = new UserRepository();
  55.  
  56.         users = userRepository.getAll();
  57.  
  58.         usersTableModel = new UsersTableModel();
  59.         table = new JTable(usersTableModel);
  60.         scrollPane = new JScrollPane(table);
  61.         contentPanel.add(scrollPane, BorderLayout.CENTER);
  62.  
  63.         //Buttons Panel
  64.         newButton = new JButton("New");
  65.         newButton.addActionListener((e) -> {
  66.             newButtonClicked();
  67.         });
  68.  
  69.         buttonsPanel.add(newButton);
  70.  
  71.         editButton = new JButton("Edit");
  72.         buttonsPanel.add(editButton);
  73.  
  74.         deleteButton = new JButton("Delete");
  75.         buttonsPanel.add(deleteButton);
  76.  
  77.         saveButton = new JButton("Save");
  78.         buttonsPanel.add(saveButton);
  79.  
  80.         //Input panel
  81.         inputPanel.add(new JLabel("Username: "));
  82.         usernameTextField = new JTextField();
  83.         inputPanel.add(usernameTextField);
  84.         inputPanel.add(new JLabel("password: "));
  85.         passwordField = new JPasswordField();
  86.         inputPanel.add(passwordField);
  87.         pack();
  88.  
  89.     }
  90.  
  91.     private void newButtonClicked() {
  92.         usernameTextField.setText("");
  93.         passwordField.setText("");
  94.     }
  95.  
  96.     private class UsersTableModel extends AbstractTableModel {
  97.  
  98.         private String[] columns = {"Id", "Username", "password"};
  99.  
  100.         @Override
  101.         public String getColumnName(int index) {
  102.             return columns[index]; //To change body of generated methods, choose Tools | Templates.
  103.         }
  104.  
  105.         @Override
  106.         public int getRowCount() { //kati ota row herna
  107.             return users.size();
  108.         }
  109.  
  110.         @Override
  111.         public int getColumnCount() {
  112.             return 3;
  113.         }
  114.  
  115.         @Override
  116.         public Object getValueAt(int rowIndex, int columnIndex) {
  117.             User user = users.get(rowIndex);
  118.             switch (columnIndex) {
  119.                 case 0:
  120.                     return user.id;
  121.                 case 1:
  122.                     return user.username;
  123.                 case 2:
  124.                     return user.password;
  125.             }
  126.             return null;
  127.         }
  128.     }
  129. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top