0

I am back with another problem :(

Let me show you guys the code:

    JCheckBox pPec = new JCheckBox("Potion Effect");
    pPec.setBounds(new Rectangle(50, 270, 140, 30));
    pPec.setFont(fDisp);

    List<String> pPeLl = new ArrayList<String>();
    for (DPE dpe : DPE.values()){
        pPeLl.add(dpe.toString());
    }

    String[] pPeL = pPeLl.toString().replace("[", "").replace("]", "").split(", ");
    JComboBox<String> pPeE = new JComboBox<String>(pPeL);
    pPeE.setBounds(new Rectangle(175, 270, 150, 30));

    List<String> pPeNLl = new ArrayList<String>();
    for (int i = 1; i <= 255; i++){
        pPeNLl.add(Integer.toString(i));
    }
    String[] pPeNL = pPeNLl.toString().replace("[", "").replace("]", "").split(", ");


    JComboBox<String> pPeN = new JComboBox<String>(pPeNL);
    pPeN.setBounds(new Rectangle(175, 300, 73, 30));

    JTextField pPeT = new JTextField();
    ((AbstractDocument)pPeT.getDocument()).setDocumentFilter(new NumberFilter());
    pPeT.setBounds(new Rectangle(175+73+4, 300, 73, 30));




    if (file.exists()){
        for (String s : DFileLoader.getMethod(pathToSaveAs)){
            if (s.startsWith("playerPotionEffect%%@@")){
                pPec.setSelected(true);
                potionEffect = true;
                break;
            }else{
                pPeN.setEnabled(false);
                pPeT.setEnabled(false);
                pPeE.setEnabled(false);
                potionEffect = false;

            }
        }
        if (DFileLoader.getMethod(pathToSaveAs).size() <= 0){
            pPeN.setEnabled(false);
            pPeT.setEnabled(false);
            pPeE.setEnabled(false);
            potionEffect = false;

        }
    }else{
        pPeN.setEnabled(false);
        pPeT.setEnabled(false);
        pPeE.setEnabled(false);
        potionEffect = false;

    }


    pPec.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            if (pPec.isSelected()){
                pPeE.setEnabled(true);
                pPeN.setEnabled(true);
                pPeT.setEnabled(true);
            }else if (!pPec.isSelected()){
                pPeE.setEnabled(false);
                pPeN.setEnabled(false);
                pPeT.setEnabled(false);
            }

            if (pPec.isSelected()) potionEffect = true;
            else potionEffect = false;
        }
    });
    pane.add(pPec);
    if (file.exists()){
        for (String s : DFileLoader.getMethod(pathToSaveAs)){
            if (s.startsWith("playerPotionEffect%%@@")){
                String[] d = s.split("%%@@");
                String text;
                if (d.length <= 1) text = "";
                else text = d[3];

                pPeE.setSelectedItem(d[1]);
                pPeN.setSelectedItem(d[2]);
                pPeT.setText(text);
            }
        }
    }
    pane.add(pPeN);
    pane.add(pPeT);
    pane.add(pPeE);

The DFileLoader.getMethod(String) returns a String List

What I am trying to do is that it loads the information from a file, and if the file starts with "playerPotionEffect%%@@" (as shown after file.exists()), it sets the checkbox as selected. If not, it doesn't select it and it disables the other components shown in this piece of code. The checkbox selection works fine, it's just when I load the file, the 2 JComboBoxes and the JTextField are disabled, even though I want them to be enabled.

Can anyone help me? It might be really obvious where the problem is, and that I just haven't noticed. I have tried moving code around, but it still doesn't work.

the "pane" is a Container for the JFrame's content pane.

Also, I have it set to as you click on the checkbox, it enables and disables the other components. This works, and if I turned the checkbox off and on again, the components would be enabled. However, I just want it so that it is enabled on load when the box is checked.

Blockhead7360
  • 145
  • 10
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson May 07 '16 at 17:00
  • What is `DFileLoader.getMethod(pathToSaveAs)` returning? – rdonuk May 07 '16 at 17:01
  • @rdonuk It says right underneath the code. It returns a String list – Blockhead7360 May 07 '16 at 17:07
  • I mean what exactly returning? Because you are setting disable the components in the for loop. So, for example if the last string does not start with "playerPotionEffect%%@@", components will be disabled. So check it if all strings are starts with "playerPotionEffect%%@@". – rdonuk May 07 '16 at 17:11
  • @rdonuk What it returns is working well. All the combo boxes and text fields are filled in correctly. They are just disabled for some reason – Blockhead7360 May 07 '16 at 17:13
  • So, all of the elements starts with "playerPotionEffect%%@@", right? – rdonuk May 07 '16 at 17:19
  • @rdonuk I feel stupid – Blockhead7360 May 07 '16 at 17:25

1 Answers1

0

Lol I suck!

To fix this, I have to enable the components in the loop as well before the break. Not all the strings start with playerPotionEffect

xD

Blockhead7360
  • 145
  • 10