0

This is my second time posting a question on stackOverflow. In my code I have a JOptionPane that will allow the user to press "OK" or "Cancel". If the user presses "OK" the code should return an ArrayList containing either ALL the values or the SELECTED values.

I have tested this code and it seems to work, or so it seems. Whenever the user clicks on the "OK" button for the first time, the JOptionPane closes but reappears after that. When the user has clicked on the "OK" button for a second time, the code works like it should, and the window closes without reappearing.

/**
 * 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;
    }
}

I believe the problem is in this line:

 if (result == JOptionPane.OK_OPTION) {

But I don't understand why because I already have done a similar thing in my actionHandler for the delete button, which works like it should.

/**
     * 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");
            }
        }
    });

I'm hoping my question has enough structure and information to be answered.

-lordarnoud

4

1 回答 1

1

我犯了一个愚蠢的错误。最初使用代码时没有 JOptionPane。所以我忘了我在支票上用过它。

if(cfa.display(resultList) != null){
            results.addAll(cfa.display(resultList));

我要感谢那些发表评论的人。我学到了一些关于提问的新知识,并确保检查我的代码的每一部分,即使它看起来不相关。

-洛达努德

于 2012-11-09T12:36:43.703 回答