Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //class tab_items
- public class tab_items extends JInternalFrame implements MouseListener,DocumentListener,ActionListener{
- DS co= new DS ();
- JPanel panel1;
- JScrollPane pane;
- DefaultTableModel model= new DefaultTableModel();
- JTable tbl= new JTable(model);
- JLabel lbl_search;
- JTextField txt_search;
- JPopupMenu pop;
- JMenuItem mi_add,mi_edit, mi_del;
- public tab_items(){
- panel1= new JPanel();
- panel1.setLayout(null);
- //popupmenu
- pop=new JPopupMenu();
- mi_add=new JMenuItem("Add new record");
- mi_edit=new JMenuItem("Edit record");
- mi_del=new JMenuItem("Delete record");
- add(pop);
- pop.add(mi_add);
- mi_add.addActionListener(this);
- mi_add.addMouseListener(this);
- pop.add(mi_edit);
- mi_edit.addActionListener(this);
- pop.add(mi_del);
- mi_del.addActionListener(this);
- // try{
- // UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
- // }catch(Exception ex){}
- initcomponents();
- setVisible(true);
- setSize(700,500);
- setLocation(100,150);
- setTitle("List Of Items");
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- }
- public void initcomponents(){
- try{
- model.addColumn("ID");
- model.addColumn("Item Name");
- model.addColumn("Cost Price");
- model.addColumn("Selling Price");
- model.addColumn("Qty");
- tbl.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
- PreparedStatement pstat=co.con.prepareStatement("Select * from tbl_items ORDER BY itemid ASC");
- ResultSet rs=pstat.executeQuery();
- while(rs.next()){
- model.addRow(new Object[]{rs.getInt(1), rs.getString(2).trim(), rs.getString(3).trim(), rs.getString(4).trim(),rs.getInt(5)});
- }
- }catch(Exception ex){}
- // SETTING COLUMN WIDTH
- TableColumnModel model=tbl.getColumnModel();
- model.getColumn(0).setPreferredWidth(50);
- model.getColumn(1).setPreferredWidth(400);
- model.getColumn(2).setPreferredWidth(100);
- model.getColumn(3).setPreferredWidth(100);
- model.getColumn(4).setPreferredWidth(50);
- // adding panel
- add(panel1);
- panel1.setBounds(0,0,800,600);
- //scrollpane initialize
- int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
- int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
- pane=new JScrollPane(tbl, v, h); //table included in scrollpane
- panel1.add(pane);
- pane.setBounds(0,50,700,400);
- tbl.addMouseListener(this);
- lbl_search= new JLabel();
- lbl_search.setIcon(new ImageIcon(getClass().getResource("/img/btn_search_up.png")));
- panel1.add(lbl_search);
- lbl_search.setBounds(5,10,25,20);
- txt_search=new JTextField();
- panel1.add(txt_search);
- txt_search.setBounds(35,10,200,20);
- txt_search.getDocument().addDocumentListener(this);
- /*
- btn_add= new JButton();
- btn_add.setIcon(new ImageIcon(getClass().getResource("/img/add-button-md.png")));
- // btn_add.setHorizontalAlignment(SwingConstants.CENTER);
- // btn_add.setText("Add");
- add(btn_add);
- btn_add.setBounds(665, 125, 65, 65);
- btn_edit= new JButton();
- btn_edit.setText("Update");
- add(btn_edit);
- btn_edit.setBounds(665, 200, 65, 65);
- btn_delete=new JButton();
- btn_delete.setText("Delete");
- add(btn_delete);
- btn_delete.setBounds(665, 275, 65, 65);
- */
- }
- public void mouseClicked(MouseEvent e) {}
- public void mousePressed(MouseEvent me){maybeShowPopup(me);}
- public void mouseReleased(MouseEvent me){maybeShowPopup(me);}
- public void mouseEntered(MouseEvent e) {}
- public void mouseExited(MouseEvent e) {}
- private void maybeShowPopup(MouseEvent e){
- if (e.isPopupTrigger()){
- pop.show(e.getComponent(),e.getX(), e.getY());
- }
- }
- public void update(DocumentEvent de){
- Document doc=(Document)de.getDocument();
- int length=doc.getLength();
- String str=null;
- try
- {
- str=doc.getText(0,length);
- }
- catch(Exception ex)
- {
- JOptionPane.showMessageDialog(null, "Error in retreiving Search Lengthn"+ex);
- }
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/dstore","root","");
- PreparedStatement pstat=con.prepareStatement("select * from tbl_items where itemid LIKe '"+str+"%' OR itemname LIKe '"+str+"%' ORDER BY itemid ASC");
- ResultSet rs=pstat.executeQuery();
- model.setRowCount(0);
- while(rs.next()){
- model.addRow(new Object[]{rs.getInt(1), rs.getString(2).trim(), rs.getString(3).trim(), rs.getString(4).trim(),rs.getInt(5)});
- }
- }
- catch(Exception ex)
- {
- JOptionPane.showMessageDialog(null,"ERROR IN DOCUMENT LISTENER.nCannot Fetch Records"+ex);
- }
- }
- public void insertUpdate(DocumentEvent de) {
- update(de);
- }
- public void removeUpdate(DocumentEvent de) {
- update(de);
- }
- public void changedUpdate(DocumentEvent de) {
- update(de);
- }
- public void actionPerformed(ActionEvent ae) {
- if(ae.getSource()==mi_add){
- // frm_add_item ob= new frm_add_item();
- // ob.show();
- // dp.add(ob);
- }else if(ae.getSource()==mi_edit) {
- }else {
- }
- }
- }
- #//class frm_main ( it contains desktop pane)#
- import java.awt.Dimension;
- import java.awt.event.ActionEvent;
- import java.beans.PropertyVetoException;
- import javax.swing.AbstractAction;
- import javax.swing.JButton;
- import javax.swing.JDesktopPane;
- import javax.swing.JFrame;
- import javax.swing.JInternalFrame;
- import javax.swing.JMenuItem;
- import javax.swing.JPanel;
- import javax.swing.JPopupMenu;
- import javax.swing.SwingUtilities;
- //** @see http://stackoverflow.com/a/18556224/230513 */
- public class Test {
- private JDesktopPane jdp = new JDesktopPane() {
- @Override
- public Dimension getPreferredSize() {
- return new Dimension(600, 400);
- }
- };
- private NewAction newAction = new NewAction("New");
- public Test() {
- createAndShowGUI();
- }
- public static void main(String[] args) {
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- new Test();
- }
- });
- }
- private void createAndShowGUI() {
- JFrame frame = new JFrame();
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- newAction.actionPerformed(null);
- frame.add(jdp);
- frame.pack();
- frame.setVisible(true);
- }
- private void createInternalFrame(int x, int y) {
- final JInternalFrame jif =
- new JInternalFrame("Test" + x, true, true, true, true);
- jif.setLocation(x, y);
- JPanel jp = new JPanel() {
- @Override
- public Dimension getPreferredSize() {
- return new Dimension(256, 128);
- }
- };
- JPopupMenu popup = new JPopupMenu();
- jp.setComponentPopupMenu(popup);
- popup.add(new JMenuItem(newAction));
- jp.add(new JButton(newAction));
- jif.add(jp);
- jif.pack();
- jdp.add(jif);
- jif.setVisible(true);
- try {
- jif.setSelected(true);
- } catch (PropertyVetoException e) {
- e.printStackTrace(System.err);
- }
- }
- private class NewAction extends AbstractAction {
- private int i;
- public NewAction(String name) {
- super(name);
- }
- @Override
- public void actionPerformed(ActionEvent ae) {
- i++;
- createInternalFrame(50 * i, 50 * i);
- }
- }
- }
- JdesktopPane.add(JInternalFrame_obj1);
- JInternalFrame_obj1.toFront();
- JdesktopPane.add(JInternalFrame_obj1);
- JInternalFrame_obj1.toFront();
- this.getParent().add(JInternalFrame_obj12);
- JInternalFrame_obj2.toFront();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement