简体   繁体   English

如何在JTable中使用侦听器?

[英]How to use listener in JTable?

My task is to show a dialog using JOptionPane that shows the information of the selected product on the JTable. 我的任务是显示一个使用JOptionPane的对话框,该对话框在JTable上显示所选产品的信息。

I used this listener but I'm not sure if it is the right one to use? 我使用了此侦听器,但不确定是否适合使用它?

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

I'm done with everything except showing the dialog part. 除了显示对话框部分,我已完成所有工作。 Here are my codes so far. 到目前为止,这是我的代码。 Am I on the right track? 我在正确的轨道上吗?

Here are my codes. 这是我的代码。

    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;
            }
        }
    }
}

You have to add the selection listener: 您必须添加选择侦听器:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM