每次用户尝试拖动 CheckBox 并将其从一个面板粘贴到另一个面板时,我都需要做一些事情。我知道 Java 提供拖放 API,但我并不完全希望复选框从一个面板拖到另一个面板。我想要的是给用户一种拖放的错觉,在幕后我希望我的代码运行并执行某些操作。我该怎么做?
现在,当我将复选框 image1 从 panelleft 拖放到 panel_right 时,我希望某些代码在用户的拖放操作中在后台运行
for(ResourceListObject currentImage : imageList ){
imageOnRepositoryCheckBox[checkBoxNumber] = new JCheckBox(currentImage.getName());
imageOnRepositoryCheckBox[checkBoxNumber].setBounds(6, gapping+checkBoxNumber*26, 368, 23);
imageOnRepositoryCheckBox[checkBoxNumber].setTransferHandler(new FromTransferHandler());
if(imagesToBeImported != null){
if(imagesToBeImported.contains(currentImage)){
imageOnRepositoryCheckBox[checkBoxNumber].setForeground(Color.GRAY);
imageOnRepositoryCheckBox[checkBoxNumber].setToolTipText("This image is already on the list of images to be imported and can't be selected again.");
}
}
panel.add(imageOnRepositoryCheckBox[checkBoxNumber]);
checkBoxNumber++;
}
第二段将运行的代码是
for(JCheckBox currentCheckBox : imageOnRepositoryCheckBox){
if(currentCheckBox.isSelected()){
Iterator itr = imagesOfCurrentRepository.iterator();
while(itr.hasNext()) {
ResourceListObject iteratedImage = (ResourceListObject)itr.next();
if(iteratedImage.getName().equals(currentCheckBox.getText())){
boolean isAdded = imagesToBeImported.add(iteratedImage);
descriptionPanel.updateDescription("The image selected for importing is "+currentCheckBox.getText());
if(isAdded){
currentCheckBox.setForeground(Color.GRAY);
currentCheckBox.setToolTipText("This image is already on the list of images to be imported and can't be selected again.");
}
}
}
updateImagesToBeImportedPanel(panel_1, imagesToBeImported);
}
checkBoxNumber++;
}
所以我希望用户将其视为拖放,但在后端我会做自己的事情。