Java 更新内容时出现Jtable故障

Java 更新内容时出现Jtable故障,java,swing,jtable,jscrollpane,visual-glitch,Java,Swing,Jtable,Jscrollpane,Visual Glitch,每次我执行表模型的更新时,我的应用程序都会出现问题,正如人们在第一张图片中看到的那样,我用数字1进行查询,它返回表中结果集的数据,在图片2中,我执行了一个新的查询,现在用数字2,它似乎可以工作,但是当我水平滚动表时,jtable似乎在第一个查询中崩溃并重新绘制 public GUIDetalleActividades(String cedula) { this.cedula = cedula; setTitle("Detalle Actividades");

每次我执行表模型的更新时,我的应用程序都会出现问题,正如人们在第一张图片中看到的那样,我用数字1进行查询,它返回表中结果集的数据,在图片2中,我执行了一个新的查询,现在用数字2,它似乎可以工作,但是当我水平滚动表时,jtable似乎在第一个查询中崩溃并重新绘制

public GUIDetalleActividades(String cedula) {
        this.cedula = cedula;
        setTitle("Detalle Actividades");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 597, 500);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblAContinuacinSe = new JLabel(
                "A continuaci\u00F3n se muestra un listado con todos los proyectos a su cargo,  en el siguiente campo podr\u00E1 consultar");
        lblAContinuacinSe.setBounds(10, 11, 570, 14);
        contentPane.add(lblAContinuacinSe);

        JLabel lblParaVerLos = new JLabel("las actividades de un determinado proyecto");
        lblParaVerLos.setBounds(10, 29, 407, 14);
        contentPane.add(lblParaVerLos);

        //creating upper table with resultset

        table = new JTable(cons.tablaConsultaProyectosACargo(cedula));

        JScrollPane scrollPane = new JScrollPane(table, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setBounds(21, 86, 539, 134);

        contentPane.add(scrollPane);
        table.getColumnModel().getColumn(1).setPreferredWidth(100);
        table.getColumnModel().getColumn(2).setPreferredWidth(100);
        table.getColumnModel().getColumn(3).setPreferredWidth(100);
        table.getColumnModel().getColumn(4).setPreferredWidth(100);

        scrollPane.setViewportView(table);

        JLabel lblConsultarActividadNoproyecto = new JLabel("Consultar actividad No.proyecto:");
        lblConsultarActividadNoproyecto.setBounds(21, 244, 174, 14);
        contentPane.add(lblConsultarActividadNoproyecto);

        txtnum = new JTextField();
        txtnum.setBounds(192, 241, 86, 20);
        contentPane.add(txtnum);
        txtnum.setColumns(10);

        JButton btnConsultar = new JButton("Consultar");
        btnConsultar.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                //pressing the button should repaint the table with new data and no glitches
                showtable2(txtnum.getText());
            }
        });
        btnConsultar.setBounds(301, 240, 89, 23);
        contentPane.add(btnConsultar);

    }

    //creating table with default table model sent from the class that does all the queries
    public void showtable2(String num) {

        table_1 = new JTable(cons.tablaConsultaActividades(num));
        table_1.setEnabled(false);
        JScrollPane scrollPane_1 = new JScrollPane(table_1, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPane_1.setBounds(20, 310, 552, 134);
        table_1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        contentPane.add(scrollPane_1);
        table_1.getColumnModel().getColumn(1).setPreferredWidth(100);
        table_1.getColumnModel().getColumn(2).setPreferredWidth(100);
        table_1.getColumnModel().getColumn(3).setPreferredWidth(100);
        table_1.getColumnModel().getColumn(4).setPreferredWidth(100);

        scrollPane_1.setViewportView(table_1);

    }

    public String getCedula() {
        return cedula;
    }

    public void setCedula(String cedula) {
        this.cedula = cedula;
    }
}


因此问题出现在无效的showtable2中,永远不要重新创建scrollpanel的字段或表格模型,只需删除它们一次,然后用table.setmodel更新表格模型即可。因此问题出现在无效的showtable2中,永远不要重新创建scrollpanel的字段或表格模型,只需删除它们一次,然后用table.setmodel更新表的模型。在第一张图片中,您忘了发布图片…是的,很抱歉上载它,stackoverflow不允许我手动添加它。发布链接,我们可以编辑它们以使其显示发布,请注意不要继续重新创建表/滚动窗格。相反,只需在第一张图片中更新表的
TableModel
,您忘记发布图片…是的,很抱歉上传它,stackoverflow不允许我手动添加它发布链接,我们可以编辑它们以使其显示发布,非常感谢不要继续重新创建表/滚动窗格。取而代之的是,为每个表创建一个实例,只需更新表的
TableModel