Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import java.awt.Color;
- import java.awt.Button;
- import java.awt.HeadlessException;
- import java.awt.SystemColor;
- import javax.swing.JLabel;
- import javax.swing.ImageIcon;
- import javax.swing.JOptionPane;
- import javax.swing.SwingConstants;
- import java.awt.Font;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseMotionAdapter;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import java.sql.*;
- import javax.swing.JTable;
- import javax.swing.JTextArea;
- import javax.swing.UIManager;
- import javax.swing.border.TitledBorder;
- import javax.swing.border.LineBorder;
- import javax.swing.border.BevelBorder;
- import javax.swing.table.DefaultTableModel;
- import javax.swing.JTextField;
- import net.proteanit.sql.DbUtils;
- import javax.swing.ListSelectionModel;
- import javax.swing.JScrollPane;
- public class StronaGlowna extends JFrame {
- Connection con = null;
- PreparedStatement pst = null;
- ResultSet rs = null;
- private JPanel contentPane;
- int xx,xy;
- private JTable table;
- private JTextField zad;
- private JTextField ID;
- /**
- * Launch the application.
- */
- public void showTableData(){
- try{
- con = DriverManager.getConnection("jdbc:mysql://localhost/todolist","root","");
- String sql = "SELECT * FROM todolist";
- pst = con.prepareStatement(sql);
- rs = pst.executeQuery();
- table.setModel(DbUtils.resultSetToTableModel(rs));
- } catch (Exception ex){
- JOptionPane.showMessageDialog(null, ex);
- }
- }
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- StronaGlowna frame = new StronaGlowna();
- frame.dispose();
- frame.setUndecorated(true);
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the frame.
- */
- public StronaGlowna() {
- setBackground(Color.WHITE);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 691, 367);
- contentPane = new JPanel();
- contentPane.setBackground(Color.LIGHT_GRAY);
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(null);
- JPanel panel = new JPanel();
- panel.setBackground(Color.DARK_GRAY);
- panel.setBounds(0, 0, 361, 380);
- contentPane.add(panel);
- JLabel lblNewLabel = new JLabel("");
- lblNewLabel.addMouseListener(new MouseAdapter() {
- @Override
- public void mousePressed(MouseEvent e) {
- xx = e.getX();
- xy = e.getY();
- }
- });
- lblNewLabel.addMouseMotionListener(new MouseMotionAdapter() {
- @Override
- public void mouseDragged(MouseEvent e) {
- int x = e.getXOnScreen();
- int y = e.getYOnScreen();
- StronaGlowna.this.setLocation(x-xx,y-xy);
- }
- });
- lblNewLabel.setIcon(new ImageIcon(StronaGlowna.class.getResource("/images/todolist.jpg")));
- panel.add(lblNewLabel);
- JLabel lblNewLabel_1 = new JLabel("Od teraz o niczym nie zapomnisz!");
- panel.add(lblNewLabel_1);
- lblNewLabel_1.setFont(new Font("Palace Script MT", Font.BOLD, 35));
- lblNewLabel_1.setForeground(new Color(238, 232, 170));
- Button button = new Button("Dodaj Zadanie");
- button.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- try{
- String sql = "INSERT INTO todolist"
- +"(Zadanie)"
- +"VALUES (?)";
- con = DriverManager.getConnection("jdbc:mysql://localhost/todolist","root","");
- pst = con.prepareStatement(sql);
- pst.setString(1,zad.getText());
- pst.executeUpdate();
- JOptionPane.showMessageDialog(null, "inserted successfully");
- } catch (SQLException | HeadlessException ex){
- JOptionPane.showMessageDialog(null, ex);
- }
- showTableData();
- }
- });
- button.setForeground(SystemColor.text);
- button.setBackground(new Color(211, 211, 211));
- button.setBounds(367, 310, 102, 34);
- contentPane.add(button);
- Button button_1 = new Button("Edytuj zadanie");
- button_1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- try{
- String sql = "UPDATE todolist SET Zadanie=? WHERE ID=?";
- con = DriverManager.getConnection("jdbc:mysql://localhost/todolist","root","");
- pst = con.prepareStatement(sql);
- pst.setString(2, ID.getText());
- pst.setString(1,zad.getText());
- pst.executeUpdate();
- JOptionPane.showMessageDialog(null, "updated successfully");
- } catch (SQLException | HeadlessException ex){
- JOptionPane.showMessageDialog(null, ex);
- }
- showTableData();
- }
- });
- button_1.setForeground(Color.WHITE);
- button_1.setBackground(new Color(169, 169, 169));
- button_1.setBounds(475, 310, 102, 34);
- contentPane.add(button_1);
- Button button_2 = new Button("Usu\u0144 zadanie");
- button_2.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- try{
- String sql = "DELETE FROM todolist WHERE ID=?";
- con = DriverManager.getConnection("jdbc:mysql://localhost/todolist","root","");
- pst = con.prepareStatement(sql);
- pst.setString(1,ID.getText());
- pst.executeUpdate();
- JOptionPane.showMessageDialog(null, "deleted successfully");
- } catch (SQLException | HeadlessException ex){
- JOptionPane.showMessageDialog(null, ex);
- }
- showTableData();
- }
- });
- button_2.setForeground(Color.WHITE);
- button_2.setBackground(new Color(128, 128, 128));
- button_2.setBounds(583, 310, 102, 34);
- contentPane.add(button_2);
- JLabel lblX = new JLabel("X");
- lblX.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent arg0) {
- System.exit(0);
- }
- });
- lblX.setForeground(new Color(165, 42, 42));
- lblX.setFont(new Font("Tahoma", Font.PLAIN, 26));
- lblX.setBounds(660, 0, 21, 43);
- contentPane.add(lblX);
- JLabel lblZadanieDoWykonania = new JLabel("Zadanie do wykonania: ");
- lblZadanieDoWykonania.setFont(new Font("Tahoma", Font.BOLD, 12));
- lblZadanieDoWykonania.setBounds(371, 48, 163, 14);
- contentPane.add(lblZadanieDoWykonania);
- zad = new JTextField();
- zad.setBounds(371, 73, 247, 20);
- contentPane.add(zad);
- zad.setColumns(10);
- ID = new JTextField();
- ID.setBounds(371, 128, 86, 20);
- contentPane.add(ID);
- ID.setColumns(10);
- JLabel lblIdZadania = new JLabel("ID zadania:");
- lblIdZadania.setFont(new Font("Tahoma", Font.BOLD, 12));
- lblIdZadania.setBounds(371, 104, 76, 14);
- contentPane.add(lblIdZadania);
- JScrollPane scrollPane = new JScrollPane();
- scrollPane.setBounds(371, 159, 310, 138);
- contentPane.add(scrollPane);
- table = new JTable();
- scrollPane.setViewportView(table);
- table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
- table.setModel(new DefaultTableModel(
- new Object[][] {
- {null, null},
- {null, null},
- {null, null},
- {null, null},
- {null, null},
- {null, null},
- {null, null},
- {null, null},
- {null, null},
- {null, null},
- },
- new String[] {
- "ID", "Zadanie"
- }
- ) {
- Class[] columnTypes = new Class[] {
- Integer.class, String.class
- };
- public Class getColumnClass(int columnIndex) {
- return columnTypes[columnIndex];
- }
- });
- table.getColumnModel().getColumn(0).setPreferredWidth(15);
- table.getColumnModel().getColumn(0).setMinWidth(5);
- table.getColumnModel().getColumn(0).setMaxWidth(15);
- table.getColumnModel().getColumn(1).setPreferredWidth(161);
- showTableData();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement