如何在JTable中使用监听器?

时间:2014-11-02 10:01:28

标签: java swing jtable

我的任务是使用JOptionPane显示一个对话框,该对话框显示JTable上所选产品的信息。

我使用过这个听众,但我不确定它是否适合使用?

table.getModel().addTableModelListener(new TableModelListener()
            {
                public void tableChanged(TableModelEvent e) 
                {
                   //code for JOptionPane?
                }
              });

除了显示对话框部分外,我完成了所有操作。到目前为止,这是我的代码。我是在正确的轨道上吗?

这是我的代码。

    public class Table {

    public static void main(String[] args) 
    {
        Runnable r = new Runnable() {

            public void run() {
                new Table().createUI();
            }
        };

        EventQueue.invokeLater(r);
    }

    private void createUI() {

        try {
            JFrame frame = new JFrame();
            frame.setLayout(new BorderLayout());
            JTable table = new JTable();

            String readLine = null;

            TableModel tableModel = new TableModel();
            File file = new File("Products.csv");

            FileReader reader = new FileReader(file);
            BufferedReader bufReader = new BufferedReader(reader);

            List<Products> studentList = new ArrayList<Products>();
            while((readLine = bufReader.readLine()) != null) {
                String[] splitData = readLine.split(",");

                Products products = new Products();
                products.setName(splitData[0]);
                products.setNumber(splitData[1]);
                products.setPrice(splitData[2]);

                studentList.add(products);
            }

            tableModel.setList(studentList);
            table.setModel(tableModel);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new JScrollPane(table));
            frame.setTitle("Lab 5");
            frame.pack();
            frame.setVisible(true);

            table.getModel().addTableModelListener(new TableModelListener()
            {
                public void tableChanged(TableModelEvent e) 
                {
                   //code for JOptionPane?
                }
              });

        } catch(IOException ex) {}
    }

    class Products {

        private String name;
        private String number;
        private String price;

        public String getPrice() {
            return price;
        }
        public void setPrice(String price) {
            this.price = price;
        }

        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getNumber() {
            return number;
        }
        public void setNumber(String number) {
            this.number = number;
        }
    }

    class TableModel extends AbstractTableModel
    {
        private List<Products> list = new ArrayList<Products>();
        private String[] columnNames = {"Barcode", "Name","Price"};

        public void setList(List<Products> list) {
            this.list = list;
            fireTableDataChanged();
        }

        @Override
        public String getColumnName(int column) {
            return columnNames[column];
        }

        public int getRowCount() {
            return list.size();
        }

        public int getColumnCount() {
            return columnNames.length;
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            switch (columnIndex) {
            case 0:
                return list.get(rowIndex).getName();
            case 1:
                return list.get(rowIndex).getNumber();
            case 2:
                return list.get(rowIndex).getPrice();
            default:
                return null;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您必须添加选择侦听器:

table.getSelectionModel().addListSelectionListener(
                    new ListSelectionListener() {
                        public void valueChanged(ListSelectionEvent event) {
                            int selectedRow = table.getSelectedRow();
                            JOptionPane.showMessageDialog(
                                    frame,
                                    "Selected product: "
                                            + table.getModel().getValueAt(
                                                    selectedRow, 0));
                        }
                    });