-2

我正在处理从数据库中获取数据的表单。现在我想JButton为每个获取的记录添加打开,即稍后的按钮将打开记录。但是我遇到了按钮未显示在表格中的问题。我想将按钮作为列添加到表中的每条记录中。提前致谢。

包添加面板;

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 );
            }
    }

}
4

2 回答 2

1

看起来您正试图将JButton实例放入TableModel. 相反,您需要一个TableCellEditor, 在此处讨论。Table Button Column提供了很好的解释,这个相关示例可能会有所帮助。

于 2013-04-24T02:12:18.627 回答
1

这对我有用。使用名称为“”的列并将其设置为 JButton。

制作您自己的表格模型,如下所示:

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);
            }
        }
 }

然后更改该列的渲染器

 Table.getColumn("").setCellRenderer((table, value, isSelected, hasFocus, row, column) -> {
    return (JButton)value;
    });
于 2017-07-19T06:45:48.553 回答