/** * * Appfrica Labs Uganda Ltd Copyrigth @since 2009 * @version 2 * * CatFrm.java * * This form is add categories to the database. These categories are shared with both the * Answer and the Query class. * * Created on Oct 22, 2009, 4:45:36 PM */ package questionbox; import javax.swing.JOptionPane; import qbox.model.*; /** * * @author Victor Miclovich */ public class CatFrm extends javax.swing.JFrame { /** Creates new form CatFrm */ public CatFrm() { initComponents(); this.setTitle("Add Category Form"); lblTitle.setText("Title"); lblDescr.setText("Description"); buSave.setText("Save & Clear"); butClear.setText("Cancel"); setLocationRelativeTo(null); setResizable(false); } /** 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() { jScrollPane1 = new javax.swing.JScrollPane(); txtAreaDescr = new javax.swing.JTextArea(); txtTitle = new javax.swing.JTextField(); lblDescr = new javax.swing.JLabel(); lblTitle = new javax.swing.JLabel(); butClear = new javax.swing.JButton(); buSave = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setName("Form"); // NOI18N jScrollPane1.setName("jScrollPane1"); // NOI18N txtAreaDescr.setColumns(20); txtAreaDescr.setRows(5); txtAreaDescr.setName("txtAreaDescr"); // NOI18N jScrollPane1.setViewportView(txtAreaDescr); txtTitle.setName("txtTitle"); // NOI18N txtTitle.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { txtTitleActionPerformed(evt); } }); lblDescr.setName("lblDescr"); // NOI18N lblTitle.setName("lblTitle"); // NOI18N butClear.setName("butClear"); // NOI18N butClear.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { butClearActionPerformed(evt); } }); buSave.setName("buSave"); // NOI18N buSave.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buSaveActionPerformed(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() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(lblTitle) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(txtTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(93, 93, 93)) .addGroup(layout.createSequentialGroup() .addComponent(lblDescr) .addContainerGap(323, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup() .addGap(33, 33, 33) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(layout.createSequentialGroup() .addComponent(buSave) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(butClear)) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE))))) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(25, 25, 25) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(txtTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(lblTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(lblDescr, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(butClear) .addComponent(buSave)) .addGap(27, 27, 27)) ); pack(); }// </editor-fold>//GEN-END:initComponents private void txtTitleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtTitleActionPerformed // TODO add your handling code here: }//GEN-LAST:event_txtTitleActionPerformed private void butClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butClearActionPerformed // TODO add your handling code here: this.dispose(); }//GEN-LAST:event_butClearActionPerformed /** * Save the category to the database. * @param evt */ private void buSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buSaveActionPerformed try{ //Check that all the fields have values assigned before you save. if(txtTitle.getText().length() ==0 || txtAreaDescr.getText().length() == 0) { JOptionPane.showMessageDialog(this, "Please Enter Title and Description"); return; } //Set up the category object ready for saving. Category cat = new Category(); cat.setTitle(txtTitle.getText()); cat.setDescr(txtAreaDescr.getText()); cat.AddCategory(); JOptionPane.showMessageDialog(this, "A new category added successfully","Information",JOptionPane.ERROR_MESSAGE); //Clear the fields. txtAreaDescr.setText(""); txtTitle.setText(""); } catch (Exception e) { JOptionPane.showMessageDialog(this, "Failed to add Category","Information",JOptionPane.ERROR_MESSAGE); } }//GEN-LAST:event_buSaveActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { CatFrm catFrm = new CatFrm(); catFrm.setResizable(false); catFrm.setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton buSave; private javax.swing.JButton butClear; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JLabel lblDescr; private javax.swing.JLabel lblTitle; private javax.swing.JTextArea txtAreaDescr; private javax.swing.JTextField txtTitle; // End of variables declaration//GEN-END:variables }