Advertisement
Grela

Untitled

Sep 28th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.29 KB | None | 0 0
  1. package Listado;
  2.  
  3. import java.awt.EventQueue;
  4.  
  5. import javax.swing.JFrame;
  6. import java.awt.FlowLayout;
  7. import java.awt.event.MouseAdapter;
  8. import java.awt.event.MouseEvent;
  9. import java.io.BufferedReader;
  10. import java.io.FileReader;
  11. import java.io.FileWriter;
  12. import java.io.IOException;
  13. import java.io.PrintWriter;
  14. import java.sql.Connection;
  15. import java.sql.DriverManager;
  16. import java.sql.SQLException;
  17.  
  18. import javax.swing.JList;
  19. import javax.swing.JTextField;
  20. import javax.swing.DefaultListModel;
  21. import javax.swing.JButton;
  22. import java.awt.BorderLayout;
  23. import javax.swing.JPanel;
  24. import javax.swing.ListSelectionModel;
  25.  
  26.  
  27. public class principal {
  28.  
  29.     private JFrame frame;
  30.     private JTextField text;
  31.     private JList<String> list;
  32.  
  33.     /**
  34.      * Launch the application.
  35.      */
  36.     public static void main(String[] args) {
  37.         EventQueue.invokeLater(new Runnable() {
  38.             public void run() {
  39.                 try {
  40.                     principal window = new principal();
  41.                     window.frame.setVisible(true);
  42.                 } catch (Exception e) {
  43.                     e.printStackTrace();
  44.                 }
  45.             }
  46.         });
  47.     }
  48.  
  49.     /**
  50.      * Create the application.
  51.      */
  52.     public principal() {
  53.         initialize();
  54.     }
  55.  
  56.     /**
  57.      * Initialize the contents of the frame.
  58.      */
  59.     private void initialize() {
  60.         frame = new JFrame();
  61.         frame.setBounds(100, 100, 658, 300);
  62.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  63.         frame.getContentPane().setLayout(new BorderLayout(0, 0));
  64.  
  65.         list = new JList<String>();
  66.         list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  67.         list.addMouseListener(new MouseAdapter() {
  68.             @Override
  69.             public void mouseClicked(MouseEvent e) {
  70.                 listSelected();
  71.             }
  72.         });
  73.         DefaultListModel<String> model = new DefaultListModel<String>();
  74.         list.setModel(model);
  75.  
  76.         frame.getContentPane().add(list, BorderLayout.CENTER);
  77.  
  78.         JPanel panel = new JPanel();
  79.         frame.getContentPane().add(panel, BorderLayout.SOUTH);
  80.         panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
  81.  
  82.         JButton Delete = new JButton("Borrar seleccionado");
  83.         Delete.addMouseListener(new MouseAdapter() {
  84.             @Override
  85.             public void mouseClicked(MouseEvent arg0) {
  86.                 deleteElement();
  87.             }
  88.         });
  89.         panel.add(Delete);
  90.  
  91.         JButton SaveDisk = new JButton("Guardar en disco");
  92.         SaveDisk.addMouseListener(new MouseAdapter() {
  93.             @Override
  94.             public void mouseClicked(MouseEvent e) {
  95.                 saveDisk();
  96.             }
  97.         });
  98.         panel.add(SaveDisk);
  99.  
  100.         JButton LoadDisk = new JButton("Cargar de disco");
  101.         LoadDisk.addMouseListener(new MouseAdapter() {
  102.             @Override
  103.             public void mouseClicked(MouseEvent e) {
  104.                 loadDisk();
  105.             }
  106.         });
  107.         panel.add(LoadDisk);
  108.  
  109.         JButton SaveDB = new JButton("Guardar en BD");
  110.         SaveDB.addMouseListener(new MouseAdapter() {
  111.             @Override
  112.             public void mouseClicked(MouseEvent arg0) {
  113.                 saveDB();
  114.             }
  115.         });
  116.         panel.add(SaveDB);
  117.  
  118.         JButton LoadDB = new JButton("Cargar de BD");
  119.         panel.add(LoadDB);
  120.  
  121.         JPanel panel_1 = new JPanel();
  122.         frame.getContentPane().add(panel_1, BorderLayout.WEST);
  123.         panel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
  124.  
  125.         text = new JTextField();
  126.         panel_1.add(text);
  127.         text.setColumns(10);
  128.  
  129.         JButton Add = new JButton("A\u00F1adir");
  130.         Add.addMouseListener(new MouseAdapter() {
  131.             @Override
  132.             public void mouseClicked(MouseEvent e) {
  133.                 addElement();
  134.             }
  135.         });
  136.         panel_1.add(Add);
  137.  
  138.         JButton Edit = new JButton("Actualizar");
  139.         Edit.addMouseListener(new MouseAdapter() {
  140.             @Override
  141.             public void mouseClicked(MouseEvent e) {
  142.                 modifyElement();
  143.             }
  144.         });
  145.         panel_1.add(Edit);
  146.     }
  147.  
  148.     private void deleteElement() {
  149.         String selectedValue = list.getSelectedValue();
  150.         DefaultListModel<String> model = (DefaultListModel<String>) list.getModel();
  151.         model.removeElement(selectedValue);
  152.     }
  153.  
  154.     private void addElement() {
  155.         String value = text.getText();
  156.         DefaultListModel<String> model = (DefaultListModel<String>) list.getModel();
  157.         model.addElement(value);
  158.     }
  159.  
  160.     private void listSelected() {
  161.         String selectedValue = list.getSelectedValue();
  162.         text.setText(selectedValue);
  163.  
  164.     }
  165.  
  166.     private void modifyElement() {
  167.         String value = text.getText();
  168.         DefaultListModel<String> model = (DefaultListModel<String>) list.getModel();
  169.         model.setElementAt(value, list.getSelectedIndex());
  170.  
  171.     }
  172.  
  173.     public JList<String> getList() {
  174.         return list;
  175.     }
  176.  
  177.     private void saveDisk() {
  178.         FileWriter fw = null;
  179.         PrintWriter pw = null;
  180.         try {
  181.             fw = new FileWriter("fichero.txt");
  182.             pw = new PrintWriter(fw);
  183.             DefaultListModel<String> model = (DefaultListModel<String>) list.getModel();
  184.             int size = model.getSize();
  185.             for (int i = 0; i < size; i++) {
  186.                 pw.println(model.getElementAt(i));
  187.             }
  188.  
  189.         } catch (IOException e) {
  190.             System.out.println("Fallo en el acceso al fichero. " + e.getStackTrace());
  191.         } finally {
  192.             try {
  193.                 if (fw != null) {
  194.                     fw.close();
  195.                 }
  196.                 if (fw != null) {
  197.                     pw.close();
  198.                 }
  199.             } catch (IOException e) {
  200.                 e.printStackTrace();
  201.             }
  202.         }
  203.     }
  204.  
  205.     private void loadDisk() {
  206.         FileReader fr = null;
  207.         BufferedReader br = null;
  208.         try {
  209.             fr = new FileReader("fichero.txt");
  210.             br = new BufferedReader(fr);
  211.             String line;
  212.             DefaultListModel<String> model = (DefaultListModel<String>) list.getModel();
  213.             model.removeAllElements();
  214.             while ((line = br.readLine()) != null) {
  215.                 model.addElement(line);
  216.             }
  217.             br.readLine();
  218.  
  219.         } catch (IOException e) {
  220.             System.out.println("Fallo en el acceso al fichero. " + e.getStackTrace());
  221.         } finally {
  222.             try {
  223.                 if (fr != null) {
  224.                     fr.close();
  225.                 }
  226.                 if (br != null) {
  227.                     br.close();
  228.                 }
  229.             } catch (IOException e) {
  230.                 e.printStackTrace();
  231.             }
  232.         }
  233.     }
  234.     private void saveDB(){
  235.         String sURL="jdbc:mysql://localhost:3306/lista";
  236.         String user="root";
  237.         String pass="admin";
  238.         String sDriver="com.mysql.jdbc.Driver";
  239.         Connection con=null;
  240.        
  241.         try {
  242.             Class.forName(sDriver).newInstance();
  243.         } catch (InstantiationException e) {
  244.             // TODO Auto-generated catch block
  245.             e.printStackTrace();
  246.         } catch (IllegalAccessException e) {
  247.             // TODO Auto-generated catch block
  248.             e.printStackTrace();
  249.         } catch (ClassNotFoundException e) {
  250.             // TODO Auto-generated catch block
  251.             e.printStackTrace();
  252.         }
  253.         try {
  254.             con = DriverManager.getConnection(sURL,user,pass);
  255.         } catch (SQLException e) {
  256.             // TODO Auto-generated catch block
  257.             e.printStackTrace();
  258.         }
  259.        
  260.     }
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement