java—为什么jtable不从其他类刷新

rdlzhqv9  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(398)

我有以下货到付款!当我从refreshbutton()调用fruitstable()时,它刷新jtable!但是当我从另一个类调用它时,它不会刷新表!下面是简化代码

public ResultSet sqlCommand(String sqlStatement) throws SQLException

    {
        this.sqlStatement = sqlStatement;
        ConnectingToMysql fruitsConnection = new ConnectingToMysql();
        Connection myConnection = fruitsConnection.ConnectingToMysql();
        Statement st = myConnection.createStatement();
        ResultSet result = st.executeQuery(sqlStatement);
        return result;          
    }       

    public Vector<String> getColumnData() throws SQLException
    {
        ResultSetMetaData metaData = sqlCommand(sqlStatement).getMetaData();
        int columnCount = metaData.getColumnCount();
        Vector<String> allColumn = new Vector<String>();                    
        for(int i = 1; i <= columnCount; i++)
        {
            allColumn.add(metaData.getColumnName(i));
        }           
        return allColumn;
    }       

    public Vector<Vector<String>> getData() throws SQLException
    {
        ResultSet result = sqlCommand(sqlStatement);
        ResultSetMetaData metaData = result.getMetaData();
        int columnCount = metaData.getColumnCount();            
        Vector<Vector<String>> allRow = new Vector<Vector<String>>(columnCount);            
        while(result.next())
        {
            Vector<String> eachRow = new Vector<String>();
            for(int i = 1; i <= columnCount; i++)
            {
                eachRow.add(result.getString(i));
            }
            allRow.add(eachRow);
        }               
        return allRow;              
    }

    public JScrollPane fruitsTable() throws SQLException
    {
        Vector<String> columnData = getColumnData();
        Vector<Vector<String>> rowData = getData();
        fruitsTableModel.setDataVector(rowData,columnData);         
        Font font = new Font("Courier", Font.PLAIN,16);             
        fruitsTable = new JTable();         
        fruitsTable.setModel(fruitsTableModel);                 
        fruitsTable.setPreferredScrollableViewportSize(new Dimension(850,310));
        fruitsTable.setRowHeight(30);
        fruitsTable.setFont(font);
        JScrollPane fruitsScrollPane = new JScrollPane(fruitsTable);            
        dataValue();            
        return fruitsScrollPane;            
    }

    private JButton refreshButton() throws SQLException
    {
        JButton refreshButton = new JButton("Refresh Record");
        refreshButton.setPreferredSize(new Dimension(100,40));          
        refreshButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                try 
                {
                    fruitsTable();                      
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }    
            }                   
        });
        return refreshButton;           
    }

这是另一个类中唯一调用fruitstable()的代码。

saveButton.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent event)
        {           
            try
            {                   
                String number = numberTextField.getText();
                int ID = Integer.parseInt(number);
                String fruit = fruitTextField.getText();
                String color = colorTextField.getText();
                String taste = tasteTextField.getText();                    
                System.out.println("The value is: " + number + " " + fruit + " " + color + " " + taste);

                String sqlText = "Insert into fruits(ID,Fruit,Color,Tast) values(?,?,?,?)";

                ConnectingToMysql connectingToMysql = new ConnectingToMysql();
                Connection makeConnection = connectingToMysql.ConnectingToMysql();
                PreparedStatement pdt = makeConnection.prepareStatement(sqlText);       
                pdt.setInt(1,ID);                       
                if(fruit.isEmpty())
                {
                    pdt.setNull(2,java.sql.Types.VARCHAR);
                }

                else
                {
                    pdt.setString(2,fruit);
                }                   
                if(color.isEmpty())
                {
                    pdt.setNull(3,java.sql.Types.VARCHAR);
                }
                else
                {
                    pdt.setString(3,color);
                }                   
                if(taste.isEmpty())
                {
                    pdt.setNull(4,java.sql.Types.VARCHAR);
                }
                else
                {
                    pdt.setString(4,taste);    
                }                   
                pdt.executeUpdate();                    
                GetFruits getFruits = new GetFruits();
                getFruits.fruitsTable();
            }               
            catch(SQLIntegrityConstraintViolationException e)
            {
                JOptionPane.showMessageDialog(null, "The 'Number' you entered already exist!", "Error", JOptionPane.ERROR_MESSAGE);
                System.out.println(e);
            }
jq6vz3qz

jq6vz3qz1#

GetFruits getFruits = new GetFruits();
getFruits.fruitsTable();

您正在创建一个新的getfruits对象并刷新它的表,而不是更新现有的表。尝试创建一个getfruits类变量,这样就可以使用同一个示例,而不是每次都创建一个新示例。只是猜测,因为我看不到包含动作侦听器的所有代码。

相关问题