1

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

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

4

1 回答 1

0

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

于 2015-12-02T21:21:36.310 回答