swingworker jtable重画不工作

时间:2013-12-04 13:26:00

标签: mysql swing jtable swingworker tablemodel

我正在制作购买三明治和百吉饼的程序,所以当用户购买三明治时,它会被添加到数据库表中,我的购物车类会选择该数据库表中的所有元素并将其显示在jtable中,所以我想要每次用户点击购买按钮(在另一个类中)时更新的购物车jtable我试图使用SwingWorker更新我的表模型

问题: 当我点击一个购买按钮时,项目被插入到db表中,并且它从db table中选择到cart类中的模型,但是表没有重新绘制或重新验证,我试图在与cart类相同的类中运行重绘表重新绘制,我是SwingWorker的新手,所以我猜问题就在那里。 我的购物车类代码:

public JPanel panelInkobskurv() {

    con = new DBConnection();
    con.createConnection();

    String sql = "Select * from temp_order";
    try {
        PreparedStatement prepstmt = con.connection.prepareStatement(sql);
        rs = prepstmt.executeQuery(sql);
        model = ListTableModel.createModelFromResultSet(rs);                
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

设置表格:

table.setModel(model);
table.setRowHeight(table.getRowHeight() + 15);
table.setFont(new Font("Serif", Font.PLAIN, 12));
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.getColumnModel().getColumn(0).setPreferredWidth(30);
table.getColumnModel().getColumn(1).setPreferredWidth(180);
table.getColumnModel().getColumn(2).setPreferredWidth(40);
table.setShowGrid(false);
table.setTableHeader(null);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
DefaultTableCellRenderer leftRenderer = new DefaultTableCellRenderer();
DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
leftRenderer.setHorizontalAlignment(JLabel.LEFT);
rightRenderer.setHorizontalAlignment(JLabel.RIGHT);
table.getColumnModel().getColumn(0).setCellRenderer( leftRenderer );
table.getColumnModel().getColumn(1).setCellRenderer( leftRenderer );
table.getColumnModel().getColumn(2).setCellRenderer( rightRenderer );

scrollPane = new JScrollPane(table);

public void execTask(){
    SwingWork worker = new SwingWork(table);
    worker.execute();
}

我的SwingWorker课程:

class SwingWork extends SwingWorker<TableModel, TableModel> {

    private final JTable table;

    public SwingWork(JTable table){
        this.table = table;
    }
    @Override
    protected TableModel doInBackground() throws Exception {
        DBConnection connec = new DBConnection();
        connec.createConnection();
        String sql2 = "Select * from temp_order";
        PreparedStatement prepstmt2 = connec.connection.prepareStatement(sql2);
        ResultSet rs2 = prepstmt2.executeQuery();
        ListTableModel md = ListTableModel.createModelFromResultSet(rs2);
        return md;
    }

    public void done(){
        try {
            TableModel model = get();
            table.setModel(model);
            table.revalidate();
            table.repaint();    
        } catch (InterruptedException | ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

0 个答案:

没有答案