stackOverflowに質問を投稿するのはこれが2回目です。私のコードには、ユーザーが「OK」または「キャンセル」を押すことができるJOptionPaneがあります。ユーザーが「OK」を押すと、コードはすべての値またはSELECTED値のいずれかを含むArrayListを返す必要があります。
私はこのコードをテストしましたが、動作しているようです。ユーザーが初めて[OK]ボタンをクリックすると、JOptionPaneは閉じますが、その後再び表示されます。ユーザーが[OK]ボタンをもう一度クリックすると、コードは正常に機能し、ウィンドウは再表示されずに閉じます。
/**
* This method will show a list of files to the user and give the user the option to continue or abort the copy.
*
* @param resultList
* @return ArrayList with values or null
*/
public ArrayList<String> display(ArrayList<String> resultList) {
JPanel panel = new JPanel();
//Here the list is being filled with the arrayList from the parameter.
JList list = new JList(resultList.toArray());
//Here the JLabel is added to the JPanel.
panel.add(new JLabel("These files will be copied:"));
JScrollPane scrollpane = new JScrollPane(list);
scrollpane.setPreferredSize(new Dimension(400,200));
panel.add(scrollpane);
//Here the JOption,Pane is being shown with a confirm dialog.
int result = JOptionPane.showConfirmDialog(null, panel, "Copy",JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
//If the users clicks on the OK button the method will return the selected values.
if (result == JOptionPane.OK_OPTION) {
//Check if the user has selected some values, if not the method will return all the values.
if(list.getSelectedValues().length < 1){
return resultList;
}
//If the user has selected some values the method will get them, fill an ArrayList with them and return them.
else{
@SuppressWarnings({ "rawtypes", "unchecked" })
ArrayList<String> results = new ArrayList(Arrays.asList(list.getSelectedValues()));
return results;
}
}
//Return null if the user has pressed Cancel.
else{
return null;
}
}
問題は次の行にあると思います。
if (result == JOptionPane.OK_OPTION) {
しかし、削除ボタンのactionHandlerで同じようなことをすでに行っているので、理由がわかりません。これは正常に機能します。
/**
* This is the actionListener for the delete button.
* This method will delete the values and properties of the selected configuration.
*/
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
MessageConsole mc = new MessageConsole(textArea);
//Here the getSaveAction is being used with the correct parameter values that are gained from the fields.
mc.redirectOut();
//Check if the user has entered a configuration name.
if(configName.getSelectedItem().toString().length() > 0){
int result1 = JOptionPane.showConfirmDialog(null, "Are you sure you want to delete this configuration?", "Delete", JOptionPane.YES_NO_OPTION);
if(result1 == JOptionPane.YES_OPTION){
int result2 = JOptionPane.showConfirmDialog(null,"Are you really sure?", "Delete", JOptionPane.YES_NO_OPTION);
if(result2 == JOptionPane.YES_OPTION){
//If the user has entered a configuration name the method will delete all the properties and values that belong to it.
gf.getDeleteAction(configName.getSelectedItem().toString());
sourceField.setText("");
destinationField.setText("");
endsWithField.setText("");
containsField.setText("");
yearAfterField.setText("");
monthAfterField.setText("");
dayAfterField.setText("");
hourAfterField.setText("");
minuteAfterField.setText("");
secondAfterField.setText("");
yearBeforeField.setText("");
monthBeforeField.setText("");
dayBeforeField.setText("");
hourBeforeField.setText("");
minuteBeforeField.setText("");
secondBeforeField.setText("");
setComboBox();
}
if(result2 == JOptionPane.NO_OPTION){
System.out.println("Nothing has been deleted.");
}
}
if(result1 == JOptionPane.NO_OPTION){
System.out.println("Nothing has been deleted.");
}
}
//If the user has not entered a configuration name the system will print a message.
else{
System.out.println("You need to select a configuration name");
}
}
});
私の質問が答えられるのに十分な構造と情報を持っていることを望んでいます。
-lordarnoud