Advertisement
Guest User

Untitled

a guest
Dec 12th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.76 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.EventQueue;
  3.  
  4. import javax.swing.JFrame;
  5. import javax.swing.JPanel;
  6. import javax.swing.border.EmptyBorder;
  7.  
  8. import java.awt.Color;
  9. import java.awt.Button;
  10. import java.awt.HeadlessException;
  11. import java.awt.SystemColor;
  12.  
  13. import javax.swing.JLabel;
  14. import javax.swing.ImageIcon;
  15. import javax.swing.JOptionPane;
  16. import javax.swing.SwingConstants;
  17.  
  18. import java.awt.Font;
  19. import java.awt.event.MouseAdapter;
  20. import java.awt.event.MouseEvent;
  21. import java.awt.event.MouseMotionAdapter;
  22. import java.awt.event.ActionListener;
  23. import java.awt.event.ActionEvent;
  24. import java.sql.*;
  25.  
  26. import javax.swing.JTable;
  27. import javax.swing.JTextArea;
  28. import javax.swing.UIManager;
  29. import javax.swing.border.TitledBorder;
  30. import javax.swing.border.LineBorder;
  31. import javax.swing.border.BevelBorder;
  32. import javax.swing.table.DefaultTableModel;
  33. import javax.swing.JTextField;
  34.  
  35. import net.proteanit.sql.DbUtils;
  36. import javax.swing.ListSelectionModel;
  37. import javax.swing.JScrollPane;
  38.  
  39.  
  40. public class StronaGlowna extends JFrame {
  41.    
  42.     Connection con = null;
  43.     PreparedStatement pst = null;
  44.     ResultSet rs = null;
  45.    
  46.    
  47.    
  48.     private JPanel contentPane;
  49.     int xx,xy;
  50.     private JTable table;
  51.     private JTextField zad;
  52.     private JTextField ID;
  53.  
  54.     /**
  55.      * Launch the application.
  56.      */
  57.    
  58.     public void showTableData(){
  59.         try{
  60.             con = DriverManager.getConnection("jdbc:mysql://localhost/todolist","root","");
  61.             String sql = "SELECT * FROM todolist";
  62.             pst = con.prepareStatement(sql);
  63.             rs = pst.executeQuery();
  64.             table.setModel(DbUtils.resultSetToTableModel(rs));
  65.         } catch (Exception ex){
  66.             JOptionPane.showMessageDialog(null, ex);
  67.         }
  68.     }
  69.    
  70.     public static void main(String[] args) {
  71.        
  72.         EventQueue.invokeLater(new Runnable() {
  73.             public void run() {
  74.                 try {
  75.                     StronaGlowna frame = new StronaGlowna();
  76.                     frame.dispose();
  77.                    
  78.                     frame.setUndecorated(true);
  79.                     frame.setVisible(true);
  80.                 } catch (Exception e) {
  81.                     e.printStackTrace();
  82.                 }
  83.             }
  84.         });
  85.     }
  86.  
  87.     /**
  88.      * Create the frame.
  89.      */
  90.     public StronaGlowna() {
  91.         setBackground(Color.WHITE);
  92.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  93.         setBounds(100, 100, 691, 367);
  94.         contentPane = new JPanel();
  95.         contentPane.setBackground(Color.LIGHT_GRAY);
  96.         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  97.         setContentPane(contentPane);
  98.         contentPane.setLayout(null);
  99.        
  100.        
  101.        
  102.         JPanel panel = new JPanel();
  103.         panel.setBackground(Color.DARK_GRAY);
  104.         panel.setBounds(0, 0, 361, 380);
  105.         contentPane.add(panel);
  106.        
  107.         JLabel lblNewLabel = new JLabel("");
  108.        
  109.         lblNewLabel.addMouseListener(new MouseAdapter() {
  110.             @Override
  111.             public void mousePressed(MouseEvent e) {
  112.                
  113.                 xx = e.getX();
  114.                 xy = e.getY();
  115.             }
  116.         });
  117.         lblNewLabel.addMouseMotionListener(new MouseMotionAdapter() {
  118.             @Override
  119.             public void mouseDragged(MouseEvent e) {
  120.                 int x = e.getXOnScreen();
  121.                 int y = e.getYOnScreen();
  122.                 StronaGlowna.this.setLocation(x-xx,y-xy);
  123.             }
  124.         });
  125.         lblNewLabel.setIcon(new ImageIcon(StronaGlowna.class.getResource("/images/todolist.jpg")));
  126.         panel.add(lblNewLabel);
  127.        
  128.         JLabel lblNewLabel_1 = new JLabel("Od teraz o niczym nie zapomnisz!");
  129.         panel.add(lblNewLabel_1);
  130.         lblNewLabel_1.setFont(new Font("Palace Script MT", Font.BOLD, 35));
  131.         lblNewLabel_1.setForeground(new Color(238, 232, 170));
  132.        
  133.         Button button = new Button("Dodaj Zadanie");
  134.         button.addActionListener(new ActionListener() {
  135.             public void actionPerformed(ActionEvent arg0) {
  136.                
  137.                 try{
  138.                 String sql = "INSERT INTO todolist"
  139.                             +"(Zadanie)"
  140.                             +"VALUES (?)";
  141.                 con = DriverManager.getConnection("jdbc:mysql://localhost/todolist","root","");
  142.                 pst = con.prepareStatement(sql);
  143.                 pst.setString(1,zad.getText());
  144.                 pst.executeUpdate();
  145.                 JOptionPane.showMessageDialog(null, "inserted successfully");
  146.                
  147.                 } catch (SQLException | HeadlessException ex){
  148.                     JOptionPane.showMessageDialog(null, ex);
  149.                 }
  150.                
  151.                 showTableData();
  152.                                
  153.             }
  154.         });
  155.         button.setForeground(SystemColor.text);
  156.         button.setBackground(new Color(211, 211, 211));
  157.         button.setBounds(367, 310, 102, 34);
  158.         contentPane.add(button);
  159.        
  160.         Button button_1 = new Button("Edytuj zadanie");
  161.         button_1.addActionListener(new ActionListener() {
  162.             public void actionPerformed(ActionEvent arg0) {
  163.                
  164.                 try{
  165.                     String sql = "UPDATE todolist SET Zadanie=? WHERE ID=?";
  166.                     con = DriverManager.getConnection("jdbc:mysql://localhost/todolist","root","");
  167.                     pst = con.prepareStatement(sql);
  168.                     pst.setString(2, ID.getText());
  169.                     pst.setString(1,zad.getText());
  170.                     pst.executeUpdate();
  171.                     JOptionPane.showMessageDialog(null, "updated successfully");
  172.                    
  173.                     } catch (SQLException | HeadlessException ex){
  174.                         JOptionPane.showMessageDialog(null, ex);
  175.                     }
  176.                    
  177.                     showTableData();
  178.             }
  179.         });
  180.        
  181.        
  182.        
  183.         button_1.setForeground(Color.WHITE);
  184.         button_1.setBackground(new Color(169, 169, 169));
  185.         button_1.setBounds(475, 310, 102, 34);
  186.         contentPane.add(button_1);
  187.        
  188.         Button button_2 = new Button("Usu\u0144 zadanie");
  189.         button_2.addActionListener(new ActionListener() {
  190.             public void actionPerformed(ActionEvent arg0) {
  191.                
  192.                 try{
  193.                     String sql = "DELETE FROM todolist WHERE ID=?";
  194.                     con = DriverManager.getConnection("jdbc:mysql://localhost/todolist","root","");
  195.                     pst = con.prepareStatement(sql);
  196.                     pst.setString(1,ID.getText());
  197.                     pst.executeUpdate();
  198.                     JOptionPane.showMessageDialog(null, "deleted successfully");
  199.                    
  200.                     } catch (SQLException | HeadlessException ex){
  201.                         JOptionPane.showMessageDialog(null, ex);
  202.                     }
  203.                 showTableData();
  204.             }
  205.         });
  206.         button_2.setForeground(Color.WHITE);
  207.         button_2.setBackground(new Color(128, 128, 128));
  208.         button_2.setBounds(583, 310, 102, 34);
  209.         contentPane.add(button_2);
  210.        
  211.         JLabel lblX = new JLabel("X");
  212.         lblX.addMouseListener(new MouseAdapter() {
  213.             @Override
  214.             public void mouseClicked(MouseEvent arg0) {
  215.                 System.exit(0);
  216.             }
  217.         });
  218.         lblX.setForeground(new Color(165, 42, 42));
  219.         lblX.setFont(new Font("Tahoma", Font.PLAIN, 26));
  220.         lblX.setBounds(660, 0, 21, 43);
  221.         contentPane.add(lblX);
  222.        
  223.         JLabel lblZadanieDoWykonania = new JLabel("Zadanie do wykonania: ");
  224.         lblZadanieDoWykonania.setFont(new Font("Tahoma", Font.BOLD, 12));
  225.         lblZadanieDoWykonania.setBounds(371, 48, 163, 14);
  226.         contentPane.add(lblZadanieDoWykonania);
  227.        
  228.         zad = new JTextField();
  229.         zad.setBounds(371, 73, 247, 20);
  230.         contentPane.add(zad);
  231.         zad.setColumns(10);
  232.        
  233.         ID = new JTextField();
  234.         ID.setBounds(371, 128, 86, 20);
  235.         contentPane.add(ID);
  236.         ID.setColumns(10);
  237.        
  238.         JLabel lblIdZadania = new JLabel("ID zadania:");
  239.         lblIdZadania.setFont(new Font("Tahoma", Font.BOLD, 12));
  240.         lblIdZadania.setBounds(371, 104, 76, 14);
  241.         contentPane.add(lblIdZadania);
  242.        
  243.         JScrollPane scrollPane = new JScrollPane();
  244.         scrollPane.setBounds(371, 159, 310, 138);
  245.         contentPane.add(scrollPane);
  246.        
  247.         table = new JTable();
  248.         scrollPane.setViewportView(table);
  249.         table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
  250.         table.setModel(new DefaultTableModel(
  251.             new Object[][] {
  252.                 {null, null},
  253.                 {null, null},
  254.                 {null, null},
  255.                 {null, null},
  256.                 {null, null},
  257.                 {null, null},
  258.                 {null, null},
  259.                 {null, null},
  260.                 {null, null},
  261.                 {null, null},
  262.             },
  263.             new String[] {
  264.                 "ID", "Zadanie"
  265.             }
  266.         ) {
  267.             Class[] columnTypes = new Class[] {
  268.                 Integer.class, String.class
  269.             };
  270.             public Class getColumnClass(int columnIndex) {
  271.                 return columnTypes[columnIndex];
  272.             }
  273.         });
  274.         table.getColumnModel().getColumn(0).setPreferredWidth(15);
  275.         table.getColumnModel().getColumn(0).setMinWidth(5);
  276.         table.getColumnModel().getColumn(0).setMaxWidth(15);
  277.         table.getColumnModel().getColumn(1).setPreferredWidth(161);
  278.         showTableData();
  279.     }
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement