Jump to content

Best way to get info from gui?


roguehippo

Recommended Posts

hello, i was wondering what the best ways to get info (like if a checkbox is checked or if a button is selected) from the gui.

the reason i ask is because i have run into a couple problems. what i have tried so far: if i use a statement to check like 

 

if(gui.button.isSelected) the button never shows up in the "gui." pop up menu. and if in the gui class i try to make a function to get the details of an item it doesn't allow me to access the variable. can anyone explain how to correctly get the information from the gui. thanks!

Link to comment
Share on other sites

Well there are a range of ways but the easiest to start with and the one which I pretty much always use (atleast for simple guis) is with action listeners. Essentially, if you put a start button on your gui and add an action listener to it, then whenever the button is pressed the code inside the action listener will run. In this block you can just statically change values of variables in your main class (or send them over with setters). Additionally you can dispose of the gui in this block. Alternatively, if you don't want a start button, you can just add action or change listeners to the various components in your gui and send data like that instead. If you need snippets, feel free to pm me but if you think you can manage then gl! :D I know it's no fun when people just give you the code so it's always nice to try and work it out for yourself !

 

~apa

Link to comment
Share on other sites

hello, i was wondering what the best ways to get info (like if a checkbox is checked or if a button is selected) from the gui.

the reason i ask is because i have run into a couple problems. what i have tried so far: if i use a statement to check like 

 

if(gui.button.isSelected) the button never shows up in the "gui." pop up menu. and if in the gui class i try to make a function to get the details of an item it doesn't allow me to access the variable. can anyone explain how to correctly get the information from the gui. thanks!

 

Because you have not shown any code it is hard to understand exactly what you are doing wrong.

 

However the issue you has described sounds like the result of a lack of understanding of basic Java. I strongly recommend you follow some Java tutorials to understand the basics.

 

From what you have described I can think of two things you might be doing wrong.

 

Firstly, you might not be storing the button as a global variable. If you are not doing this, the variable is local to the method in which you created it and so cannot be accessed outside of it. To be able to access this variable from outside of the class you will need to store it globally:

public final class Gui extends JFrame {

    final JButton button;

    public Gui() {
        button = new JButton();
    }
}

If you are already doing this, then it is likely an issue with your usage of access modifiers. There are several different access modifiers https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

 

If your Gui class is in a different package to the class you are trying to access button from, then you will need to give the button the 'public' access modifier.

public final class Gui extends JFrame {

    public final JButton button;

    public Gui() {
        button = new JButton();
    }
}

If your Gui class is in the same package as the class you are trying to access the button from you don't need to give it any modifier.

 

If the button is private then you will need to provide a getter method to access this variable from outside of the class:

public final class Gui extends JFrame {

    private final JButton button;

    public Gui() {
        button = new JButton();
    }

    public final JButton getButton() {
        return button;
    }
}

An alternative to all of the above would be to  make some kind of configuration class. For example for a woodcutter you might make:

public final class ScriptConfig {

    private Tree selectedTree;
    private boolean shouldBank;
    
    public final void setSelectedTree(final Tree selectedTree) {
        this.selectedTree = selectedTree;
    }

    public final void setShouldBank() {
        shouldBank = true;
    }

    public final Tree getSelectedTree() {
        return selectedTree;
    }

    public final boolean shouldBank() {
        return shouldBank;
    }
}

You then create an instance of this config in your Script class:

@ScriptManifest(...)
public class Chopper extends Script {

    @Override
    public void onStart() {
        ScriptConfig config = new ScriptConfig();
        Gui gui = new Gui(config);
    }

    ...
}

And then pass this config class to your Gui:

public final Gui extends JFrame {
 
    public Gui(final ScriptConfig config) {
    
        // init stuff
    }
}

Then, when the user presses the start button, or however you indicate that the script is started, you can set all the configuration options:

public final Gui extends JFrame {
 
    private final JCheckBox shouldBankCheck;
    private final JComboBox<Tree> treeSelector;
    private final JButton startButton;

    public Gui(final ScriptConfig config) {
        // init stuff

        startButton.addActionListener(e -> {
            if(shouldBankCheck.getState()) config.setShouldBank();
            config.setSelectedTree(treeSelector.getSelectedItem());
        });
    }
}

And finally, you will be able to access the chosen settings from the config instance in your Script class:

@ScriptManifest(...)
public class Chopper extends Script {

    @Override
    public void onStart() throws InterruptedException {
        ScriptConfig config = new ScriptConfig();
        Gui gui = new Gui(config);

        // sleep while gui is open

        if(config.shouldBank()) ...
        
        Tree selectedTree = config.getSelectedTree();
    }

    ...
}
Edited by Explv
  • Like 1
Link to comment
Share on other sites

Create static variable in your main class --> create an ActionListener for your start button --> have all your if checkbox is selected statements in the ActionListener and fetch the variable from the main class and set it to w/e

JButton startButton= new JButton("Start");
		startButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if checkbox is selected
                                  MainClass.myVariable = something
                                if combobox = something
                                  MainClass.myOtherVariable = something
			}
		});
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...