0

I am Currently building a Number generator for my boss that is customizable in every front.

I can save a load start and end value etc but I cannot save and load the foreground colour the user has selected.

My radio button to select foreground colour:

rGreen.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent ae){
            if (rGreen.isSelected()){
                sColour.setText("GREEN");
                String gr = sColour.getText();
                if (gr =="GREEN"){
                tNumber.setForeground(Color.GREEN);
                rRed.setEnabled(false);
                rBlue.setEnabled(false);
                rYellow.setEnabled(false);
                rPink.setEnabled(false);
                rPurple.setEnabled(false);
                rWhite.setEnabled(false);
                rOrange.setEnabled(false);
                }
            }else
            {
                tNumber.setForeground(Color.WHITE);
                rRed.setEnabled(true);
                rBlue.setEnabled(true);
                rYellow.setEnabled(true);
                rPink.setEnabled(true);
                rPurple.setEnabled(true);
                rWhite.setEnabled(true);
                rOrange.setEnabled(true);
            }
        }
        }
    );

Save config

bSave = new JButton("Save");
    bSave.setFont(new Font("Tahoma", Font.BOLD, 11));
    bSave.setInheritsPopupMenu(true);
    mnConfiguration.add(bSave);
    bSave.addActionListener(this);
    f.setVisible(true);
    // ----------------Save Config------------------------------
    bSave.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            try {
                saveProperties();
                JOptionPane.showMessageDialog(f,
                        "Properties were saved successfully!");
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(f,
                        "Error saving properties file: " + ex.getMessage());
            }
        }
    });
    try {
        loadProperties();
    } catch (IOException ex) {
        JOptionPane
                .showMessageDialog(f,
                        "The config.properties file does not exist, default properties loaded.");
    }
    tName.setText(configProps.getProperty("Name"));
    tFrom.setText(configProps.getProperty("Start"));
    tTo.setText(configProps.getProperty("End"));
    sColour.setText(configProps.getProperty("Colour"));
    tTitle.setText(tName.getText());

save and Load

// -----------------------Saving-----------------------
private void saveProperties() throws IOException {

    configProps.setProperty("Name", tName.getText());
    configProps.setProperty("Start", tFrom.getText());
    configProps.setProperty("End", tTo.getText());
    configProps.setProperty("Colour", sColour.getText());
    OutputStream outputStream = new FileOutputStream(configFile);
    configProps.store(outputStream, "host setttings");
    outputStream.close();

}

// ------------------------Loading--------------
private void loadProperties() throws IOException {
    Properties defaultProps = new Properties();
    // sets default properties
    defaultProps.setProperty("Name", "Randomiser");
    defaultProps.setProperty("Start", "1");
    defaultProps.setProperty("End", "100");
    defaultProps.setProperty("Colour", "WHITE");

    configProps = new Properties(defaultProps);

    // loads properties from file
    InputStream inputStream = new FileInputStream(configFile);
    configProps.load(inputStream);
    inputStream.close();
}

So I thought if the green radio button was pushed the JTextField would be changed to GREEN and if the text field was GREEN it would change the foreground colour to GREEN. But what happens is the text field changes to GREEN but the foreground won't change.

What am I doing wrong here?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ajay Ward
  • 27
  • 1
  • 7
  • 1
    Let's start with `gr =="GREEN"` isn't how `String` comparison in Java works and you should be using something more like `"GREEN".equals(gr)` instead – MadProgrammer Mar 10 '15 at 03:08
  • yea i just figured that out but it still doesn't save it sets the Jtextfield to green but doesn't set the rGreen radio button as pushed down – Ajay Ward Mar 10 '15 at 03:50
  • The way I read the question, you couldn't figure out why the text color wouldn't change, despite what you title might have said. Pick a problem – MadProgrammer Mar 10 '15 at 03:53
  • Calling `sColour.setText(configProps.getProperty("Colour"));` doesn't activate the associated `ActionListener`. Consider writing a method which you can call when you load the properties AND when your `ActionListener` is called which updates the state of the component – MadProgrammer Mar 10 '15 at 03:55

0 Answers0