0

I write a code that add/delete data from database to JTable which has 3 columns when User press "Add" or "Delete" Button (JtoggleButton) and then press the buttons(JButton) that their labels are the number from 1 to 9.

Here the user interface of the code: enter image description here

When I try to add data to the table in the first time, it's ok and the number of columns is still 3 columns: enter image description here

But when I add data in the second or third time continuously , there are more 3 extra blank columns added into the table too which I don't need it.: enter image description here

Here the code that I think the problem is occurred:

final DefaultTableModel defaultmodel2 = new DefaultTableModel();
final JToggleButton tglbtnAdd = new JToggleButton("Add");
final JToggleButton tglbtnDelete = new JToggleButton("Delete");


    JButton button = new JButton("1");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection dbconbt1 = DriverManager.getConnection("" +"jdbc:sqlserver://localhost;databaseName=Store;user=<>;password=<>");
            Statement sqlstatement = dbconbt1.createStatement();
            ResultSet dbresultset1 = sqlstatement.executeQuery("select * from Store.dbo.Product where ProductID = 'P-1'");
            ResultSetMetaData rsmetadata = dbresultset1.getMetaData();       // Get metadata on them
            int numcols = rsmetadata.getColumnCount();    // How many columns?

            if(tglbtnAdd.isSelected() == true)
              { 
                for (int i = 1; i <= numcols; i++)
                {
                    defaultmodel2.addColumn( rsmetadata.getColumnName(i));
                }

                while (dbresultset1.next())
                {    Vector<Object> row = new Vector<Object>(numcols);
                  for (int i = 1; i <= numcols; i++)
                   {
                    row.addElement( dbresultset1.getObject(i) );
                   }
                    defaultmodel2.addRow(row );

                }

              }
            if(tglbtnDelete.isSelected() == true)
              {  
                defaultmodel2.removeRow(0);
              }
            //  Get row
            dbresultset1.close();
            sqlstatement.close();
            dbconbt1.close();
            } catch (SQLException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
            } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
             e.printStackTrace();
          }
        }
    }); 

1 Answer 1

2

Every time you call actionPerformed, you are also calling defaultmodel2.addColumn( rsmetadata.getColumnName(i));

This means, that each time an actionPerformed method is called, it is adding more columns to the column model.

You have a few choices...

  1. Check to see if the table model already contains columns and don't add new ones
  2. Check to see if the table model has the particular column and don't add it if it does
  3. Uses the columns from "Product List" table instead...
Sign up to request clarification or add additional context in comments.

1 Comment

That makes a nice change :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.