Results 1 to 8 of 8
  1. #1
    tancy is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default HELP !!!!!!! URGENT !!!!! how to select on a specific row in JTable and delete it (wi

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.sql.*;
    import javax.swing.table.*;
    import java.util.Vector;
    import java.io.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
     
     
    public class TancyPart1test_11 extends JApplet implements ActionListener {
     
        JPanel allPanel = new JPanel();
        JPanel search = new JPanel();
        JPanel result = new JPanel();
        JPanel result1 = new JPanel();
        JPanel Panel = new JPanel();
        JPanel delete = new JPanel();
     
     
        JComboBox jlstFunction = new JComboBox(new String[]{"Product Code","Product Name"});
     
        JTextField jtfSearch = new JTextField("",20);
        JButton jbtSearch = new JButton("Search");
        JButton jbtDelete = new JButton("Delete");
     
     
     
        /*private String[] columnNames =    {"   Code   ", "ProductName", "Quantity", "Description","   Price(RM)   ","   Cost(RM)  "};
        private Object[][] rowData = {{"","","","","",""},{"","","","","",""},{"","","","","",""},
                                      {"","","","","",""},{"","","","","",""},{"","","","","",""},
                                      {"","","","","",""},{"","","","","",""},{"","","","","",""},
                                      {"","","","","",""},{"","","","","",""}};
     
     
        private JTable jTable1 = new JTable(rowData,columnNames); */
        Connection conn;
        ResultSet rs;
     
     
     
     
     
     
        public TancyPart1test_11(){
     
            allPanel.setLayout(new BorderLayout());
            search.setLayout(new FlowLayout(FlowLayout.LEFT));
            search.add(jlstFunction);
            search.add(jtfSearch);
            search.add(jbtSearch);
            search.add(jbtDelete);
            result.setLayout(new BorderLayout());
            /*result.add(new JScrollPane(jTable1),BorderLayout.CENTER);  
     
            TableColumn column1 = jTable1.getColumnModel().getColumn(0);
            column1.setPreferredWidth(20);        
            jTable1.setRowHeight(20); */
     
     
            /*DefaultTableModel tableModel = new DefaultTableModel();
            JTable jTable1 = new JTable(tableModel);*/
     
     
     
    Vector columnNames = new Vector();
    Vector data = new Vector();
    JPanel p=new JPanel();
    try {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection conn = DriverManager.getConnection("jdbc:odbc:product");
    String sql = "Select * from product";
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery( sql );
    ResultSetMetaData md = rs.getMetaData();
    int columns = md.getColumnCount();
    for (int i = 1; i <= columns; i++) {
    columnNames.addElement( md.getColumnName(i) );
    }
    while (rs.next()) {
    Vector row = new Vector(columns);
    for (int i = 1; i <= columns; i++){
    row.addElement( rs.getObject(i) );
    }
    data.addElement( row );
    }
    rs.close();
    stmt.close();
    }
    catch(Exception e){
    System.out.println(e);
    }
    JTable jTable1 = new JTable(data,columnNames);
    DefaultTableModel tableModel = new DefaultTableModel();
     
     
     
    TableColumn col;
    for (int i = 0; i < jTable1.getColumnCount(); i++) {
    col = jTable1.getColumnModel().getColumn(i);
    col.setMaxWidth(250); 
    }
     
            result.add(new JScrollPane(jTable1),BorderLayout.CENTER); 
            Panel.setLayout(new BorderLayout());
            Panel.add(search,BorderLayout.NORTH);
            Panel.add(result,BorderLayout.CENTER);        
            allPanel.add(Panel,BorderLayout.NORTH);
            add(allPanel);
     
            DBConnect();
     
            jbtSearch.addActionListener(this);
            jbtDelete.addActionListener(this);
     
     
     
     
        }
        public static void main(String []args ){
     
        TancyPart1test_11 applet = new TancyPart1test_11();
        JFrame frame = new JFrame();
        frame.setSize(900,500);
        frame.setTitle("Delete Part");
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        applet.init();
        applet.start();
        frame.getContentPane().add(applet);
     
        }
     
          /*private Vector getColumnNames() {
        Vector<String> columnNames = new Vector<String>();
     
        for (int i = 0; i < jTable1.getColumnCount(); i++)
          columnNames.add(jTable1.getColumnName(i));
     
        return columnNames;
      }*/
     
        private void DBConnect() {
             try{
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                conn = DriverManager.getConnection("jdbc:odbc:Product");
     
            }catch(  ClassNotFoundException | SQLException ex){
                JOptionPane.showMessageDialog(null, ex.toString());
            }
        }
     
        public void actionPerformed(ActionEvent e) {
     
            if(e.getSource()==jbtSearch){
                String function = (String) jlstFunction.getSelectedItem();
                String code = jtfSearch.getText();
                if("".equals(code)){
                    JOptionPane.showMessageDialog(null, "Code must within 4 digit and at least one...","error",JOptionPane.INFORMATION_MESSAGE);
                }
                else if(function.equalsIgnoreCase("Product Code")){
     
                    try {
                        Statement stmtDelete;
                        stmtDelete = conn.createStatement();
                        stmtDelete.executeUpdate("DELETE FROM Product WHERE productCode ='"+jtfSearch.getText()+"' ;");
                    } catch (SQLException ex) {
                        Logger.getLogger(TancyPart1test_1.class.getName()).log(Level.SEVERE, null, ex);
                    }
     
                }
     
     
     
            }
     
     
     
     
     
     
        else if(e.getSource()==jbtDelete){
     
                try{   
                String function1 = (String) jlstFunction.getSelectedItem();
                String coding = jtfSearch.getText();
                if("".equals(coding)){
                    JOptionPane.showMessageDialog(null, "No Record Deleted...","ERROR",JOptionPane.INFORMATION_MESSAGE);
                }
                else if(function1.equalsIgnoreCase("Product Name")){
                     /*if (jTable1.getSelectedRow() >= 0)
                     tableModel.removeRow(jTable1.getSelectedRow()); */
     
     
                }
     
     
     
     
     
     
        }
                 catch(Exception ex){
                    JOptionPane.showMessageDialog(null, ex.toString());
                }
     
     
     
    }
        }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: HELP !!!!!!! URGENT !!!!! how to select on a specific row in JTable and delete it

    Moved from a staff-only section.

    Please read this.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    tancy is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Re: HELP !!!!!!! URGENT !!!!! how to select on a specific row in JTable and delete it

    dont know what u meaning :( can u help me ?? :(

    HELP !!!!!!! URGENT !!!!! how to select on a specific row in JTable and delete it (with also changes in database microsoft access) , the JTable i create already retrieve the data from microsoft access file

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.sql.*;
    import javax.swing.table.*;
    import java.util.Vector;
    import java.io.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    
    public class TancyPart1test_11 extends JApplet implements ActionListener {
        
        JPanel allPanel = new JPanel();
        JPanel search = new JPanel();
        JPanel result = new JPanel();
        JPanel result1 = new JPanel();
        JPanel Panel = new JPanel();
        JPanel delete = new JPanel();
        
        
        JComboBox jlstFunction = new JComboBox(new String[]{"Product Code","Product Name"});
        
        JTextField jtfSearch = new JTextField("",20);
        JButton jbtSearch = new JButton("Search");
        JButton jbtDelete = new JButton("Delete");
        
        
        
        /*private String[] columnNames =    {"   Code   ", "ProductName", "Quantity", "Description","   Price(RM)   ","   Cost(RM)  "};
        private Object[][] rowData = {{"","","","","",""},{"","","","","",""},{"","","","","",""},
                                      {"","","","","",""},{"","","","","",""},{"","","","","",""},
                                      {"","","","","",""},{"","","","","",""},{"","","","","",""},
                                      {"","","","","",""},{"","","","","",""}};
        
         
        private JTable jTable1 = new JTable(rowData,columnNames); */
        Connection conn;
        ResultSet rs;
        
       
     
            
            
        
        public TancyPart1test_11(){
        
            allPanel.setLayout(new BorderLayout());
            search.setLayout(new FlowLayout(FlowLayout.LEFT));
            search.add(jlstFunction);
            search.add(jtfSearch);
            search.add(jbtSearch);
            search.add(jbtDelete);
            result.setLayout(new BorderLayout());
            /*result.add(new JScrollPane(jTable1),BorderLayout.CENTER);  
            
            TableColumn column1 = jTable1.getColumnModel().getColumn(0);
            column1.setPreferredWidth(20);        
            jTable1.setRowHeight(20); */
            
            
            /*DefaultTableModel tableModel = new DefaultTableModel();
            JTable jTable1 = new JTable(tableModel);*/
            
          
            
    Vector columnNames = new Vector();
    Vector data = new Vector();
    JPanel p=new JPanel();
    try {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection conn = DriverManager.getConnection("jdbc:odbc:product");
    String sql = "Select * from product";
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery( sql );
    ResultSetMetaData md = rs.getMetaData();
    int columns = md.getColumnCount();
    for (int i = 1; i <= columns; i++) {
    columnNames.addElement( md.getColumnName(i) );
    }
    while (rs.next()) {
    Vector row = new Vector(columns);
    for (int i = 1; i <= columns; i++){
    row.addElement( rs.getObject(i) );
    }
    data.addElement( row );
    }
    rs.close();
    stmt.close();
    }
    catch(Exception e){
    System.out.println(e);
    }
    JTable jTable1 = new JTable(data,columnNames);
    DefaultTableModel tableModel = new DefaultTableModel();
    
    
    
    TableColumn col;
    for (int i = 0; i < jTable1.getColumnCount(); i++) {
    col = jTable1.getColumnModel().getColumn(i);
    col.setMaxWidth(250); 
    }
            
            result.add(new JScrollPane(jTable1),BorderLayout.CENTER); 
            Panel.setLayout(new BorderLayout());
            Panel.add(search,BorderLayout.NORTH);
            Panel.add(result,BorderLayout.CENTER);        
            allPanel.add(Panel,BorderLayout.NORTH);
            add(allPanel);
        
            DBConnect();
            
            jbtSearch.addActionListener(this);
            jbtDelete.addActionListener(this);
        
        
        
        
        }
        public static void main(String []args ){
        
        TancyPart1test_11 applet = new TancyPart1test_11();
        JFrame frame = new JFrame();
        frame.setSize(900,500);
        frame.setTitle("Delete Part");
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        applet.init();
        applet.start();
        frame.getContentPane().add(applet);
        
        }
        
          /*private Vector getColumnNames() {
        Vector<String> columnNames = new Vector<String>();
    
        for (int i = 0; i < jTable1.getColumnCount(); i++)
          columnNames.add(jTable1.getColumnName(i));
    
        return columnNames;
      }*/
    
        private void DBConnect() {
             try{
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                conn = DriverManager.getConnection("jdbc:odbc:Product");
    
            }catch(  ClassNotFoundException | SQLException ex){
                JOptionPane.showMessageDialog(null, ex.toString());
            }
        }
        
        public void actionPerformed(ActionEvent e) {
            
            if(e.getSource()==jbtSearch){
                String function = (String) jlstFunction.getSelectedItem();
                String code = jtfSearch.getText();
                if("".equals(code)){
                    JOptionPane.showMessageDialog(null, "Code must within 4 digit and at least one...","error",JOptionPane.INFORMATION_MESSAGE);
                }
                else if(function.equalsIgnoreCase("Product Code")){
                     
                    try {
                        Statement stmtDelete;
                        stmtDelete = conn.createStatement();
                        stmtDelete.executeUpdate("DELETE FROM Product WHERE productCode ='"+jtfSearch.getText()+"' ;");
                    } catch (SQLException ex) {
                        Logger.getLogger(TancyPart1test_1.class.getName()).log(Level.SEVERE, null, ex);
                    }
                     
                }
               
                
                
            }
            
            
                    
            
            
        
        else if(e.getSource()==jbtDelete){
                
                try{   
                String function1 = (String) jlstFunction.getSelectedItem();
                String coding = jtfSearch.getText();
                if("".equals(coding)){
                    JOptionPane.showMessageDialog(null, "No Record Deleted...","ERROR",JOptionPane.INFORMATION_MESSAGE);
                }
                else if(function1.equalsIgnoreCase("Product Name")){
                     /*if (jTable1.getSelectedRow() >= 0)
                     tableModel.removeRow(jTable1.getSelectedRow()); */
                    
                     
                }
                
               
                
    
        
            
        }
                 catch(Exception ex){
                    JOptionPane.showMessageDialog(null, ex.toString());
                }
       
         
         
    }
        }    
        
    }

  4. #4
    tancy is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Re: HELP !!!!!!! URGENT !!!!! how to select on a specific row in JTable and delete it

    please help me :(

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: HELP !!!!!!! URGENT !!!!! how to select on a specific row in JTable and delete it

    JTable has a getSelectedRow() method that will return the index of the row selected.
    I can't be sure in your code (not your fault, entirely, the forum seems to not handle block comments), but it looks like you have a delete button.
    So in the action listener for that button find out what row is selected in the table and then use that to grab whatever data it is you need to know to run a DELETE statement against the database.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: HELP !!!!!!! URGENT !!!!! how to select on a specific row in JTable and delete it

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: HELP !!!!!!! URGENT !!!!! how to select on a specific row in JTable and delete it

    The original poster seems to have difficulty following any sort of directions including suggestions given to him here and elsewhere. This does not bode well if he plans a future career in programming.
    Last edited by Fubarable; 11-28-2012 at 05:12 AM.

Similar Threads

  1. get the select row from a jtable
    By AARON22 in forum Java Applets
    Replies: 1
    Last Post: 05-21-2011, 05:32 PM
  2. delete a row from jtable
    By javanew in forum NetBeans
    Replies: 2
    Last Post: 04-26-2011, 04:27 PM
  3. Select a row in Jtable
    By pink123 in forum AWT / Swing
    Replies: 5
    Last Post: 03-21-2011, 03:04 PM
  4. How to: Select specific array indices?
    By louist in forum New To Java
    Replies: 3
    Last Post: 03-07-2011, 10:21 PM
  5. Select specific cell
    By Echilon in forum New To Java
    Replies: 1
    Last Post: 01-01-2008, 07:47 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •