[英]Java jbutton in an arraylist, how can i know which one is clicked
我有两个大小相同的数组列表,一个用于JButton,一个用于JTextField。 单击按钮时,应更新相应的文本字段。 我怎么能知道呢?
ps jcomponents对(jbutton + jtextfield)的数量不固定。 它们旨在由用户添加或删除。 但是,它们必须处于固定顺序。
ArrayList<JButton> buttonList = new ArrayList<JButton>();
ArrayList<JTextField> textFieldList = new ArrayList<JTextField>();
private JButton createButton(){
JButton button = new JButton("Choose File");
buttonList.add(button);
button.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
textField = textFieldList.get(i)
textField.setText("show updats");
}
}
);
return button;
}
我如何知道JButton ArrayList中的索引“ i”?
ActionEvent
有一个source ,它将是触发事件的JButton
。
public void actionPerformed(ActionEvent event) {
int i = buttonList.indexOf(event.getSource());
.
.
.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.