tenten

Untitled

Jul 6th, 2015
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.22 KB | None | 0 0
  1. here my full code .. i added listener in addbtn action performed method.. but not showing total in the total column as i enter qty and discount... :(
  2.  
  3. import java.awt.Toolkit;
  4. import java.awt.event.KeyAdapter;
  5. import java.awt.event.KeyEvent;
  6. import java.sql.Connection;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import java.sql.Statement;
  11. import java.util.ArrayList;
  12. import java.util.Vector;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15. import javax.swing.DefaultComboBoxModel;
  16. import javax.swing.JOptionPane;
  17. import javax.swing.JTable;
  18. import javax.swing.JTextField;
  19. import javax.swing.SwingUtilities;
  20. import javax.swing.event.TableModelEvent;
  21. import javax.swing.event.TableModelListener;
  22. import javax.swing.table.DefaultTableModel;
  23. import javax.swing.table.TableModel;
  24. import net.proteanit.sql.DbUtils;
  25.  
  26. /**
  27. *
  28. * @author Tili
  29. */
  30. public class Newsale extends javax.swing.JFrame {
  31.  
  32. Connection conn = null;
  33. Statement stmt= null;
  34. PreparedStatement pst = null;
  35. ResultSet rs = null;
  36.  
  37. private TableModelListener tableModelListener;
  38.  
  39.  
  40. /**
  41. * Creates new form newSale
  42. */
  43. public Newsale() throws SQLException {
  44. initComponents();
  45. this.setLocation(300,100);
  46. initialize();
  47. conn = MYSQLConnect.ConnectDB();
  48. setLocationRelativeTo(null);
  49.  
  50.  
  51.  
  52.  
  53. final JTextField txtfield = (JTextField) IDcombo.getEditor().getEditorComponent();
  54. txtfield.addKeyListener(new KeyAdapter() {
  55. public void keyReleased(KeyEvent ke){
  56. SwingUtilities.invokeLater(new Runnable() {
  57.  
  58. @Override
  59. public void run() {
  60. try {
  61. comboFilter(txtfield.getText());
  62. } catch (SQLException ex) {
  63. Logger.getLogger(Newsale.class.getName()).log(Level.SEVERE, null, ex);
  64. }
  65. }
  66. });
  67.  
  68. }
  69. });
  70.  
  71. final JTextField txtfield2 = (JTextField) Namecombo.getEditor().getEditorComponent();
  72. txtfield2.addKeyListener(new KeyAdapter() {
  73. public void keyReleased(KeyEvent ke){
  74. SwingUtilities.invokeLater(new Runnable() {
  75.  
  76. @Override
  77. public void run() {
  78. try {
  79. comboFilter(txtfield2.getText());
  80. } catch (SQLException ex) {
  81. Logger.getLogger(Newsale.class.getName()).log(Level.SEVERE, null, ex);
  82. }
  83. }
  84. });
  85.  
  86. }
  87. });
  88.  
  89. }
  90.  
  91. public void comboFilter(String enteredText) throws SQLException{
  92. ArrayList<String> filterArray = new ArrayList<String>();
  93. ArrayList<String> filterArray2 = new ArrayList<String>();
  94.  
  95. String str1="";
  96. String str2="";
  97.  
  98.  
  99.  
  100. try{
  101. String str = "SELECT * FROM druginfo WHERE ItemId LIKE '"+enteredText+"%'";
  102.  
  103. stmt = conn.createStatement();
  104. rs = stmt.executeQuery(str);
  105.  
  106. while(rs.next()){
  107. str1=rs.getString("ItemId");
  108. filterArray.add(str1);
  109. }
  110. }catch(Exception e ){
  111. JOptionPane.showMessageDialog(null,"Error " +e);
  112. }
  113.  
  114. try{
  115. String str0 = "SELECT * FROM druginfo WHERE ItemName LIKE '"+enteredText+"%'";
  116.  
  117. stmt = conn.createStatement();
  118. rs = stmt.executeQuery(str0);
  119.  
  120. while(rs.next()){
  121. str2=rs.getString("ItemName");
  122. filterArray2.add(str2);
  123. }
  124.  
  125. }catch(Exception e ){
  126. JOptionPane.showMessageDialog(null,"Error " +e);
  127. }
  128.  
  129.  
  130.  
  131. if(filterArray.size()>0){
  132. IDcombo.setModel(new DefaultComboBoxModel(filterArray.toArray()));
  133. IDcombo.setSelectedItem(enteredText);
  134. IDcombo.showPopup();
  135. } else{
  136. IDcombo.hidePopup();
  137. }
  138.  
  139. if(filterArray2.size()>0){
  140. Namecombo.setModel(new DefaultComboBoxModel(filterArray2.toArray()));
  141. Namecombo.setSelectedItem(enteredText);
  142. Namecombo.showPopup();
  143. }else{
  144. Namecombo.hidePopup();
  145. }
  146.  
  147.  
  148.  
  149. }
  150.  
  151. //changing the titile bar java icon
  152. private void initialize(){
  153. this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("cash-register-icon.png")));
  154. }
  155.  
  156. //for adding multiple lines in the jtable
  157. public void addDataToTable(JTable table,TableModel model) {
  158. DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
  159. DefaultTableModel resultSetModel = (DefaultTableModel) model;
  160. for (int i = 0; i < resultSetModel.getRowCount(); i++) {
  161. Vector row=new Vector();
  162. for (int j = 0; j < resultSetModel.getColumnCount(); j++) {
  163. row.addElement(resultSetModel.getValueAt(i, j));
  164. }
  165. tableModel.addRow(row);
  166. }
  167. tableModel.fireTableDataChanged();
  168. }
  169.  
  170.  
  171.  
  172.  
  173. // }
  174. //}
  175.  
  176.  
  177. /**
  178. * This method is called from within the constructor to initialize the form.
  179. * WARNING: Do NOT modify this code. The content of this method is always
  180. * regenerated by the Form Editor.
  181. */
  182. @SuppressWarnings("unchecked")
  183. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  184. private void initComponents() {
  185.  
  186. jPanel1 = new javax.swing.JPanel();
  187. jDesktopPane1 = new javax.swing.JDesktopPane();
  188. newSaleBtn = new javax.swing.JButton();
  189. searchInvoiceBtn = new javax.swing.JButton();
  190. saleInvoiceHelpBtn = new javax.swing.JButton();
  191. saleInvoicePrintBtn = new javax.swing.JButton();
  192. saleInvoicePreviousBtn = new javax.swing.JButton();
  193. saleInvoiceNextBtn = new javax.swing.JButton();
  194. saleInvoiceHomeBtn = new javax.swing.JButton();
  195. jLabel4 = new javax.swing.JLabel();
  196. jLabel5 = new javax.swing.JLabel();
  197. jLabel7 = new javax.swing.JLabel();
  198. dayChooser = new com.toedter.calendar.JDateChooser();
  199. jButton5 = new javax.swing.JButton();
  200. countitems_txt = new javax.swing.JTextField();
  201. jTextField1 = new javax.swing.JTextField();
  202. IDcombo = new javax.swing.JComboBox();
  203. jTextField3 = new javax.swing.JTextField();
  204. jLabel9 = new javax.swing.JLabel();
  205. instock = new javax.swing.JTextField();
  206. showDruglist_btn = new javax.swing.JButton();
  207. exptxt = new javax.swing.JTextField();
  208. jLabel6 = new javax.swing.JLabel();
  209. add_btn = new javax.swing.JButton();
  210. explbl1 = new javax.swing.JLabel();
  211. explbl = new javax.swing.JLabel();
  212. Namecombo = new javax.swing.JComboBox();
  213. jScrollPane2 = new javax.swing.JScrollPane();
  214. tableSale = new javax.swing.JTable();
  215. jLabel8 = new javax.swing.JLabel();
  216. jLabel3 = new javax.swing.JLabel();
  217. jTextField5 = new javax.swing.JTextField();
  218. jLabel2 = new javax.swing.JLabel();
  219. jLabel1 = new javax.swing.JLabel();
  220. jTextField4 = new javax.swing.JTextField();
  221.  
  222. setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
  223. setTitle("New Sale");
  224. setMinimumSize(new java.awt.Dimension(800, 500));
  225. setResizable(false);
  226.  
  227. jPanel1.setLayout(null);
  228.  
  229. newSaleBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/bit/project/new.png"))); // NOI18N
  230. newSaleBtn.setToolTipText("New");
  231. newSaleBtn.setPreferredSize(new java.awt.Dimension(60, 40));
  232. newSaleBtn.addActionListener(new java.awt.event.ActionListener() {
  233. public void actionPerformed(java.awt.event.ActionEvent evt) {
  234. newSaleBtnActionPerformed(evt);
  235. }
  236. });
  237.  
  238. searchInvoiceBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/bit/project/findinvoice.png"))); // NOI18N
  239. searchInvoiceBtn.setToolTipText("Search");
  240. searchInvoiceBtn.addActionListener(new java.awt.event.ActionListener() {
  241. public void actionPerformed(java.awt.event.ActionEvent evt) {
  242. searchInvoiceBtnActionPerformed(evt);
  243. }
  244. });
  245.  
  246. saleInvoiceHelpBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/bit/project/help-icon.png"))); // NOI18N
  247. saleInvoiceHelpBtn.setToolTipText("Help");
  248.  
  249. saleInvoicePrintBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/bit/project/print-icon.png"))); // NOI18N
  250. saleInvoicePrintBtn.setToolTipText("Print");
  251.  
  252. saleInvoicePreviousBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/bit/project/previous.png"))); // NOI18N
  253. saleInvoicePreviousBtn.setToolTipText("Previous ");
  254.  
  255. saleInvoiceNextBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/bit/project/next.png"))); // NOI18N
  256. saleInvoiceNextBtn.setToolTipText("Next");
  257.  
  258. saleInvoiceHomeBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/bit/project/blue-home-icon.png"))); // NOI18N
  259. saleInvoiceHomeBtn.setToolTipText("Home");
  260. saleInvoiceHomeBtn.addActionListener(new java.awt.event.ActionListener() {
  261. public void actionPerformed(java.awt.event.ActionEvent evt) {
  262. saleInvoiceHomeBtnActionPerformed(evt);
  263. }
  264. });
  265.  
  266. javax.swing.GroupLayout jDesktopPane1Layout = new javax.swing.GroupLayout(jDesktopPane1);
  267. jDesktopPane1.setLayout(jDesktopPane1Layout);
  268. jDesktopPane1Layout.setHorizontalGroup(
  269. jDesktopPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  270. .addGroup(jDesktopPane1Layout.createSequentialGroup()
  271. .addContainerGap()
  272. .addComponent(saleInvoiceHomeBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
  273. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  274. .addComponent(newSaleBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
  275. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  276. .addComponent(searchInvoiceBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
  277. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  278. .addComponent(saleInvoicePrintBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
  279. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 413, Short.MAX_VALUE)
  280. .addComponent(saleInvoicePreviousBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
  281. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  282. .addComponent(saleInvoiceNextBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
  283. .addGap(11, 11, 11)
  284. .addComponent(saleInvoiceHelpBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
  285. .addGap(22, 22, 22))
  286. );
  287. jDesktopPane1Layout.setVerticalGroup(
  288. jDesktopPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  289. .addGroup(jDesktopPane1Layout.createSequentialGroup()
  290. .addContainerGap()
  291. .addGroup(jDesktopPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  292. .addComponent(searchInvoiceBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  293. .addComponent(saleInvoicePrintBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  294. .addComponent(saleInvoiceHelpBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  295. .addComponent(newSaleBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  296. .addComponent(saleInvoicePreviousBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  297. .addComponent(saleInvoiceNextBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  298. .addComponent(saleInvoiceHomeBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  299. .addContainerGap())
  300. );
  301. jDesktopPane1.setLayer(newSaleBtn, javax.swing.JLayeredPane.DEFAULT_LAYER);
  302. jDesktopPane1.setLayer(searchInvoiceBtn, javax.swing.JLayeredPane.DEFAULT_LAYER);
  303. jDesktopPane1.setLayer(saleInvoiceHelpBtn, javax.swing.JLayeredPane.DEFAULT_LAYER);
  304. jDesktopPane1.setLayer(saleInvoicePrintBtn, javax.swing.JLayeredPane.DEFAULT_LAYER);
  305. jDesktopPane1.setLayer(saleInvoicePreviousBtn, javax.swing.JLayeredPane.DEFAULT_LAYER);
  306. jDesktopPane1.setLayer(saleInvoiceNextBtn, javax.swing.JLayeredPane.DEFAULT_LAYER);
  307. jDesktopPane1.setLayer(saleInvoiceHomeBtn, javax.swing.JLayeredPane.DEFAULT_LAYER);
  308.  
  309. jPanel1.add(jDesktopPane1);
  310. jDesktopPane1.setBounds(0, 0, 920, 60);
  311.  
  312. jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  313. jLabel4.setText("Total Discount");
  314. jPanel1.add(jLabel4);
  315. jLabel4.setBounds(660, 490, 100, 17);
  316.  
  317. jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  318. jLabel5.setText("Total items");
  319. jPanel1.add(jLabel5);
  320. jLabel5.setBounds(660, 460, 70, 30);
  321.  
  322. jLabel7.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  323. jLabel7.setText("Item Name");
  324. jPanel1.add(jLabel7);
  325. jLabel7.setBounds(50, 150, 80, 30);
  326.  
  327. dayChooser.setToolTipText("Give the date");
  328. dayChooser.setDateFormatString("yyyy-MM-dd");
  329. jPanel1.add(dayChooser);
  330. dayChooser.setBounds(650, 120, 210, 25);
  331.  
  332. jButton5.setText("OK");
  333. jPanel1.add(jButton5);
  334. jButton5.setBounds(760, 570, 100, 30);
  335.  
  336. countitems_txt.setEnabled(false);
  337. jPanel1.add(countitems_txt);
  338. countitems_txt.setBounds(760, 460, 100, 25);
  339. jPanel1.add(jTextField1);
  340. jTextField1.setBounds(650, 150, 210, 25);
  341.  
  342. IDcombo.setEditable(true);
  343. IDcombo.addActionListener(new java.awt.event.ActionListener() {
  344. public void actionPerformed(java.awt.event.ActionEvent evt) {
  345. IDcomboActionPerformed(evt);
  346. }
  347. });
  348. jPanel1.add(IDcombo);
  349. IDcombo.setBounds(130, 120, 150, 24);
  350. jPanel1.add(jTextField3);
  351. jTextField3.setBounds(760, 520, 100, 25);
  352.  
  353. jLabel9.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  354. jLabel9.setText("Date");
  355. jPanel1.add(jLabel9);
  356. jLabel9.setBounds(570, 120, 29, 17);
  357.  
  358. instock.setEditable(false);
  359. jPanel1.add(instock);
  360. instock.setBounds(130, 520, 190, 30);
  361.  
  362. showDruglist_btn.setText("Show Drug List");
  363. showDruglist_btn.addActionListener(new java.awt.event.ActionListener() {
  364. public void actionPerformed(java.awt.event.ActionEvent evt) {
  365. showDruglist_btnActionPerformed(evt);
  366. }
  367. });
  368. jPanel1.add(showDruglist_btn);
  369. showDruglist_btn.setBounds(370, 120, 130, 30);
  370.  
  371. exptxt.setEditable(false);
  372. jPanel1.add(exptxt);
  373. exptxt.setBounds(130, 480, 190, 30);
  374.  
  375. jLabel6.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  376. jLabel6.setText("Item");
  377. jPanel1.add(jLabel6);
  378. jLabel6.setBounds(50, 120, 40, 20);
  379.  
  380. add_btn.setText("Add");
  381. add_btn.addActionListener(new java.awt.event.ActionListener() {
  382. public void actionPerformed(java.awt.event.ActionEvent evt) {
  383. add_btnActionPerformed(evt);
  384. }
  385. });
  386. jPanel1.add(add_btn);
  387. add_btn.setBounds(130, 190, 100, 30);
  388.  
  389. explbl1.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
  390. explbl1.setText("In Stock");
  391. jPanel1.add(explbl1);
  392. explbl1.setBounds(50, 520, 70, 30);
  393.  
  394. explbl.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
  395. explbl.setText("exp date");
  396. jPanel1.add(explbl);
  397. explbl.setBounds(50, 480, 70, 30);
  398.  
  399. Namecombo.setEditable(true);
  400. Namecombo.setFont(new java.awt.Font("Dialog", 1, 12)); // NOI18N
  401. Namecombo.addActionListener(new java.awt.event.ActionListener() {
  402. public void actionPerformed(java.awt.event.ActionEvent evt) {
  403. NamecomboActionPerformed(evt);
  404. }
  405. });
  406. jPanel1.add(Namecombo);
  407. Namecombo.setBounds(130, 160, 370, 25);
  408.  
  409. tableSale.setModel(new javax.swing.table.DefaultTableModel(
  410. new Object [][] {
  411.  
  412. },
  413. new String [] {
  414. "ItemID", "Item Name", "Cost", "Qty", "Discount", "Total"
  415. }
  416. ) {
  417. Class[] types = new Class [] {
  418. java.lang.String.class, java.lang.String.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class
  419. };
  420. boolean[] canEdit = new boolean [] {
  421. false, false, true, true, true, true
  422. };
  423.  
  424. public Class getColumnClass(int columnIndex) {
  425. return types [columnIndex];
  426. }
  427.  
  428. public boolean isCellEditable(int rowIndex, int columnIndex) {
  429. return canEdit [columnIndex];
  430. }
  431. });
  432. tableSale.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
  433. tableSale.getTableHeader().setReorderingAllowed(false);
  434. tableSale.addKeyListener(new java.awt.event.KeyAdapter() {
  435. public void keyPressed(java.awt.event.KeyEvent evt) {
  436. tableSaleKeyPressed(evt);
  437. }
  438. });
  439. jScrollPane2.setViewportView(tableSale);
  440.  
  441. jPanel1.add(jScrollPane2);
  442. jScrollPane2.setBounds(50, 230, 810, 210);
  443.  
  444. jLabel8.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  445. jLabel8.setText("Total Price");
  446. jPanel1.add(jLabel8);
  447. jLabel8.setBounds(660, 520, 80, 17);
  448.  
  449. jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  450. jLabel3.setText("Invoice No");
  451. jPanel1.add(jLabel3);
  452. jLabel3.setBounds(570, 150, 70, 20);
  453. jPanel1.add(jTextField5);
  454. jTextField5.setBounds(760, 490, 100, 25);
  455.  
  456. jLabel2.setFont(new java.awt.Font("Calibri", 1, 24)); // NOI18N
  457. jLabel2.setText("Sales Invoice");
  458. jPanel1.add(jLabel2);
  459. jLabel2.setBounds(410, 60, 150, 50);
  460.  
  461. jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  462. jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/bit/project/plain_old.jpeg"))); // NOI18N
  463. jPanel1.add(jLabel1);
  464. jLabel1.setBounds(0, 0, 910, 620);
  465. jPanel1.add(jTextField4);
  466. jTextField4.setBounds(650, 400, 80, 20);
  467.  
  468. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  469. getContentPane().setLayout(layout);
  470. layout.setHorizontalGroup(
  471. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  472. .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 910, Short.MAX_VALUE)
  473. );
  474. layout.setVerticalGroup(
  475. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  476. .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 613, Short.MAX_VALUE)
  477. );
  478.  
  479. pack();
  480. setLocationRelativeTo(null);
  481. }// </editor-fold>
  482.  
  483. private void saleInvoiceHomeBtnActionPerformed(java.awt.event.ActionEvent evt) {
  484. this.setVisible(false);
  485. }
  486.  
  487. private void searchInvoiceBtnActionPerformed(java.awt.event.ActionEvent evt) {
  488. Findinvoice fiobj = new Findinvoice();
  489. fiobj.setVisible(true);
  490. }
  491.  
  492. private void NamecomboActionPerformed(java.awt.event.ActionEvent evt) {
  493. String drugname = (String) Namecombo.getSelectedItem();
  494. try{
  495.  
  496. String name = "SELECT * FROM druginfo WHERE ItemName LIKE '"+drugname+"%'";
  497.  
  498. PreparedStatement pstmt = conn.prepareStatement(name);
  499. ResultSet rs = pstmt.executeQuery();
  500.  
  501. while (rs.next()){
  502. IDcombo.setSelectedItem(rs.getString("ItemID"));
  503. }
  504.  
  505. }catch(Exception e){
  506. JOptionPane.showMessageDialog(null,"error "+ e);
  507. }
  508.  
  509.  
  510.  
  511. }
  512.  
  513. private void IDcomboActionPerformed(java.awt.event.ActionEvent evt) {
  514. String drugid = (String) IDcombo.getSelectedItem();
  515. try{
  516.  
  517. String name = "SELECT * FROM druginfo WHERE ItemID LIKE '"+drugid+"%'";
  518.  
  519. PreparedStatement pstmt = conn.prepareStatement(name);
  520. rs = pstmt.executeQuery();
  521.  
  522. while (rs.next()){
  523. Namecombo.setSelectedItem(rs.getString("ItemName"));
  524. }
  525.  
  526. }catch(Exception e){
  527. JOptionPane.showMessageDialog(null,"error "+ e);
  528. }
  529. try{
  530.  
  531. String exp = "SELECT ExpDate,InStock FROM druginfo WHERE ItemID LIKE '"+drugid+"%'";
  532. PreparedStatement pstmt = conn.prepareStatement(exp);
  533. ResultSet rs2 = pstmt.executeQuery();
  534.  
  535. while (rs2.next()){
  536. String date = rs2.getString("ExpDate");
  537. String stock = rs2.getString("InStock");
  538. exptxt.setText(date);
  539. instock.setText(stock);
  540.  
  541. }
  542.  
  543. }catch(Exception e){
  544. JOptionPane.showMessageDialog(null,"error "+ e);
  545. }
  546. }
  547.  
  548. private void showDruglist_btnActionPerformed(java.awt.event.ActionEvent evt) {
  549. try {
  550. NewSaleDruglist nsd = new NewSaleDruglist();
  551. nsd.setVisible(true);
  552. } catch (SQLException ex) {
  553. Logger.getLogger(Newsale.class.getName()).log(Level.SEVERE, null, ex);
  554. }
  555. }
  556.  
  557. private void add_btnActionPerformed(java.awt.event.ActionEvent evt) {
  558.  
  559. String temp = (String) IDcombo.getSelectedItem();
  560. String sql = "select ItemID,ItemName,CostPrice from druginfo where ItemID=?";
  561. try {
  562. pst=conn.prepareStatement(sql);
  563. pst.setString(1, temp);
  564.  
  565.  
  566. rs=pst.executeQuery();
  567. addDataToTable(tableSale,DbUtils.resultSetToTableModel(rs));
  568.  
  569. tableSale.getModel().addTableModelListener( new TableModelListener() {
  570.  
  571. @Override
  572. public void tableChanged(TableModelEvent e) {
  573. int row = e.getFirstRow();
  574. int column = e.getColumn();
  575. if (column == 6 ) {
  576. TableModel model = tableSale.getModel();
  577.  
  578. double cost = ( model.getValueAt(row, 3)==null)?0:((double)model.getValueAt(row, 3));
  579. double quantity = ( model.getValueAt(row, 4)==null)?0:((double) model.getValueAt(row, 4));
  580. double discount = ( model.getValueAt(row, 5)==null)?0:((double) model.getValueAt(row, 5));
  581.  
  582.  
  583. double total = new Double ((quantity * cost)-discount);
  584. System.out.println("pppppppp"+total);
  585. model.setValueAt(total, row, 6);
  586.  
  587. }
  588. }
  589.  
  590. });
  591.  
  592.  
  593. IDcombo.setSelectedItem(null);
  594. Namecombo.setSelectedItem(null);
  595. exptxt.setText(null);
  596. instock.setText(null);
  597.  
  598.  
  599.  
  600.  
  601. } catch (Exception ex) {
  602. JOptionPane.showMessageDialog(null, "errorrr"+ex);
  603. }
  604.  
  605. // int rows = tableSale.getRowCount();
  606. // countitems_txt.settext(Integer.parseInt();
  607.  
  608.  
  609. }
  610.  
  611. private void newSaleBtnActionPerformed(java.awt.event.ActionEvent evt) {
  612.  
  613. int clear = JOptionPane.showConfirmDialog(null, "Do you really want to clear the data ?","Clear Data",JOptionPane.YES_NO_OPTION);
  614.  
  615. if((clear)==0){
  616. addDataToTable(tableSale,DbUtils.resultSetToTableModel(null));
  617.  
  618.  
  619. }
  620. }
  621.  
  622. private void tableSaleKeyPressed(java.awt.event.KeyEvent evt) {
  623.  
  624.  
  625. tableSale.getModel().addTableModelListener( new TableModelListener() {
  626.  
  627. @Override
  628. public void tableChanged(TableModelEvent evt) {
  629. if (evt.getType() == TableModelEvent.UPDATE) {
  630.  
  631. int row = evt.getFirstRow();
  632. int column = evt.getColumn();
  633. if (column == 6 ) {
  634. TableModel model = tableSale.getModel();
  635.  
  636. double cost = ( model.getValueAt(row, 3)==null)?0:((Double)model.getValueAt(row, 3));
  637. double quantity = ( model.getValueAt(row, 4)==null)?0:((Double) model.getValueAt(row, 4));
  638. double discount = ( model.getValueAt(row, 5)==null)?0:((Double) model.getValueAt(row, 5));
  639.  
  640.  
  641. double total = new Double ((quantity * cost)-discount);
  642. // System.out.println("pppppppp"+total);
  643. model.setValueAt(total, row, 6);
  644.  
  645. }
  646. }
  647. }
  648. });
  649. }
  650.  
  651. /**
  652. * @param args the command line arguments
  653. */
  654. public static void main(String args[]) {
  655. /* Set the Nimbus look and feel */
  656. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  657. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  658. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  659. */
  660. try {
  661. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  662. if ("Nimbus".equals(info.getName())) {
  663. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  664. break;
  665. }
  666. }
  667. } catch (ClassNotFoundException ex) {
  668. java.util.logging.Logger.getLogger(Newsale.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  669. } catch (InstantiationException ex) {
  670. java.util.logging.Logger.getLogger(Newsale.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  671. } catch (IllegalAccessException ex) {
  672. java.util.logging.Logger.getLogger(Newsale.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  673. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  674. java.util.logging.Logger.getLogger(Newsale.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  675. }
  676. //</editor-fold>
  677. //</editor-fold>
  678.  
  679.  
  680.  
  681. /* Create and display the form */
  682. java.awt.EventQueue.invokeLater(new Runnable() {
  683. public void run() {
  684. try {
  685. new Newsale().setVisible(true);
  686. } catch (SQLException ex) {
  687. Logger.getLogger(Newsale.class.getName()).log(Level.SEVERE, null, ex);
  688. }
  689. }
  690. });
  691. }
  692.  
  693. // Variables declaration - do not modify
  694. private javax.swing.JComboBox IDcombo;
  695. private javax.swing.JComboBox Namecombo;
  696. private javax.swing.JButton add_btn;
  697. private javax.swing.JTextField countitems_txt;
  698. private com.toedter.calendar.JDateChooser dayChooser;
  699. private javax.swing.JLabel explbl;
  700. private javax.swing.JLabel explbl1;
  701. private javax.swing.JTextField exptxt;
  702. private javax.swing.JTextField instock;
  703. private javax.swing.JButton jButton5;
  704. private javax.swing.JDesktopPane jDesktopPane1;
  705. private javax.swing.JLabel jLabel1;
  706. private javax.swing.JLabel jLabel2;
  707. private javax.swing.JLabel jLabel3;
  708. private javax.swing.JLabel jLabel4;
  709. private javax.swing.JLabel jLabel5;
  710. private javax.swing.JLabel jLabel6;
  711. private javax.swing.JLabel jLabel7;
  712. private javax.swing.JLabel jLabel8;
  713. private javax.swing.JLabel jLabel9;
  714. private javax.swing.JPanel jPanel1;
  715. private javax.swing.JScrollPane jScrollPane2;
  716. private javax.swing.JTextField jTextField1;
  717. private javax.swing.JTextField jTextField3;
  718. private javax.swing.JTextField jTextField4;
  719. private javax.swing.JTextField jTextField5;
  720. private javax.swing.JButton newSaleBtn;
  721. private javax.swing.JButton saleInvoiceHelpBtn;
  722. private javax.swing.JButton saleInvoiceHomeBtn;
  723. private javax.swing.JButton saleInvoiceNextBtn;
  724. private javax.swing.JButton saleInvoicePreviousBtn;
  725. private javax.swing.JButton saleInvoicePrintBtn;
  726. private javax.swing.JButton searchInvoiceBtn;
  727. private javax.swing.JButton showDruglist_btn;
  728. private javax.swing.JTable tableSale;
  729. // End of variables declaration
  730.  
  731.  
  732.  
  733. }
Advertisement
Add Comment
Please, Sign In to add comment