Advertisement
Guest User

Untitled

a guest
Aug 18th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.62 KB | None | 0 0
  1. //class tab_items
  2.  
  3. public class tab_items extends JInternalFrame implements MouseListener,DocumentListener,ActionListener{
  4.  
  5. DS co= new DS ();
  6. JPanel panel1;
  7. JScrollPane pane;
  8. DefaultTableModel model= new DefaultTableModel();
  9. JTable tbl= new JTable(model);
  10. JLabel lbl_search;
  11. JTextField txt_search;
  12. JPopupMenu pop;
  13. JMenuItem mi_add,mi_edit, mi_del;
  14.  
  15. public tab_items(){
  16.  
  17.  
  18. panel1= new JPanel();
  19. panel1.setLayout(null);
  20.  
  21. //popupmenu
  22. pop=new JPopupMenu();
  23. mi_add=new JMenuItem("Add new record");
  24. mi_edit=new JMenuItem("Edit record");
  25. mi_del=new JMenuItem("Delete record");
  26. add(pop);
  27.  
  28. pop.add(mi_add);
  29. mi_add.addActionListener(this);
  30. mi_add.addMouseListener(this);
  31. pop.add(mi_edit);
  32. mi_edit.addActionListener(this);
  33.  
  34. pop.add(mi_del);
  35. mi_del.addActionListener(this);
  36.  
  37. // try{
  38. // UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  39. // }catch(Exception ex){}
  40.  
  41. initcomponents();
  42. setVisible(true);
  43. setSize(700,500);
  44. setLocation(100,150);
  45. setTitle("List Of Items");
  46. setDefaultCloseOperation(EXIT_ON_CLOSE);
  47.  
  48.  
  49. }
  50.  
  51. public void initcomponents(){
  52.  
  53. try{
  54. model.addColumn("ID");
  55. model.addColumn("Item Name");
  56. model.addColumn("Cost Price");
  57. model.addColumn("Selling Price");
  58. model.addColumn("Qty");
  59.  
  60. tbl.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  61. PreparedStatement pstat=co.con.prepareStatement("Select * from tbl_items ORDER BY itemid ASC");
  62. ResultSet rs=pstat.executeQuery();
  63. while(rs.next()){
  64. model.addRow(new Object[]{rs.getInt(1), rs.getString(2).trim(), rs.getString(3).trim(), rs.getString(4).trim(),rs.getInt(5)});
  65. }
  66. }catch(Exception ex){}
  67.  
  68. // SETTING COLUMN WIDTH
  69. TableColumnModel model=tbl.getColumnModel();
  70. model.getColumn(0).setPreferredWidth(50);
  71. model.getColumn(1).setPreferredWidth(400);
  72. model.getColumn(2).setPreferredWidth(100);
  73. model.getColumn(3).setPreferredWidth(100);
  74. model.getColumn(4).setPreferredWidth(50);
  75.  
  76. // adding panel
  77.  
  78. add(panel1);
  79. panel1.setBounds(0,0,800,600);
  80.  
  81. //scrollpane initialize
  82. int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
  83. int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
  84. pane=new JScrollPane(tbl, v, h); //table included in scrollpane
  85. panel1.add(pane);
  86. pane.setBounds(0,50,700,400);
  87. tbl.addMouseListener(this);
  88.  
  89. lbl_search= new JLabel();
  90. lbl_search.setIcon(new ImageIcon(getClass().getResource("/img/btn_search_up.png")));
  91. panel1.add(lbl_search);
  92. lbl_search.setBounds(5,10,25,20);
  93.  
  94. txt_search=new JTextField();
  95. panel1.add(txt_search);
  96. txt_search.setBounds(35,10,200,20);
  97. txt_search.getDocument().addDocumentListener(this);
  98. /*
  99. btn_add= new JButton();
  100. btn_add.setIcon(new ImageIcon(getClass().getResource("/img/add-button-md.png")));
  101. // btn_add.setHorizontalAlignment(SwingConstants.CENTER);
  102. // btn_add.setText("Add");
  103.  
  104. add(btn_add);
  105. btn_add.setBounds(665, 125, 65, 65);
  106.  
  107. btn_edit= new JButton();
  108. btn_edit.setText("Update");
  109. add(btn_edit);
  110. btn_edit.setBounds(665, 200, 65, 65);
  111.  
  112. btn_delete=new JButton();
  113. btn_delete.setText("Delete");
  114. add(btn_delete);
  115. btn_delete.setBounds(665, 275, 65, 65);
  116. */
  117.  
  118. }
  119.  
  120. public void mouseClicked(MouseEvent e) {}
  121. public void mousePressed(MouseEvent me){maybeShowPopup(me);}
  122. public void mouseReleased(MouseEvent me){maybeShowPopup(me);}
  123. public void mouseEntered(MouseEvent e) {}
  124. public void mouseExited(MouseEvent e) {}
  125. private void maybeShowPopup(MouseEvent e){
  126. if (e.isPopupTrigger()){
  127. pop.show(e.getComponent(),e.getX(), e.getY());
  128. }
  129. }
  130.  
  131. public void update(DocumentEvent de){
  132.  
  133. Document doc=(Document)de.getDocument();
  134. int length=doc.getLength();
  135. String str=null;
  136. try
  137. {
  138. str=doc.getText(0,length);
  139. }
  140. catch(Exception ex)
  141. {
  142. JOptionPane.showMessageDialog(null, "Error in retreiving Search Lengthn"+ex);
  143. }
  144.  
  145. try
  146. {
  147. Class.forName("com.mysql.jdbc.Driver");
  148. Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/dstore","root","");
  149. PreparedStatement pstat=con.prepareStatement("select * from tbl_items where itemid LIKe '"+str+"%' OR itemname LIKe '"+str+"%' ORDER BY itemid ASC");
  150. ResultSet rs=pstat.executeQuery();
  151. model.setRowCount(0);
  152. while(rs.next()){
  153. model.addRow(new Object[]{rs.getInt(1), rs.getString(2).trim(), rs.getString(3).trim(), rs.getString(4).trim(),rs.getInt(5)});
  154. }
  155.  
  156. }
  157. catch(Exception ex)
  158. {
  159. JOptionPane.showMessageDialog(null,"ERROR IN DOCUMENT LISTENER.nCannot Fetch Records"+ex);
  160. }
  161.  
  162. }
  163. public void insertUpdate(DocumentEvent de) {
  164. update(de);
  165. }
  166. public void removeUpdate(DocumentEvent de) {
  167. update(de);
  168. }
  169. public void changedUpdate(DocumentEvent de) {
  170. update(de);
  171. }
  172.  
  173. public void actionPerformed(ActionEvent ae) {
  174.  
  175.  
  176. if(ae.getSource()==mi_add){
  177.  
  178. // frm_add_item ob= new frm_add_item();
  179. // ob.show();
  180. // dp.add(ob);
  181.  
  182. }else if(ae.getSource()==mi_edit) {
  183.  
  184.  
  185. }else {
  186.  
  187.  
  188. }
  189.  
  190. }
  191.  
  192. }
  193.  
  194. #//class frm_main ( it contains desktop pane)#
  195.  
  196. import java.awt.Dimension;
  197. import java.awt.event.ActionEvent;
  198. import java.beans.PropertyVetoException;
  199. import javax.swing.AbstractAction;
  200. import javax.swing.JButton;
  201. import javax.swing.JDesktopPane;
  202. import javax.swing.JFrame;
  203. import javax.swing.JInternalFrame;
  204. import javax.swing.JMenuItem;
  205. import javax.swing.JPanel;
  206. import javax.swing.JPopupMenu;
  207. import javax.swing.SwingUtilities;
  208.  
  209. //** @see http://stackoverflow.com/a/18556224/230513 */
  210. public class Test {
  211.  
  212. private JDesktopPane jdp = new JDesktopPane() {
  213. @Override
  214. public Dimension getPreferredSize() {
  215. return new Dimension(600, 400);
  216. }
  217. };
  218. private NewAction newAction = new NewAction("New");
  219.  
  220. public Test() {
  221. createAndShowGUI();
  222. }
  223.  
  224. public static void main(String[] args) {
  225. SwingUtilities.invokeLater(new Runnable() {
  226. @Override
  227. public void run() {
  228. new Test();
  229. }
  230. });
  231. }
  232.  
  233. private void createAndShowGUI() {
  234. JFrame frame = new JFrame();
  235. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  236. newAction.actionPerformed(null);
  237. frame.add(jdp);
  238. frame.pack();
  239. frame.setVisible(true);
  240. }
  241.  
  242. private void createInternalFrame(int x, int y) {
  243. final JInternalFrame jif =
  244. new JInternalFrame("Test" + x, true, true, true, true);
  245. jif.setLocation(x, y);
  246. JPanel jp = new JPanel() {
  247. @Override
  248. public Dimension getPreferredSize() {
  249. return new Dimension(256, 128);
  250. }
  251. };
  252. JPopupMenu popup = new JPopupMenu();
  253. jp.setComponentPopupMenu(popup);
  254. popup.add(new JMenuItem(newAction));
  255. jp.add(new JButton(newAction));
  256. jif.add(jp);
  257. jif.pack();
  258. jdp.add(jif);
  259. jif.setVisible(true);
  260. try {
  261. jif.setSelected(true);
  262. } catch (PropertyVetoException e) {
  263. e.printStackTrace(System.err);
  264. }
  265. }
  266.  
  267. private class NewAction extends AbstractAction {
  268.  
  269. private int i;
  270.  
  271. public NewAction(String name) {
  272. super(name);
  273. }
  274.  
  275. @Override
  276. public void actionPerformed(ActionEvent ae) {
  277. i++;
  278. createInternalFrame(50 * i, 50 * i);
  279.  
  280. }
  281. }
  282. }
  283.  
  284. JdesktopPane.add(JInternalFrame_obj1);
  285. JInternalFrame_obj1.toFront();
  286.  
  287. JdesktopPane.add(JInternalFrame_obj1);
  288. JInternalFrame_obj1.toFront();
  289.  
  290. this.getParent().add(JInternalFrame_obj12);
  291. JInternalFrame_obj2.toFront();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement