0

我创建了一个窗口,其中包含一个填充了 mysql 数据的 jtable。当点击 Jdialog 时,还有一个按钮“加载表”,让用户从 JComboBox 中选择另一个表!一切正常,但问题是当从 JComboBox 中选择一个表并单击 Jdialog OK 按钮时,JTable 没有从新表中加载数据(不刷新)!下面是简化代码!!

请任何帮助!!!!

类 mysqlData 扩展 JFrame {

Font buttonFont = new Font("Arial",Font.PLAIN, 16);
Font labelfont = new Font("Arial",Font.PLAIN, 24);
Connection connecting;
public String tableName = "Examination";

static final String Select = "Available Mysql Tables";



//connection statements
mysqlData()
    {
        Connections con = new Connections();
        connecting = con.connect;
        System.out.print("value of input is: " + tableName);


    }



    //creating jtable and populating it with data from mysql database
    public JScrollPane dataTable() throws SQLException
    {

        Statement st = connecting.createStatement();
        ResultSet result = st.executeQuery("Select * from " + tableName);
        ResultSetMetaData md = result.getMetaData();
        int columnCount = md.getColumnCount();

        Vector<String> data = new Vector<String>();
        Vector<String> column = new Vector<String>();

        //JOptionPane.showMessageDialog(null,"Wrong username or password" + tableName,"Failed!",JOptionPane.ERROR_MESSAGE);

        DefaultTableModel tablemodel = new DefaultTableModel(data,column);
        tablemodel.setRowCount(0);
        tablemodel.setColumnCount(0);

        for(int i=1; i<=columnCount; i++)
        {
            tablemodel.addColumn(md.getColumnName(i));
        }

        while(result.next())
        {
            Vector<String> row = new Vector<String>(columnCount);

            for(int i=1; i<=columnCount; i++)
            {
                row.add(result.getString(i));
            }

            tablemodel.addRow(row);
        }



        JTable table = new JTable(tablemodel);
        table.setPreferredScrollableViewportSize(new Dimension(900,780));
        JScrollPane scrolPane = new JScrollPane(table);
        return scrolPane;
     }




    //a button that pops up a jdialog and lets users to select a table from jcombo box
    public JButton tablesButton()
    {
        JButton tablesListButton = new JButton("LOAD TABLES");
        tablesListButton.setFont(buttonFont);
        tablesListButton.addActionListener(listener);

        return tablesListButton;
    }



    //actionlistener for the  tablesButton

    ActionListener listener = new ActionListener()
    {
        public void actionPerformed(ActionEvent event)
        {
            String[] tables  = new String[]{"Attendance","Examination","Students","Subjects","Teachers","Salary"};
            tableName = (String) JOptionPane.showInputDialog(mysqlData.this,"Please select your favorite sport",Select, JOptionPane.INFORMATION_MESSAGE, null,tables,"Attendance");


        }
    };

}

4

2 回答 2

0

我认为你必须做很多重构,我会给你一些提示来做到这一点。

1)主要问题是在你的ActionListener当一个表

tableName = (String) JOptionPane.showInputDialog(mysqlData.this,"Please select your favorite sport",Select, JOptionPane.INFORMATION_MESSAGE, null,tables,"Attendance");

你正在用它做任何事情,你必须刷新你的tableModel. 你必须再打电话

Statement st = connecting.createStatement();
        ResultSet result = st.executeQuery("Select * from " + tableName);
        ResultSetMetaData md = result.getMetaData();

你必须访问你当前的JTable东西,比如

DefaultTableModel tableModel = (DefaultTableModel)jtable.getModel();
for(int i=1; i<=columnCount; i++)
        {
            tablemodel.addColumn(md.getColumnName(i));
        }

        while(result.next())
        {
            Vector<String> row = new Vector<String>(columnCount);

            for(int i=1; i<=columnCount; i++)
            {
                row.add(result.getString(i));
            }

            tablemodel.addRow(row);
        }

您不想每次都创建一个新的 jtable 实例,而只想更改模型。

于 2013-09-01T19:30:50.723 回答
0

由于编译错误,我对您的示例进行了一些重构。下面的代码应该可以工作:

    ActionListener listener = new ActionListener() {
    public void actionPerformed(ActionEvent event) {
        String[] tables = new String[] { "Attendance", "Examination", "Students", "Subjects", "Teachers", "Salary" };

        tableName = (String) JOptionPane.showInputDialog(null,
                        "Please select your favorite sport", "Attendance",
                        JOptionPane.INFORMATION_MESSAGE, null,
                        tables, tables[0]);
        JOptionPane.showMessageDialog(null, "You have selected: " + tableName);

        System.out.println("value of input is: " + tableName);
    }
};

为了将来参考,这里是一组 JOption/InputPane 示例:
http ://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html

高温高压

于 2013-09-01T18:52:55.260 回答