Search code examples
javajoptionpane

How to display different Arrays in separate JOptionPane


I'm trying to make 3 JOptionPane. Each one must show some array of data.

The first must show the 20 information of firstDataArray

Only when the user click on cancel I want the new JOptionPane to pop up it must display the information of secondDataArray etc...

The problem I'm facing is when the user click on cancel on the first JOptionPane , the Second JOptionPane is displaying all the information of the firstDataArray and the secondDataArray (I want 2 separated JOptionPane with different array list)

Same when the cancel bottom of the second JOptionPane, the third JoptionPane displays all the information of the 3 data Array

What I'm doing wrong ?

Much Appreciation ,

Bass

      String[] firstDataArray= new String[20];
            String[] secondDataArray= new String[20];
            String[] thirdDataArray= new String[20];

            firstDataArray= ClassA.getFirstData();
            secondDataArray= ClassA.getSecondData();
            thirdDataArray= ClassA.getThirdData();

            StringBuilder textRegion = new StringBuilder();

            String txt = JOptionPane.showInputDialog(null,
                    textRegion.append(Arrays.toString(firstDataArray).replace("[", "").replace("]", "").replace(",", "")).append('\n'), "Choose Option)",
                    JOptionPane.PLAIN_MESSAGE);
//If the user click cancel , show the other array in a new JoptionPane   
            if (txt == null) {

                txt = JOptionPane.showInputDialog(null,
                        textRegion.append(Arrays.toString(secondDataArray).replace("[", "").replace("]", "").replace(",", "")).append('\n'), "Choisir municipalite (Chose option)",
                        JOptionPane.PLAIN_MESSAGE);


//If the user click cancel again , show the other array in a third JoptionPane  
                    if (txt == null) {

                    txt = JOptionPane.showInputDialog(null,
                            textRegion.append(Arrays.toString(thirdDataArray).replace("[", "").replace("]", "").replace(",", "")).append('\n'), "Chose Option)",
                            JOptionPane.PLAIN_MESSAGE);
                    if (txt == null) {

                    } else {
                        setNomMunicipalite(txt);
                    }

                } else {
                    setNomMunicipalite(txt);
                }
            } else {
                setNomMunicipalite(txt);
            }

Solution

  • You are always appending each array to the same StringBuilder.

    Use a new one for each JOptionPane

            StringBuilder textRegion = new StringBuilder();
    
            String txt = JOptionPane.showInputDialog(null,
                    textRegion.append(Arrays.toString(firstDataArray).replace("[", "").replace("]", "").replace(",", "")).append('\n'), "Choose Option)",
                    JOptionPane.PLAIN_MESSAGE);
            //If the user click cancel , show the other array in a new JoptionPane   
            if (txt == null) {
                textRegion = new StringBuilder(); // <--- 
                txt = JOptionPane.showInputDialog(null,
                        textRegion.append(Arrays.toString(secondDataArray).replace("[", "").replace("]", "").replace(",", "")).append('\n'), "Choisir municipalite (Chose option)",
                        JOptionPane.PLAIN_MESSAGE);
    
    
                    //If the user click cancel again , show the other array in a third JoptionPane  
                    if (txt == null) {
                    textRegion = new StringBuilder(); // <---
    
                    txt = JOptionPane.showInputDialog(null,
                            textRegion.append(Arrays.toString(thirdDataArray).replace("[", "").replace("]", "").replace(",", "")).append('\n'), "Chose Option)",
                            JOptionPane.PLAIN_MESSAGE);
                    if (txt == null) {
    
                    } else {
                        setNomMunicipalite(txt);
                    }
    
                } else {
                    setNomMunicipalite(txt);
                }
            } else {
                setNomMunicipalite(txt);
            }