将组件添加到JTable

时间:2013-04-23 08:06:35

标签: java swing jdbc jtable jbutton

我想在表格中添加JButton。我正在使用表来显示数据库记录。实际上我想为表中的每条记录添加按钮,但是按钮不会显示在表格上。它没有显示任何错误。请帮忙。提前谢谢。

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

1 个答案:

答案 0 :(得分:1)

  1. 帖子中的所有代码行都是重要的原因,为什么有ResultSetTableModel,TableFromDatabase(和/或从SwingWorker调用JDBC,Runnable #Thread)
  2. 永远不要调用tableModel.fireTableDataChanged();,
    • 在XxxTableModel定义之外
    • DefaultTableModel已正确实现此通知程序
    • 您需要覆盖此通知程序的代码,因为在Swing(Oracle tutorial)中讨论Concurency,再次关于我的第一点。
  3. 一切都很重要,请read this answer about ListModel and JList,所有要点,同一问题,
  4. JPanel在API中实现了FlowLayout,没有理由使用NullLayout,将其更改为BorderLayout
  5. 为JPanel覆盖getPreferredSize以获得合理的Dimension,包含JScrollPane中包含的JTable
  6. JButton added in JTable is here solved a few times