0

I want to add JButton in table. I am using table to display database records. Actually I want to add button for each record in table but, the button is not displayed on table. It doesnt show any errors. Please help. Thanks in advance.

package addPanel;

import java.sql.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class panelShowData extends JPanel
{



    Connection connection = null;
    Statement statement = null;
    ResultSet resultSet = null;

    String url = "jdbc:mysql://localhost:3306/records";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root";
    String password = "";

    JScrollPane scrollPane;
    JTable table;
    DefaultTableModel tableModel;

    String nameSearch="";


    public panelShowData()
    {

         this.setLayout(null);
         setVisible(true);
         setBounds(0, 200, 500, 450);
    }

    public void searchData( String nameSearch)
    {

         tableModel = new DefaultTableModel();

            try 
            {           
                Class.forName( driver ).newInstance(  );
                connection = DriverManager.getConnection( url, userName, password );

                statement = connection.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE,
                                    ResultSet.CONCUR_UPDATABLE );

                resultSet = statement.executeQuery( "select * from registration where firstname='"
                                    + nameSearch
                                + "'or lastname ='"
                                + nameSearch + "'" );

                System.out.println( "Query executed" );
                System.out.println( "nameSearch="+nameSearch );

                String firstName;
                String lastName;
                int id;

                JButton add=new JButton("ADD");

                    while ( resultSet.next(  ) )
                    {       
                        System.out.print( resultSet.getString( 2 ) + "\t" );
                        System.out.print( resultSet.getString( 4 ) + "\n" );

                        firstName = resultSet.getString( 2 );
                        lastName = resultSet.getString( 4 );
                        id = resultSet.getInt(1);

                        String[ ] columnName = { "Id","First Name", "Last Name","click" };
                        Object[ ] data = { id, ""+firstName, "" + lastName, add };


                        System.out.println("Names is:"+firstName);
                        tableModel.setColumnIdentifiers( columnName );
                        tableModel.addRow( data );
                        tableModel.fireTableDataChanged();
                    }

                table = new JTable( tableModel );
                table.setEnabled(false);
                scrollPane = new JScrollPane( table );
                scrollPane.setBounds( 10, 10, 350, 100 );
                scrollPane.revalidate();
                scrollPane.repaint();

                add( scrollPane );
                connection.close(  );   
            }
            catch (Exception e)
            {
                e.printStackTrace();
                JOptionPane.showMessageDialog(  null, "Record Not Found",
                                "Sorry", JOptionPane.ERROR_MESSAGE );
            }
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
vibhor
  • 59
  • 2
  • 7

1 Answers1

1
  1. all code lines in your post are important reasons, why ResultSetTableModel, TableFromDatabase (and/or to invoked JDBC from SwingWorker, Runnable#Thread) are there
  2. never to call tableModel.fireTableDataChanged();,
    • outside XxxTableModel defintion
    • DefaultTableModel has implemented this notifier and correctly
    • your code required to override this notifiers because talking about Concurency in Swing (Oracle tutorial), again about my point 1st.
  3. everything important is there, please to read this answer about ListModel and JList, all points, the same issue,
  4. JPanel has FlowLayout implemented in API, no reason to use NullLayout, change that to BorderLayout
  5. override getPreferredSize for reasonable Dimension for JPanel, contains JTable wrapped in JScrollPane
  6. JButton added in JTable is here solved a few times
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319