1

我在运行时在我的表模型中实现了“addTableModelListener”,但我想再创建一个控件来删除它。我已经搜索了谷歌,但我没有找到任何合适的逻辑来实现删除表模型侦听器。

请帮我实现该removeTableModelListener方法。

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;

public class CheckList extends JPanel {

    private static final long serialVersionUID = 1L;
    /**
    * Create the panel.
    */
    static JTable table;
    public int row;
    public int column;
    static MyTableModel model;
    static int t;

    public CheckList() {
        try {
            model = new MyTableModel();
            table = new JTable(model);
            table.setPreferredScrollableViewportSize(new Dimension(100, 70));
            table.setFillsViewportHeight(true);
            table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);

            // Create the scroll pane and add the table to it.
            JScrollPane scrollPane = new JScrollPane(table);
            scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
            scrollPane.setBounds(10, 132, 998, 522);
            setLayout(null);
            add(scrollPane);

            JButton btnNewButton = new JButton("New button");
            btnNewButton.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    if (model.getRowCount() != 0) {
                        model.getDataVector().removeAllElements();

                    }
                    model.addrows();
                }
            });
            btnNewButton.setBounds(868, 33, 89, 23);
            add(btnNewButton);

            final JComboBox comboBox = new JComboBox();
            comboBox.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {

                    switch (comboBox.getSelectedIndex()) {
                        case 0: {
                            model.removeTableModelListener(table.getModel());

                        }
                        case 1: {

                            model.addTableModelListener(new TableModelListener() {

                                public void tableChanged(TableModelEvent e) {

                                    try {// TODO Auto-generated method stub

                                        row = e.getFirstRow();
                                        column = e.getColumn();
                                        TableModel mod = (TableModel) e.getSource();
                                        String columnName = mod.getColumnName(column);
                                        Object data = mod.getValueAt(row, column);
                                        System.out.println(columnName + "  "
                                            + data + " row is " + row);
                                        System.out.println("value at"
                                            + model.getValueAt(row, 4));
                                        Boolean status = (Boolean) data;
                                        String check;
                                        // int value = (Integer) getValueAt(row, 4);
                                        // System.out.println(value);
                                        if (status == true) {
                                            check = "Recieved";

                                        } else {
                                            check = "not Recieved";
                                        }
                                        System.out.println(check);
                                        // int row1 = row + 1;
                                        model.st.executeUpdate("update temp1 set ["
                                            + columnName + "]='" + check
                                            + "' where [Packet ID]="
                                            + model.getValueAt(row, 4) + "");

                                    } catch (Exception t) {
                                        System.out.println(t);
                                    }

                                }
                            });

                            break;
                        }
                    }
                }
            });
            comboBox.setModel(new DefaultComboBoxModel(new String[]{"view",
                    "edit"}));
            comboBox.setBounds(158, 48, 81, 20);
            add(comboBox);

            JLabel lblNewLabel = new JLabel("Mode");
            lblNewLabel.setBounds(102, 51, 46, 14);
            add(lblNewLabel);

        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public class MyTableModel extends DefaultTableModel implements
        TableCellRenderer, TableModelListener {

        /**
        *
        */
        public void add() {
            addRow(new Object[]{"", "", "   ", "  ", "", " ", new Boolean(status)});
        }

        public void addrows() {

            try {

                st = login.con.createStatement();
                ResultSet re = st.executeQuery("select * FROM temp1 where operator="
                    + managment.log_id + "");

                while (re.next()) {
                    String n1 = re.getString("customer ID");
                    String n2 = re.getString("customer name");
                    String n3 = re.getString("packet id");
                    String n4 = re.getString("packet type");
                    String n5 = re.getString("reciever id");
                    String n6 = re.getString("reciever name");
                    String n7 = re.getString("reciever status");
                    if (n7.equals("Recieved")) {
                        status = true;

                    } else {
                        status = false;
                    }
                    addRow(new Object[]{"" + n1 + "", " " + n2 + "",
                            "   " + n5 + "", "  " + n6 + "", "  " + n3 + "",
                            "  " + n4 + "", new Boolean(status)});
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                System.out.println(e);
            }
        }
        private static final long serialVersionUID = 1L;
        /*
        *
        */
        public Statement st;
        boolean status;
        public int row;
        public int column;

        /**
        *
        */
        public MyTableModel() {
            // TODO Auto-generated constructor stub

            addColumn("sender ID");
            addColumn("sender Name");

            addColumn("Recieving ID");
            addColumn("Reciever Name");

            addColumn("Packet ID");
            addColumn("Packet Category");
            addColumn("reciever status");

        }

        @Override
        public int getColumnCount() {
            // TODO Auto-generated method stub
            return super.getColumnCount();
        }

        @Override
        public int getRowCount() {
            // TODO Auto-generated method stub
            return super.getRowCount();
        }

        @Override
        public String getColumnName(int column) {
            // TODO Auto-generated method stub
            return super.getColumnName(column);
        }

        @Override
        public Object getValueAt(int row, int column) {
            // TODO Auto-generated method stub
            return super.getValueAt(row, column);
        }

        @Override
        public boolean isCellEditable(int row, int column) {

            return super.isCellEditable(row, column);

        }

        public Class<?> getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        @Override
        public void setValueAt(Object aValue, int row, int column) {
            // TODO Auto-generated method stub
            super.setValueAt(aValue, row, column);
            // System.out.println("new value");
            // printDebugData();
        }

        @Override
        public Component getTableCellRendererComponent(JTable arg0,
            Object arg1, boolean arg2, boolean arg3, int arg4, int arg5) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void tableChanged(TableModelEvent arg0) {
            // TODO Auto-generated method stub
        }
    }
// @Override
// public void addTableModelListener(TableModelListener l) {
// // TODO Auto-generated method stub
//
// super.addTableModelListener(new TableModelListener() {
//
// @Override
// public void tableChanged(TableModelEvent e) {
//
// try {// TODO Auto-generated method stub
// row = e.getFirstRow();
// column = e.getColumn();
// TableModel mod = (TableModel) e.getSource();
// String columnName = mod.getColumnName(column);
// Object data = mod.getValueAt(row, column);
// System.out.println(columnName + "  " + data
// + " row is " + row);
// System.out.println("value at" + getValueAt(row, 4));
// Boolean status = (Boolean) data;
// String check;
// // int value = (Integer) getValueAt(row, 4);
// // System.out.println(value);
// if (status == true) {
// check = "Recieved";
//
// } else {
// check = "not Recieved";
// }
// System.out.println(check);
// // int row1 = row + 1;
// st.executeUpdate("update temp1 set [" + columnName
// + "]='" + check + "' where [Packet ID]="
// + getValueAt(row, 4) + "");
//
// } catch (Exception t) {
// System.out.println(t);
// }
//
// }
// });
// }

    /*
    * private void printDebugData() { int numRows = getRowCount(); int numCols
    * = getColumnCount();
    *
    * for (int i = 0; i < numRows; i++) { System.out.print(" row " + i + ":");
    * for (int j = 0; j < numCols; j++) { System.out.print(" " + getValueAt(i,
    * j)); } System.out.println(" ");
    *
    * }
    *
    * System.out.println("--------------------------"); }
    */
}
4

1 回答 1

1

将 存储TableModelListener在最终的局部变量中。您将可以在 actionPerformed 中访问它:

final TableModelListener modelListener = new TableModelListener() {
    ...
};
final JComboBox comboBox = new JComboBox();
comboBox.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        switch (comboBox.getSelectedIndex()) {
            case 0: {
                model.removeTableModelListener(modelListener);
                break;
            }
            case 1: {
                model.addTableModelListener(modelListener);
            }
        }
    });

作为一般性观察,我会建议创建TableModelListener一个类成员变量,但您的许多变量都是static. 我建议避免制作这些变量static

于 2012-06-12T03:34:11.743 回答