1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
| public class form1 extends javax.swing.JFrame {
/**
* Creates new form form1
*/
Connection conn=null;
ResultSet rs = null;
PreparedStatement pst = null;
JComboBox cb = new JComboBox();
DefaultTableModel model;
public form1() {
initComponents();
TableColumn tc = tblstudent.getColumnModel().getColumn(1);//C est la colonne ou je vais mettre mon combobox
TableCellEditor tce = new DefaultCellEditor(cb);
tc.setCellEditor(tce);
tblstudent.getColumnModel().getColumn(3).setCellRenderer(new DateCellRenderer());
tblstudent.getColumn("Date").setCellEditor(new CellDateEditor());//C'est la Colonne ou je vais mettre mon JDateChosser
//DEBUT Creation de l'evenement
tblstudent.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
tblstudent.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB,1),"tab");
tblstudent.getActionMap().put("tab",new AbstractAction(){
@Override
public void actionPerformed(ActionEvent e){
if(tblstudent.getSelectedRow()==tblstudent.getRowCount()) {
if((tblstudent.getSelectedColumn()==3)&&(tblstudent.getValueAt(0, 0)!=null))
model.addRow(new Object[]{null, null, null, null});}
}
});;
//FIN Creation de l'evenement listener
}
/**
* 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">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
tblstudent = new javax.swing.JTable();
Btnajout = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent evt) {
formWindowOpened(evt);
}
});
tblstudent.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
tblstudent.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"Num", "Nom", "Prenom", "Date"
}
));
tblstudent.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
tblstudentKeyPressed(evt);
}
public void keyTyped(java.awt.event.KeyEvent evt) {
tblstudentKeyTyped(evt);
}
});
jScrollPane1.setViewportView(tblstudent);
Btnajout.setText("Ajouter");
Btnajout.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BtnajoutActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(87, 87, 87)
.addComponent(Btnajout)
.addContainerGap(579, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 569, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(70, 70, 70))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(167, 167, 167)
.addComponent(Btnajout)
.addContainerGap(68, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void formWindowOpened(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
conn=MaConnection.connectDB();
RemplireCombo();
}
@SuppressWarnings("empty-statement")
private void BtnajoutActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
DefaultTableModel model=(DefaultTableModel) tblstudent.getModel();
int x = tblstudent.getRowCount();
if (x==0) model.addRow(new Object[]{null, null, null, null});
if (tblstudent.getValueAt(0, 0)==null ) { tblstudent.editCellAt(0,0);
tblstudent.setRowSelectionInterval(0, 0);
tblstudent.getEditorComponent().requestFocusInWindow();
}else {tblstudent.editCellAt(x+1,0);
tblstudent.setRowSelectionInterval(x+1, 0);
tblstudent.getEditorComponent().requestFocusInWindow();
}
}
@SuppressWarnings("empty-statement")
private void tblstudentKeyPressed(java.awt.event.KeyEvent evt) {
//DefaultTableModel model=(DefaultTableModel) tblstudent.getModel();
//(tblstudent.getSelectedRow()+1==tblstudent.getRowCount())&&
/* int x=tblstudent.getRowCount(); int y=tblstudent.getSelectedRow();
/* if (evt.getKeyCode()==KeyEvent.VK_TAB ||evt.getKeyCode()==KeyEvent.VK_DOWN)&&(tblstudent.getSelectedRow()+1==tblstudent.getRowCount())&&(tblstudent.getSelectedColumn()==3)&&(tblstudent.getValueAt(0, 0)!=null)
{model.addRow(new Object[]{null, null, null, null});}
switch (evt.getKeyCode())
{case KeyEvent.VK_TAB:
{if ((tblstudent.getSelectedRow()+1==tblstudent.getRowCount())&&(tblstudent.getSelectedColumn()==3)&&(tblstudent.getValueAt(0, 0)!=null))
{model.addRow(new Object[]{null, null, null, null});}
}break;
case KeyEvent.VK_DOWN:
}*/
}
private void tblstudentKeyTyped(java.awt.event.KeyEvent evt) {
}
/**
* @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/tu...feel/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 | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(form1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new form1().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton Btnajout;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable tblstudent;
// End of variables declaration
//Ma Procedure qui rempli mon Combobox
private void RemplireCombo() {
try {
String sql= "select nomst from student";
pst=conn.prepareStatement(sql);
rs=pst.executeQuery();
while (rs.next()){
String nomst=rs.getString("nomst");
cb.addItem(nomst);
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
} |
Partager