Properties

Discussion in 'Resources' started by NullField, May 28, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    NullField

    This is my first post, so please excuse whatever noobness there may be.

    While working on a plugin that uses database, setting up the properties over and over started to get a bit annoying. To combat the solution, I initially tried using console input (a horrible failure). After a bit of thinking, input boxes came into mind. I did a bit of research and came across JFrames, "a starting point for graphical applications."

    This is the code with the implemented... code?
    The new code is surrounded by asterisks (couldnt bold :/)

    ---------------------------------------------------------------------------------------------------------------
    test
    Code:
    import java.io.File;
    import java.util.logging.Logger;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class test extends JavaPlugin {
        Logger logger = Logger.getLogger("Minecraft");
    
        @Override
        public void onDisable() {
    
        }
    
        @Override
        public void onEnable() {
            activePropertiesProperties properties = new activePropertiesProperties(this, new File("plugins/activeProperties/activeProperties.properties"));
            properties.makeProperties();
        }
    
    }
    
    testProperties
    Code:
    import java.io.File;
    import java.io.IOException;
    import java.util.Properties;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    
    *******************************************
    import javax.swing.JOptionPane;
    *******************************************
    
    public class testProperties extends Properties {
        private static final long serialVersionUID = 1L;
    
        private File propertiesFile;
        private test plugin;
    
        public activePropertiesProperties(test instance, File file) {
            plugin = instance;
            propertiesFile = file;
        }
    
        private void load() {
            try {
                load(new FileInputStream(propertiesFile.getPath()));
            } catch(IOException e) { }
        }
    
        private void save() {
            try {
                store(new FileOutputStream(propertiesFile.getPath()), "");
            } catch(IOException e) { }
        }
    
        private String getString(String key) {
            if(containsKey(key)) return getProperty(key);
    
            ****************************************************************************
            //Get the Input... showInputDialog(MESSAGE, PRESET VALUE);
            String input = JOptionPane.showInputDialog(key + ": ", "");
            ****************************************************************************
            //Store the Input
            put(key, input);
            //Return the Input
            return input;
        }
    
        public void makeProperties() {
            //Make the Properties file directory.
            new File(propertiesFile.getParent()).mkdir();
            //If the Properties file does not exist, create it.
            if(!propertiesFile.exists()) {
                try {
                    propertiesFile.createNewFile();
                } catch(IOException e) { }
            }
            //Load the property file
            load();
    
            //Load the properties
            String dbHost = getString("databaseHost");
            plugin.logger.info("You Have Set dbHost to " + dbHost + "!");
            //Save the property file
            save();
        }
    }
    
    ---------------------------------------------------------------------------------------------------------------

    I'm not really sure what else to say.
    Since this is my first post, some pointers would be greatly appreciated!
     
  2. Offline

    masteroftime

    What you used in your code is a JOptionPane which is a really cool class for displaying all kinds of standard dialogs with an input field, yes/no options, error messages etc. But thats it. You use them if you need to promt the user for a single input value or show them an error/information message.

    But when you want to have a more advanced GUI you need JFrame. JFrame is a standard window to which you can add all kind of buttons, input fields, text boxes and so on.

    When youre new to Java I'd recommend staying with JOptionPanes (your code should run fine anyway). If you really want more advanced GUI's you could use a GUI Builder like NetBeans which does all the layout coding for you. But I guess you dont need that for your plugin anyway.
     
  3. Offline

    Sammy

    Hi there !
    First: wrong place to post, this is for RESOURCES not questions!
    Second: plugins run server side, so its impossible to make a user see a GUI, the only input you can use is the chat

    Cheers
     
Thread Status:
Not open for further replies.

Share This Page