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 com.iucosoft.eavertizare.gui; import com.iucosoft.eavertizare.dao.FirmaDaoIntf; import com.iucosoft.eavertizare.gui.models.FirmaTableModel; import com.iucosoft.eavertizare.model.Firma; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.ListSelectionModel; import org.springframework.context.ApplicationContext; /** * * @author valera_ */ public class FirmeJDialog extends javax.swing.JDialog { private Firma firma; private FirmaDaoIntf firmaDao; private final MainJFrame parent; private final FirmaTableModel firmaTableModel; public FirmeJDialog(MainJFrame parent, boolean modal, ApplicationContext contexDao) { setTitle("Tabela firmelor"); this.parent = parent; firmaTableModel = contexDao.getBean(FirmaTableModel.class); firmaDao = contexDao.getBean("firmaDao", FirmaDaoIntf.class); setModal(modal); initComponents(); init(); setLocationRelativeTo(parent); } public void refreshFrame() { firmaTableModel.refreshModel(); } private void init() { jTableFirme.setModel(firmaTableModel); firmaTableModel.refreshModel(); jTableFirme.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); jTableFirme.addMouseListener(mouseListenerTable); } MouseListener mouseListenerTable = new MouseAdapter() { @Override public void mouseClicked(MouseEvent mouseEvent) { if (mouseEvent.getClickCount() == 2) { int row = jTableFirme.getSelectedRow(); String numeFirma = (String) firmaTableModel.getValueAt(row, 1); firma = firmaDao.findByName(numeFirma); showFirmaJDialog(firma, firmaTableModel); firmaTableModel.refreshModel(); // JOptionPane.showMessageDialog(new Frame(), "Nu exista firma cu asa nume = " + numeFirma, // "Error", JOptionPane.WARNING_MESSAGE); } } }; public void showFirmaJDialog(final Firma firma, final FirmaTableModel firmaTableModel) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(FirmaJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(FirmaJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(FirmaJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(FirmaJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> //</editor-fold> /* Create and display the dialog */ java.awt.EventQueue.invokeLater(new Runnable() { @Override public void run() { FirmaJDialog dialog = new FirmaJDialog(parent, firma, true, firmaTableModel); dialog.setVisible(true); } }); } /** * 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() { jPanel1 = new javax.swing.JPanel(); jScrollPane1 = new javax.swing.JScrollPane(); jTableFirme = new javax.swing.JTable(); jButtonOK = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Tabele de firme")); jTableFirme .setModel(new javax.swing.table.DefaultTableModel( new Object[][] { { null, null, null, null }, { null, null, null, null }, { null, null, null, null }, { null, null, null, null } }, new String[] { "Title 1", "Title 2", "Title 3", "Title 4" })); jScrollPane1.setViewportView(jTableFirme); jButtonOK.setText("OK"); jButtonOK.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonOKActionPerformed(evt); } }); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(jPanel1Layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE) .addComponent(jButtonOK)) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 668, Short.MAX_VALUE)) .addGap(0, 0, 0))); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE) .addGap(18, 18, 18).addComponent(jButtonOK))); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))); pack(); }// </editor-fold>//GEN-END:initComponents private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOKActionPerformed this.dispose(); }//GEN-LAST:event_jButtonOKActionPerformed /** * @param args the command line arguments */ // public static void main(String args[]) { // /* Set the Nimbus look and feel */ // //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> // /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. // * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html // */ // try { // for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { // if ("Nimbus".equals(info.getName())) { // javax.swing.UIManager.setLookAndFeel(info.getClassName()); // break; // } // } // } catch (ClassNotFoundException ex) { // java.util.logging.Logger.getLogger(FirmeJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); // } catch (InstantiationException ex) { // java.util.logging.Logger.getLogger(FirmeJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); // } catch (IllegalAccessException ex) { // java.util.logging.Logger.getLogger(FirmeJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); // } catch (javax.swing.UnsupportedLookAndFeelException ex) { // java.util.logging.Logger.getLogger(FirmeJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); // } // //</editor-fold> // // /* Create and display the dialog */ // java.awt.EventQueue.invokeLater(new Runnable() { // public void run() { // FirmeJDialog dialog = new FirmeJDialog(new javax.swing.JFrame(), true); // dialog.addWindowListener(new java.awt.event.WindowAdapter() { // @Override // public void windowClosing(java.awt.event.WindowEvent e) { // System.exit(0); // } // }); // dialog.setVisible(true); // } // }); // } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButtonOK; private javax.swing.JPanel jPanel1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTable jTableFirme; // End of variables declaration//GEN-END:variables }