首页 > 解决方案 > 在 JTable (Java with swing) 上使用事件监听器时,变量有奇怪的行为

问题描述

非常感谢一些帮助。

短版基本上我可以调用一个带有硬编码字符串作为参数的方法,但我不能使用一个特定的全局字符串变量(即使我可以打印它并检查它是否是一个字符串,并且它被识别)

长版: 所以我试图让一个函数在一个基本的银行应用程序中工作,这让我非常抓狂。

我有关于银行、客户和账户逻辑的课程。它按预期工作。问题在于 Gui 类。

我正在使用 JTable 来显示客户列表。每个客户都有一个我存储在全局变量中的个人号码。我将此变量用作我正在调用的方法的参数。例如 bank.findCustomer(personalNumber)。

每次单击表格中的一行时,我都可以打印当前选择的个人号码。这很好用。我检查过它确实是一个字符串。但是一旦我创建了一个客户对象来更改名称(使用客户类中的方法),我就不能使用所选个人号码的全局变量。

我可以硬编码字符串但我不能传递变量字符串,这似乎很奇怪。

我的猜测是字符串以某种方式“锁定”在同步(?)事件中,或者偶数侦听器发生冲突。我已经尝试了 1000 种不同的方法来让它发挥作用。感觉头撞墙了。。。

尝试在 actionPerformed 方法中调用 customer.setName() 时出现空指针异常。如果我在此方法之外使用它,它有效吗?

这是代码:

class Gui implements ActionListener {
private BankLogic bank;
private JFrame frame;
protected JTable table;
protected JMenuBar menuBar;
protected JMenu mainMenu, customerMenu, accountMenu;
protected JPopupMenu popupMenu;
protected JMenuItem addCustomerItem, removeCustomerItem, editCustomerItem, showCustomersItem,
        createSavingsAccountItem, createCreditAccountItem, showAccountsItem,
        removeAccountItem, depositToAccountItem;
protected JButton refreshButton;
protected DefaultTableModel defModel;
String pNumOfCustomerSelected;
protected int accNumOfAccountSelected;
private JScrollPane scroll;
private ArrayList<String> customerDataList;
protected String test = "14";

Gui() {
    bank = new BankLogic();
    // dummy customers
    bank.createCustomer("Donald", "Duck", "12");
    bank.createCustomer("Dauda", "Dabo", "14");
    bank.createSavingsAccount("12");
    bank.createCreditAccount("12");
    // for testing
    pNumOfCustomerSelected = "12";



    // === MAIN MENU === //
    // Item for menu
    addCustomerItem = new JMenuItem("Add customer");
    addCustomerItem.addActionListener(this);
    // Menu to hold items
    mainMenu = new JMenu("Menu");
    // Menu bar to hold menu
    menuBar = new JMenuBar();
    // Install components
    mainMenu.add(addCustomerItem);
    menuBar.add(mainMenu);



    // === POPUP MENU === //
    // Items for popup menu
    editCustomerItem = new JMenuItem("Edit customer name(s)");
    editCustomerItem.addActionListener(this);
    // Popup menu
    popupMenu = new JPopupMenu();
    // Install compeonents
    popupMenu.add(editCustomerItem);

    // === TABLE === //
    // Model for table
    defModel = new DefaultTableModel();
    defModel.addColumn("First name");
    defModel.addColumn("Last name");
    defModel.addColumn("Personal number");
    // Table
    table = new JTable(defModel);
    fillTable();

    // Scroll feature for table
    scroll = new JScrollPane(table);
    // refresh button
    refreshButton = new JButton("Refresh");
    refreshButton.addActionListener(this);
    // Install popup menu
    table.setComponentPopupMenu(popupMenu);
    // Install mouse listener
    table.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseReleased(MouseEvent me) {
            pNumOfCustomerSelected = (String) table.getValueAt(table.getSelectedRow(), 2);
            System.out.println(pNumOfCustomerSelected); // works fine
        }
    });


    // === MAIN FRAME === //
    // Main frame
    frame = new JFrame("Bank application");
    // Layout for frame
    frame.setLayout(new BorderLayout());
    // Install components to frame
    frame.add(scroll, BorderLayout.CENTER);
    frame.add(refreshButton, BorderLayout.SOUTH);
    frame.setJMenuBar(menuBar);
    // Settings for main frame
    frame.setSize(400, 400);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

/**
 * Fills the customer table with data
 */

public void fillTable(){
    customerDataList = bank.getAllCustomers();
    // Filling the table with rows (of customer info)
    for(int i = 0; i < customerDataList.size(); i++){
        String customerInfo = customerDataList.get(i);
        String[] customerInfoComponents = splitString(customerInfo);

        Vector row = new Vector();
        row.add(customerInfoComponents[0]);
        row.add(customerInfoComponents[1]);
        row.add(customerInfoComponents[2]);

        defModel.addRow(row);
    }
}

/**
 * Clears the table rows
 */

public void clearTable(){
    int rowCount = defModel.getRowCount();
    if(rowCount > 0) {
        for (int i = 0; i < rowCount; i++) {
            defModel.removeRow(0);
        }
    }
}

/**
 *
 * @param stringForProcess customer info
 * @return an array of the words in the string (separated by " ")
 */
String[] splitString(String stringForProcess){
    return stringForProcess.split("\\s+");
}

// action listener
@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource() == addCustomerItem)
        new AddCustomerWindow(bank, "Add customer");

    else if(e.getSource() == refreshButton){
        clearTable();
        fillTable();
    }
    else if(e.getSource() == editCustomerItem){
        Customer cust = bank.findCustomer(pNumOfCustomerSelected);

        // Customer customer = bank.findCustomer("12"); // This works fine???
        customer.setName("name");  // nullpointer
        customer.setSurName("surname");
    }
}

}

标签: javaswingjtableactionlistenermouselistener

解决方案


推荐阅读