-2

I am working on form which fetches data from database. Now I want to add JButton open for each fetched record i.e. the button later will open the record. But I got problem that button is not shown in table. I want to add button as a column to each record in table. Thanks in advance.

package addPanel;

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


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","" };
                        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( 20, 20, 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 );
            }
    }

}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
vibhor
  • 59
  • 2
  • 7

2 Answers2

1

It looks like you're trying to put a JButton instance in the TableModel. Instead, you'll need a TableCellEditor, discussed here. Table Button Column offers a good explanation and this related example may be helpful.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

This works for me. Use the column with the name "" and set it to JButton.

Make your own table model like the following:

public class TableElementModel extends DefaultTableModel{

    String[ ] columnName = { "Id","First Name", "Last Name","" };
    @Override
        public int getColumnCount() {
            return columnName.size();
        }
        @Override
        public Class<?> getColumnClass(int columnIndex) {
            switch (columnIndex) {
                case 4:
                    return JButton.class;
                default:
                    return super.getColumnClass(columnIndex);
            }
        }
 }

Then change the renderer for that column

 Table.getColumn("").setCellRenderer((table, value, isSelected, hasFocus, row, column) -> {
    return (JButton)value;
    });
Valerio Santinelli
  • 1,592
  • 2
  • 27
  • 45