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

Arnoud
  • 79
  • 2
  • 11
  • For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Nov 07 '12 at 14:20
  • @Andrew Thompson What do you mean by that? I am fairly new to asking other people for help. Isn't my third code block a SSCCE? – Arnoud Nov 07 '12 at 14:23
  • 1
    What don't you understand after reading the linked article? – Andrew Thompson Nov 07 '12 at 14:25
  • @Andrew Thompson Well in my opinion this was a fairly short question since the first alinea is only an introduction to what I want to achieve while the second alinea is the actual question. Other than that I do happen to have long code blocks, but I believe they might be needed to make sure I don't have any code that's left out that ruins my JOptionPane. Or is it just the introduction that is too long? – Arnoud Nov 07 '12 at 14:28
  • 1
    a) The term SSCCE has five words. The **first** word is short. Read the others. Uncompilable code snippets fit none of them. b) And while on the subject of 'short', the article explicitly comments on doing ten things that might be done just once or twice to show the effect. The last snippet has 16 lines ending in `.setText("");`. If you can do it once, you can do it 15 more times with ease. So **leave out** the other 15. – Andrew Thompson Nov 07 '12 at 14:32
  • @Arnoud Aarnoudse if I put that to my code then works, no issue, have to post your view, short, runnable, compilable – mKorbel Nov 07 '12 at 14:35
  • Your code seems to be fine, the problem shouldn't have nothing to do with the remarked line, check the invocation to the `display` method and make sure that is not getting called twice (some event listeners get called twice for a lot of reasons) – higuaro Nov 07 '12 at 14:37
  • @Andrew Thompson Ok I see your point, and I agree with the logic of not repeating a part of the code multiple times. By the way I would like to thank you for all the replies :) – Arnoud Nov 07 '12 at 14:37
  • @mKorbel thanks for your reply, I understood that now from Andrew but thanks for your reply. And I guess my problem lies somewhere else then. – Arnoud Nov 07 '12 at 14:41
  • @h3nr1x You are correct. I've done something very stupid. I forgot that originally it was a method without a JOptionPane and used it in a check. if(cfa.display(resultList) != null){ results.addAll(cfa.display(resultList)); Thanks for your help. – Arnoud Nov 07 '12 at 14:44
  • @Andrew Thompson Yes I was going to :) But indeed there are some time limits on some of the functions because I'm a new user. I have answered my own question now though. – Arnoud Nov 09 '12 at 12:37
  • *"time limits on some of the functions because I'm a new user."* Time limits still apply to more experienced users. The other week I entered a [question and answer](http://stackoverflow.com/questions/12986333/appearance-of-java-security-dialog) and could not accept the answer immediately. – Andrew Thompson Nov 09 '12 at 12:43

1 Answers1

1

I made a dumb mistake. Originally the code was used without having an JOptionPane. So I forgot I used it in a check.

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

I would like to thank the people who have commented though. I have learned something new about asking questions and to make sure to check every part of my code even if it may seem unrelated.

-lordarnoud

Arnoud
  • 79
  • 2
  • 11