【发布时间】:2013-03-25 13:03:41
【问题描述】:
所以我有this 项目,
源代码是here。
当您运行项目并转到处理时,那里有一个jcombobox,假设有一个 addActionListener。
p_customer_list = new JComboBox<>(customers_name);
pp_customer_list.setPreferredSize(new Dimension(360, 35));
panel_processing_header.add(pp_customer_list);
//pp_customer_list.addActionListener(this);
pp_customer_list.addActionListener (new ActionListener () {
public void actionPerformed(ActionEvent e) {
JComboBox tmpBox = (JComboBox) e.getSource();
int selected = tmpBox.getSelectedIndex();
pp_refresh_data(selected);
}
});
这是我到目前为止所拥有的,它假设当组合框的值发生变化时找到选定的索引并将其传递给pp_refresh_data(),但由于某种原因它没有运行(我尝试放置一个JOptionPane 来查看当代码执行时,它只在程序运行时执行一次。)
【问题讨论】:
-
既然知道是 pp_customer_list,为什么还要获取 ActionEvent 的来源?只需将 JComboBox 设为 final 并在侦听器中使用它。另外请不要在名称中使用下划线,除非所有字母都大写。否则,只需使用 pCustomerList 之类的东西。
-
@AliAlamiri 从事件中获取源代码(适当地保护 classCastException)是一个好主意 - 解耦代码。
标签: java swing actionlistener jcombobox java-7