Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package streaming.swing; import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import streaming.entity.Film; import streaming.entity.Genre; import streaming.entity.Pays; import streaming.exception.SynopsisVideOuNullException; import streaming.service.FilmService; import streaming.service.GenreService; import streaming.service.PaysService; /** * * @author admin */ @Component public class JDialogEditFilm extends javax.swing.JDialog { @Autowired private FilmService fServ; @Autowired private PaysService pServ; @Autowired private GenreService gServ; private List<Genre> listGenre = new ArrayList<Genre>(); private List<Pays> listPays = new ArrayList<Pays>(); @Autowired private JPanelListFilm jplf; public void setJplf(JPanelListFilm jplf) { this.jplf = jplf; } public void rafraichirListPaysGenre() { listPays = pServ.listerTous(); listGenre = gServ.listerTous(); } public void initialiserComboBoxPays() { jComboBoxPays.removeAllItems(); for (Pays p : listPays) { jComboBoxPays.addItem(p.getNom()); } } public void initialiserComboBoxGenre() { jComboBoxGenre.removeAllItems(); for (Genre g : listGenre) { jComboBoxGenre.addItem(g.getNom()); } } /** * Creates new form JDialogEditFilm */ public JDialogEditFilm() { this.setModal(true); initComponents(); } public void initialiser() { rafraichirListPaysGenre(); initialiserComboBoxPays(); initialiserComboBoxGenre(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jLabel1 = new javax.swing.JLabel(); jPanel1 = new javax.swing.JPanel(); jTextFieldIdFilm = new javax.swing.JTextField(); jLabel2 = new javax.swing.JLabel(); jPanel2 = new javax.swing.JPanel(); jTextFieldTitre = new javax.swing.JTextField(); jLabel3 = new javax.swing.JLabel(); jPanel3 = new javax.swing.JPanel(); jTextFieldAnnee = new javax.swing.JTextField(); jLabel6 = new javax.swing.JLabel(); jPanel8 = new javax.swing.JPanel(); jTextFieldSynopsis = new javax.swing.JTextField(); jLabel4 = new javax.swing.JLabel(); jPanel4 = new javax.swing.JPanel(); jComboBoxPays = new javax.swing.JComboBox<>(); jLabel5 = new javax.swing.JLabel(); jPanel5 = new javax.swing.JPanel(); jComboBoxGenre = new javax.swing.JComboBox<>(); jPanel6 = new javax.swing.JPanel(); jButtonAjouter = new javax.swing.JButton(); jPanel7 = new javax.swing.JPanel(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); getContentPane().setLayout(new java.awt.GridLayout(7, 3)); jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel1.setText("ID du Film"); jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); getContentPane().add(jLabel1); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 133, Short.MAX_VALUE)); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 42, Short.MAX_VALUE)); getContentPane().add(jPanel1); jTextFieldIdFilm.setEditable(false); getContentPane().add(jTextFieldIdFilm); jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel2.setText("Titre du Film"); getContentPane().add(jLabel2); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 133, Short.MAX_VALUE)); jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 42, Short.MAX_VALUE)); getContentPane().add(jPanel2); getContentPane().add(jTextFieldTitre); jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel3.setText("Anne de production"); getContentPane().add(jLabel3); javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); jPanel3.setLayout(jPanel3Layout); jPanel3Layout.setHorizontalGroup(jPanel3Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 133, Short.MAX_VALUE)); jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 42, Short.MAX_VALUE)); getContentPane().add(jPanel3); getContentPane().add(jTextFieldAnnee); jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel6.setText("Synopsis"); getContentPane().add(jLabel6); javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8); jPanel8.setLayout(jPanel8Layout); jPanel8Layout.setHorizontalGroup(jPanel8Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 133, Short.MAX_VALUE)); jPanel8Layout.setVerticalGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 42, Short.MAX_VALUE)); getContentPane().add(jPanel8); getContentPane().add(jTextFieldSynopsis); jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel4.setText("Pays"); getContentPane().add(jLabel4); javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4); jPanel4.setLayout(jPanel4Layout); jPanel4Layout.setHorizontalGroup(jPanel4Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 133, Short.MAX_VALUE)); jPanel4Layout.setVerticalGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 42, Short.MAX_VALUE)); getContentPane().add(jPanel4); jComboBoxPays.setModel( new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); jComboBoxPays.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jComboBoxPaysActionPerformed(evt); } }); getContentPane().add(jComboBoxPays); jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel5.setText("Genre"); getContentPane().add(jLabel5); javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5); jPanel5.setLayout(jPanel5Layout); jPanel5Layout.setHorizontalGroup(jPanel5Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 133, Short.MAX_VALUE)); jPanel5Layout.setVerticalGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 42, Short.MAX_VALUE)); getContentPane().add(jPanel5); jComboBoxGenre.setModel( new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); getContentPane().add(jComboBoxGenre); javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6); jPanel6.setLayout(jPanel6Layout); jPanel6Layout.setHorizontalGroup(jPanel6Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 133, Short.MAX_VALUE)); jPanel6Layout.setVerticalGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 42, Short.MAX_VALUE)); getContentPane().add(jPanel6); jButtonAjouter.setText("Ajouter"); jButtonAjouter.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonAjouterActionPerformed(evt); } }); getContentPane().add(jButtonAjouter); javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7); jPanel7.setLayout(jPanel7Layout); jPanel7Layout.setHorizontalGroup(jPanel7Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 133, Short.MAX_VALUE)); jPanel7Layout.setVerticalGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 42, Short.MAX_VALUE)); getContentPane().add(jPanel7); pack(); }// </editor-fold>//GEN-END:initComponents private void jButtonAjouterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAjouterActionPerformed Pays pays = null; Genre genre = null; for (Pays p : listPays) { if (p.getNom().equals(jComboBoxPays.getSelectedItem())) { pays = p; } } for (Genre g : listGenre) { if (g.getNom().equals(jComboBoxGenre.getSelectedItem())) { genre = g; } } Film f = new Film(genre, pays, jTextFieldTitre.getText(), jTextFieldSynopsis.getText(), Long.parseLong(jTextFieldAnnee.getText())); try { fServ.ajouter(f); } catch (SynopsisVideOuNullException e) { } jplf.rafraichitJTable(); this.setVisible(false); this.dispose(); }//GEN-LAST:event_jButtonAjouterActionPerformed private void jComboBoxPaysActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxPaysActionPerformed // TODO add your handling code here: }//GEN-LAST:event_jComboBoxPaysActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButtonAjouter; private javax.swing.JComboBox<String> jComboBoxGenre; private javax.swing.JComboBox<String> jComboBoxPays; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JPanel jPanel3; private javax.swing.JPanel jPanel4; private javax.swing.JPanel jPanel5; private javax.swing.JPanel jPanel6; private javax.swing.JPanel jPanel7; private javax.swing.JPanel jPanel8; private javax.swing.JTextField jTextFieldAnnee; private javax.swing.JTextField jTextFieldIdFilm; private javax.swing.JTextField jTextFieldSynopsis; private javax.swing.JTextField jTextFieldTitre; // End of variables declaration//GEN-END:variables }