刷新JFrame内容DefaultTableModel

时间:2018-04-21 07:06:32

标签: java jframe refresh defaulttablemodel

我正试图弄清楚如何使用其他网站上的示例更改模型后更新表窗口。一切正常但我无法想象一旦表模型发生变化后如何刷新窗口。

编辑:我使用了Bell的建议并重新安排了一些内容,以便构造函数调用我的getmodel3方法来填充表。我以为我可以使用setmodel方法来更改模型并更新表,但它没有像我想的那样工作。这就是我的想法,但不会发生:

  1. main方法使用从getmodel3方法传递的模型创建表的新实例。
  2. 构建表格后,我调用setmodel方法,使用getmodel4中返回的模型将不同的数据加载到新模型中。
  3. 使用新模型数据更新步骤1中的实例,并在表格中显示新数据。
  4. 实际发生的是,创建一个新实例并使用从getmodel3返回的模型,然后setmodel方法运行并从一组不同的数据更新模型变量,但表格没有没有表现出变化。

        public class myTable extends JFrame
        {
    
        public volatile DefaultTableModel model = (DefaultTableModel) myTable.getmodel3();
    
        public void setmodel(DefaultTableModel newModel) 
        {
            this.model = newModel;
        }
    
        public myTable()
        {
            JTable table = new JTable( model );
            JScrollPane scrollPane = new JScrollPane( table );
            getContentPane().add( scrollPane );
            JPanel buttonPanel = new JPanel();
            getContentPane().add( buttonPanel, BorderLayout.SOUTH );
        }
    
        public static void main(String[] args)
        {
            myTable frame = new myTable();
            frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
            frame.pack();
            frame.setVisible(true);
            frame.setmodel(getmodel4());
        }   
    

    这里的getmodel3getmodel4相同,只是它们指向不同数据的不同数据库文件。

        public static DefaultTableModel getmodel3(){
        Vector<Object> columnNames = new Vector<Object>();
        Vector<Object> data = new Vector<Object>();
    
        try
        {
            //  Connect to an Access Database
    
            String url = "jdbc:sqlite:c:\\sqlite3\\test.db";
            Connection conn = DriverManager.getConnection(url);
    
            //  Read data from a table
    
            String sql = "Select * from Tasks";
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery( sql );
            ResultSetMetaData md = rs.getMetaData();
            int columns = md.getColumnCount();
    
            //  Get column names
    
            for (int i = 1; i <= columns; i++)
            {
                columnNames.addElement( md.getColumnName(i) );
            }
    
            //  Get row data
    
            while (rs.next())
            {
                Vector<Object> row = new Vector<Object>(columns);
    
                for (int i = 1; i <= columns; i++)
                {
                    row.addElement( rs.getObject(i) );
                }
    
                data.addElement( row );
            }
    
            rs.close();
            stmt.close();
            conn.close();
        }
        catch(Exception e)
        {
            System.out.println( e );
        }
    
        //  Create table with database data
    
        DefaultTableModel model = new DefaultTableModel(data, columnNames)
        {
            @Override
            public Class getColumnClass(int column)
            {
                for (int row = 0; row < getRowCount(); row++)
                {
                    Object o = getValueAt(row, column);
    
                    if (o != null)
                    {
                        return o.getClass();
                    }
                }
    
                return Object.class;
            }
        };
    System.out.println("got model");
    
    return model;
    

    }

2 个答案:

答案 0 :(得分:0)

您可以使用table.getModel()使用现有模型,并使用table.setModel(yourModel)设置新/现有模型

DefaultTableModel model = (DefaultTableModel) table.getModel();
// do modification here
table.setModel(model);

答案 1 :(得分:0)

好吧,在你的一些帮助下,我终于明白了。 setModel因为1而无法正常工作,我尝试使用它的方法范围无法访问,2我尝试创建自己的setter setmodel但我不知道是什么我在做 :)。

最后,我在一个更容易到达的地方初始化,所以我没有在其他方法中弄乱对象。这是在调用main时加载表的工作结果,之后我可以调用update方法重新加载它。谢谢你们的帮助。

public class able extends JFrame
{

//initialize here for easy access.  getmodel3() is the model getter method and 
//returns the defaultTableModel object

private static able frame =  new able();
private static JTable table = new JTable( getmodel3() );

//Start all main stuff here using the previously initialized objects.

public static void main(String[] args)
{
    JScrollPane scrollPane = new JScrollPane( table );
    frame.getContentPane().add( scrollPane );
    JPanel buttonPanel = new JPanel();
    frame.getContentPane().add( buttonPanel, BorderLayout.SOUTH );

    frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
    frame.pack();
    frame.setVisible(true);

}   

public static void update()
{
   table.setModel(getmodel3());
}

//This method makes the table model from sql result set and returns it.    
//(found online somewhere thanks to whoever wrote it)

public static DefaultTableModel getmodel3(){
    Vector<Object> columnNames = new Vector<Object>();
    Vector<Object> data = new Vector<Object>();

    try
    {
        //  Connect to an Access Database

        String url = "jdbc:sqlite:c:\\sqlite3\\test.db";
        Connection conn = DriverManager.getConnection(url);

        //  Read data from a table

        String sql = "Select * from Tasks";
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery( sql );
        ResultSetMetaData md = rs.getMetaData();
        int columns = md.getColumnCount();

        //  Get column names

        for (int i = 1; i <= columns; i++)
        {
            columnNames.addElement( md.getColumnName(i) );
        }

        //  Get row data

        while (rs.next())
        {
            Vector<Object> row = new Vector<Object>(columns);

            for (int i = 1; i <= columns; i++)
            {
                row.addElement( rs.getObject(i) );
            }

            data.addElement( row );
        }

        rs.close();
        stmt.close();
        conn.close();
    }
    catch(Exception e)
    {
        System.out.println( e );
    }

    //  Create table with database data

    DefaultTableModel model3 = new DefaultTableModel(data, columnNames)
    {
        @Override
        public Class getColumnClass(int column)
        {
            for (int row = 0; row < getRowCount(); row++)
            {
                Object o = getValueAt(row, column);

                if (o != null)
                {
                    return o.getClass();
                }
            }

            return Object.class;
        }
    };
System.out.println("got model");

return model3;
 }

public static DefaultTableModel getmodel4(){
    Vector<Object> columnNames = new Vector<Object>();
    Vector<Object> data = new Vector<Object>();

    try
    {
        //  Connect to an Access Database

        String url = "jdbc:sqlite:c:\\sqlite3\\test2.db";
        Connection conn = DriverManager.getConnection(url);

        //  Read data from a table

        String sql = "Select * from Tasks";
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery( sql );
        ResultSetMetaData md = rs.getMetaData();
        int columns = md.getColumnCount();

        //  Get column names

        for (int i = 1; i <= columns; i++)
        {
            columnNames.addElement( md.getColumnName(i) );
        }

        //  Get row data

        while (rs.next())
        {
            Vector<Object> row = new Vector<Object>(columns);

            for (int i = 1; i <= columns; i++)
            {
                row.addElement( rs.getObject(i) );
            }

            data.addElement( row );
        }

        rs.close();
        stmt.close();
        conn.close();
    }
    catch(Exception e)
    {
        System.out.println( e );
    }

    //  Create table with database data

    DefaultTableModel model3 = new DefaultTableModel(data, columnNames)
    {
        @Override
        public Class getColumnClass(int column)
        {
            for (int row = 0; row < getRowCount(); row++)
            {
                Object o = getValueAt(row, column);

                if (o != null)
                {
                    return o.getClass();
                }
            }

            return Object.class;
        }
    };
System.out.println("got model");

return model3;
 }


}