始终在JTable

时间:2018-03-29 22:57:50

标签: java swing jtable

我在选择JTable的第一行时遇到问题。我尝试过以下方法:

1

customersTable.requestFocus();
customersTable.changeSelection(0, 0, false, false);

2

customersTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
customersTable.getSelectionModel().setSelectionInterval(0, 0);
customersTable.setColumnSelectionInterval(0, 0);
customersTable.requestFocusInWindow();

这两种方法都有效,但只有当我向表中添加一个新客户并调用populateCustomerTable()时才会这样,这就是我放置这段代码的地方。在创建JTable的方法中调用populateCustomerTable(),所以我不确定它为什么不起作用。我已经尝试将两段代码直接放在创建JTable的方法中,但两者都不起作用。

我希望一旦程序启动就会选择表的第一行,但在我将一个客户输入表后,它实际上只是按照我的意愿行事。

以下是设置我的JTable的代码:

private JPanel customersTable() {
        JPanel panelCustomersTable = new JPanel(new BorderLayout());

        customersTableModel = new DefaultTableModel(new String[] {
                "First Name", "Last Name", "House Number", "Address",
                "City", "Postcode", "Phone Number"
        }, 0);

        customersTable = new JTable(customersTableModel ) {


            public boolean isCellEditable(int row, int col) {
                return false;
            }

            public Component prepareRenderer(TableCellRenderer r, int row, int col) {
                Component c = super.prepareRenderer(r, row, col);

                if (row % 2 == 0) {
                    c.setBackground(Color.WHITE);
                } else {
                    c.setBackground(new Color(234, 234, 234));
                }

                if (isRowSelected(row)) {
                    c.setBackground(new Color(24, 134, 254));
                }

                return c;
            }
        };

        customersTable.setFont(new Font("", 0, 14));
        customersTable.setRowHeight(customersTable.getRowHeight() + 10);
        customersTable.setAutoCreateRowSorter(true);
        customersTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);


        customersTable.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent event) {
                if (event.getClickCount() == 2) {
                    //editCustomer(frame?)
                }

                try {
                    int row = customersTable.getSelectedRow();
                    String houseNumber = customersTable.getModel().getValueAt(row, 2).toString();
                    String address = customersTable.getModel().getValueAt(row, 3).toString();
                    String city = customersTable.getModel().getValueAt(row, 4).toString();
                    //populateMap(houseNumber, address, city);
                } catch (Exception e) {
                    /*
                        JOptionPane.showMessageDialog(frame, "Please select an item to delete.",
                                "Error", JOptionPane.ERROR_MESSAGE);
                     */
                }
            }
        });



        populateCustomersTable();



        customersTable.setFillsViewportHeight(true);


        JScrollPane jsp = new JScrollPane(customersTable,
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);


        panelCustomersTable.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        panelCustomersTable.add(jsp, BorderLayout.CENTER);
        panelCustomersTable.add(customersTableButtons(), BorderLayout.SOUTH);
        panelCustomersTable.add(customersTableSearch(), BorderLayout.NORTH);

        return panelCustomersTable;
    }

这是我的populateCustomersTable方法:

private static void populateCustomersTable() {
        int rows = customersTableModel.getRowCount();
        for (int i = rows - 1; i >= 0; i --) {
            customersTableModel.removeRow(i);
        }

        Database.selectCustomers();

        for (int i = 0; i < Database.getCustomersArray().size(); i++) {

            String firstName = Database.getCustomersArray().get(i).getFirstName();
            String lastName = Database.getCustomersArray().get(i).getLastName(); 
            String houseNumber = Database.getCustomersArray().get(i).getHouseNumber();
            String address = Database.getCustomersArray().get(i).getAddress();
            String city = Database.getCustomersArray().get(i).getCity();
            String postcode = Database.getCustomersArray().get(i).getPostcode();
            String phoneNumber = Database.getCustomersArray().get(i).getPhoneNumber();


            Object[] data = {firstName, lastName, houseNumber, address, city,
                    postcode, phoneNumber
            };


            customersTableModel.addRow(data);
            customersTable.requestFocus();
            customersTable.changeSelection(0, 0, false, false);
        }
    }

0 个答案:

没有答案