Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: jTable trouble...

  1. #1
    Member
    Join Date
    Dec 2010
    Posts
    69
    My Mood
    Busy
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default jTable trouble...

    If I have a jTable that is populated with data when the program begins, and people can "Add" button, "Delete" button, and there is a "Refresh" button (part of the issue, this is used to re-populate the jTable). How do I completely erase all data in the jTable and get the focus to be at row 0 and col 0 again?


    I've tried going through the thing and doing a .setValueAt("", row, col); but cant figure out how to reset the "cursor" to be at that 0,0 spot?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: jTable trouble...

    Perhaps post some code (preferably SSCCE form), which might help demonstrate how you are using the JTable. If you just wish to reset the selection, alter the selectionmodel
    table.getSelectionModel().setSeletionInterval(0,0);

  3. #3
    Member
    Join Date
    Dec 2010
    Posts
    69
    My Mood
    Busy
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: jTable trouble...

    (I was currently looking into possibly using the DefaultTableModel better so I could just repaint() the damn thing easy but it is extremely hard to complete using NetBeans and there is a lack of documentation on exactly how to do it with NetBeans.)

    Anywho... this is my Refresh button -
        private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {                                           
     
     
            doDelete();
        }                                          
     
    public void doDelete(){
        int i = 0;
        int r = 0;
     
        while (i < 600) {
            jTable1.remove(r);
     
            r++;i++;
        }
            doRefresh();
        }
     
     public void doRefresh(){
     
                try {
                String url = "jdbc:odbc:dts";
                String user = "";
                String password = "";
                Connection con = DriverManager.getConnection(url, user, password);
     
                String getCust = "SELECT *  FROM Customer";
                PreparedStatement ps = con.prepareStatement(getCust);
                ResultSet rs = ps.executeQuery();
     
                while (rs.next()){
                    String s = rs.getString(1);
                    jTable1.setValueAt(s, R, 0);
                    s = rs.getString(2);
                    jTable1.setValueAt(s, R, 1);
                    s = rs.getString(3);
                    jTable1.setValueAt(s, R, 2);
                    s = rs.getString(4);
                    jTable1.setValueAt(s, R, 3);
                    s = rs.getString(5);
                    jTable1.setValueAt(s, R, 4);
                    s = rs.getString(6);
                    jTable1.setValueAt(s, R, 5);
                    s = rs.getString(7);
                    jTable1.setValueAt(s, R, 6);
                    s = rs.getString(8);
                    jTable1.setValueAt(s, R, 7);
                    R++;
     
     
               }
     
            } catch (Exception e) {
                lblConnect.setText("Error in doRefresh()");
            }
    }
    This is my Delete button -
       private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {                                          
     
            int row = jTable1.getSelectedRow();
     
            String value = (String)jTable1.getValueAt(row, 0);
            try{
                String url = "jdbc:odbc:dts";
                String user = "";
                String password = "";
                Connection con = DriverManager.getConnection(url, user, password);
            if ( row >= 0 ){
     
            String delCust = "DELETE FROM Customer WHERE FirstName = ?";
            PreparedStatement ps = con.prepareStatement(delCust);
            ps.setString(1, value);
            ps.executeUpdate();
                }
     
           }// end try
            catch(Exception e){
                lblConnect.setText("Failed to delete entry");
            }
     
        }

    The main issue:

    If I have two entries in the Database (displayed in the jTable) and I click one, and press Delete, it deletes it from the database without making any visual impact into the jTable, then you click Refresh, and it simply appends (to the bottom of the two other entries) the one that wasn't deleted(because it is now the only entry in the DB)


    I just can't figure out how to simply completely erase all data in the jTable and reset it to start putting in data starting at row 0 col 0.

    I tried your code you said to try and it gave me and error with the ".setSeletionInterval(0,0);" portion of it saying it couldn't find the Symbol: method setSelectionInterval(int, int);



    :EDIT: in the event of me messing with some coding, it currently doesn't do any refreshing at all right now and changes arent made until the program restarts
    Last edited by _lithium_; March 6th, 2011 at 12:51 PM.

  4. #4
    Member
    Join Date
    Dec 2010
    Posts
    69
    My Mood
    Busy
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: jTable trouble...

    Ok, I've decided to switch how I store the data for the jTable, but no im running into a shit ton of issues. When I call my getData() it says "Overridable method call in constructor" then when I try to run the program, it says an error with "init" which i see is from the initComponents()... here is the code im messing with, please i need help before I die from overworking on this lol:
    public class DeedsTechView extends FrameView{
    int R = 0;
          String header[] = new String[]{
                "First Name", "Last Name","Address", "City","State","Zip","Phone", "Email"
            };
     
    private Vector<Vector<String>> data; //used for data from database
     
     
        public DeedsTechView(SingleFrameApplication app) {
     
            super(app);
            initComponents();
            try{
               String url = "jdbc:odbc:dts";
                String user = "";
                String password = "";
                Connection con = DriverManager.getConnection(url, user, password);
                String getCust = "SELECT *  FROM Customer";
                PreparedStatement ps = con.prepareStatement(getCust);
                ResultSet rs = ps.executeQuery();
                data = getData(con, rs);
     
            }
            catch(Exception e){
                System.out.println("Still crasssshinnngg!");
            }
     
     
     
        }
     
     
     
     
        @Action
        public Vector getData(Connection con, ResultSet rs){
            Vector<Vector<String>> customerData = new Vector<Vector<String>>();
               try{
                while (rs.next()){
                    Vector<String> customer = new Vector<String>();
                    customer.add(rs.getString(1));
                    customer.add(rs.getString(2));
                    customer.add(rs.getString(3));
                    customer.add(rs.getString(4));
                    customer.add(rs.getString(5));
                   customer.add(rs.getString(6));
                    customer.add(rs.getString(7));
                    customer.add(rs.getString(8));
                    customerData.add(customer);
                }
                return customerData;
            }
               catch (Exception e){
                   System.out.print("ERROR IN getData()");
                   return null;
               }
     
            }

Similar Threads

  1. trouble with GUI
    By MrHardRock in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 7th, 2011, 04:00 PM
  2. [SOLVED] array trouble
    By littlefuzed in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 13th, 2010, 08:56 PM
  3. Having trouble with a While statment
    By java0 in forum Loops & Control Statements
    Replies: 1
    Last Post: January 27th, 2010, 12:37 PM
  4. Having trouble with an if statement. please help!!
    By humdinger in forum Loops & Control Statements
    Replies: 11
    Last Post: January 21st, 2010, 02:49 PM
  5. Having trouble with strings
    By Reaperkid77 in forum Java SE APIs
    Replies: 3
    Last Post: October 20th, 2009, 06:30 PM