Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8804969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:44:40+00:00 2026-06-14T01:44:40+00:00

This is my second time posting a question on stackOverflow. In my code I

  • 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T01:44:40+00:00Added an answer on June 14, 2026 at 1:44 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my first time posting on here and have read alot of helpful
this is the second time I am posting this on stack overflow forum, the
I execute an ajax function that takes 3 seconds + so in this time
This is my second attempt at this question . I made a mess of
My problem is very similar to this question , however I am posting a
I cannot find a specific example of this, so am posting the question. Any
this is my first time posting as I usually just come by to do
First time posting in Stack Overflow, hope I'm doing this right. I'm writing an
I have an on line project question of the week; this project allows the
This is a follow up to a previous question: Disparity between date/time calculations in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.