【问题标题】:Jtable glitch when updating content更新内容时出现 Jtable 故障
【发布时间】:2015-12-02 20:28:44
【问题描述】:

每次我执行表模型更新时,我的应用程序都会出现故障,正如在第一张图片中看到的那样,我用数字 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;
    }
}

【问题讨论】:

  • 在第一张图片中你忘记发图片了...
  • 是的,抱歉上传,stackoverflow 不允许我手动添加
  • 发布链接,我们可以编辑它们以使其显示
  • 已发表,感激不尽
  • 不要一直重新创建表格/滚动窗格。相反,为每个创建一个实例,然后简单地更新表的 TableModel

标签: java swing jtable jscrollpane visual-glitch


【解决方案1】:

所以问题出在 void showtable2 中,永远不要重新创建滚动面板的字段或表格模型,只需将它们删除一次并使用 table.setmodel 更新表格的模型

【讨论】:

    猜你喜欢
    • 2020-06-12
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 2012-12-23
    • 1970-01-01
    • 2021-05-30
    • 1970-01-01
    • 2016-07-19
    相关资源
    最近更新 更多