我从 JFileChooser 在我的 JList 中添加了一些文件。我正在添加一个名为“CHECK”的新按钮,单击该按钮会告诉 JList 中是否存在特定文件(在已添加的文件中)。如果你们中的任何人能告诉我执行此步骤的正确程序是什么,那就太好了。
提前谢谢你..
这是我目前的代码;
final JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fileChooser.setMultiSelectionEnabled(true);
getContentPane().add(fileChooser, "cell 0 0 3 9");
JScrollPane scrollPane = new JScrollPane();
getContentPane().add(scrollPane, "cell 10 1 3 8,grow");
vector = new Vector<File>();
final JList list = new JList(vector);
scrollPane.setViewportView(list);
JPanel panel = new JPanel();
getContentPane().add(panel, "cell 3 4 7 1,grow");
JButton btnNewButton = new JButton("Add Files");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (File file : fileChooser.getSelectedFiles()) {
vector.add(file);
System.out.println("Added..!!");
}
list.updateUI();
}
});
panel.add(btnNewButton);
JButton btnNewButton_1 = new JButton("Remove Files");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(list.getSelectedIndices().length > 0) {
int[] selectedIndices = list.getSelectedIndices();
for (int i = selectedIndices.length-1; i >=0; i--) {
vector.removeElementAt(i);
System.out.println("Removed..!!");
}
}
list.updateUI();
}
});
panel.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("Check For Files");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = "";
if(list.getSelectedIndices().length > 0 ) {
//// to check if a file exists /////
}
}
});
panel.add(btnNewButton_2);