1

I was doing a project in JAVA Swing Netbeans IDE 7.1. I created one jFrame with some button and Dropdowns on it. On selecting a choice from the dropdown,another frame object is created and setVisible is set to true. But instead of showing one window, it is showing up 2 windows. There are other similar calls, but none have this issue, someone please help me. Thanks.

Code:

private void itemListItemStateChanged(java.awt.event.ItemEvent evt) {                                          

        String item = null;
        String filename = null;
        item = (String) itemList.getSelectedItem();
        if(item=="P"){

            filename="p";
            description.setText("Description:  P");
        }
        else if(item=="A"){

            filename="a";
            description.setText("Description:  A");
        }
        else if(item=="R"){

            filename="r";
            description.setText("Description:  R");
        }

        else if(item=="S"){

            filename="s";
            description.setText("Description:  S");
        }

        else if(item=="X"){
            displayText.setText("");
            x xl = new x();
            xl.setVisible(true);

        }

        else if(item=="Xx"){

            filename="xx";
            description.setText("Description: xx");
        }

        else {
            System.out.println("invalid selection.");

        }


        if (item=="X"){
            return;
        }
        else {
             displayText.setText("");

            BufferedReader b = null;
        try {
            b= new BufferedReader(new FileReader ("/home/sfred/"+filename+".mile"));

        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
        try {
            line = b.readLine();
        } catch (IOException ex) {
           ex.printStackTrace();
        }

        while (line != null){
            displayText.append(line + "\n");
            try {
                line=b.readLine();
            } catch (IOException ex) {
                ex.printStackTrace();
            }

        }
        }
SF-Red
  • 19
  • 2
  • Thanks for the answer, but I have coded too much so that I dont want to modify much... Kinnda lazy ... ;) .. Could you help me with this please? I have done this before, but this is the first time am getting something like this. :(.... – SF-Red Dec 06 '12 at 09:45

1 Answers1

3
  • See The Use of Multiple JFrames, Good/Bad Practice? rather use :

    1) JDialog

    2) CardLayout which will allow you to flip between multiple components on a single JFrame.

  • You cannot compare String using ==operator must use equals(String s) or else it wont evaluate correctly

    if(item.equals("X")) {
    }
    
  • Rather use switch statement (Java 7+) when comparing String:

    switch(item) {
        case "P":
            //do something
        break;
    
        case "X":
           //do something
        break;
    
        default:
           //if no match with above was found..
        break;
    }
    
Community
  • 1
  • 1
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • These all say about not using multiple jFrames, could you please help me with the current situation. I am a newbie in java development, Also, when I set System.out.println(); inside a itemChanged event, it prints the text 2 times.!! – SF-Red Dec 06 '12 at 10:07
  • @SF-Red have you done the suggested changes especaily the String comparison, and I also see 2 checks or *X* this might be whats casuing the 2 `System.out.println`s – David Kroukamp Dec 06 '12 at 10:47