Jump to content

Explv's Dank GUI Tutorial


Explv

Recommended Posts

Why declare a JFrame within the script class? Separate it.

public class GUI extends JFrame {
    public GUI() {
        setTitle("GUI);
        ...
        pack();
        setVisible(true);
    }
}

Instantiate within the script class

public class ShitScript extends Script {
    private GUI gui;
...
    @Override
    public void onStart() {
        gui = new GUI();
    }

    @Override
    public void onExit() {
        if(gui != null) gui.dispose();
    }
...
}

This way, you can manage your GUI without having to trawl through a ton of unrelated code in the script class.

 

Good input, as some might want better coding practices. Good guide none-the-less.

Link to comment
Share on other sites

  • 1 month later...

Hello man, I've never worked with IntelliJ in my life so sorry for asking this, I've made a form but how do I add this to the script?

@Explv

 

When you create a form in IntelliJ it also creates a binding class. For example if you named your form "GUI" there will also be a Java class called "GUI" that is created.

 

In the form view you will see that the there is a top level container, which is a JPanel. The first thing to do is name this JPanel, you can call it "mainPanel" or something like that.

 

Then make the binding class extend JFrame. JFrame is the main window for a swing GUI.

 

Your GUI class should look something like:

public class GUI extends JFrame {

    JPanel mainPanel;
    // some other variables
}

Now, you will need to add the JPanel (mainPanel) that you created in the form view, to this gui:

public class GUI extends JFrame {

    JPanel mainPanel;
    // some other variables

    public GUI(){
        add(mainPanel);
    }
}

Now, in your script when you want to display this GUI you can simply do:

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

    private GUI gui;

    @Override
    public void onStart() {
        gui = new GUI();
        gui.setVisible(true);
    }

    @Override
    public int onLoop() throws InterruptedException {
       return 0;
    }

    @Override
    public void onExit() {
        if(gui != null) {
            gui.setVisible(false);
            gui.dispose();
        }
    }
}
  • Like 1
Link to comment
Share on other sites

When you create a form in IntelliJ it also creates a binding class. For example if you named your form "GUI" there will also be a Java class called "GUI" that is created.

 

In the form view you will see that the there is a top level container, which is a JPanel. The first thing to do is name this JPanel, you can call it "mainPanel" or something like that.

 

Then make the binding class extend JFrame. JFrame is the main window for a swing GUI.

 

Your GUI class should look something like:

public class GUI extends JFrame {

    JPanel mainPanel;
    // some other variables
}
Now, you will need to add the JPanel (mainPanel) that you created in the form view, to this gui:

public class GUI extends JFrame {

    JPanel mainPanel;
    // some other variables

    public GUI(){
        add(mainPanel);
    }
}
Now, in your script when you want to display this GUI you can simply do:

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

    private GUI gui;

    @Override
    public void onStart() {
        gui = new GUI();
        gui.setVisible(true);
    }

    @Override
    public int onLoop() throws InterruptedException {
       return 0;
    }

    @Override
    public void onExit() {
        if(gui != null) {
            gui.setVisible(false);
            gui.dispose();
        }
    }
}

 

Holy shit thanks for this fast response, I get it! You'll get credits when I finish my script :)

Link to comment
Share on other sites

 

When you create a form in IntelliJ it also creates a binding class. For example if you named your form "GUI" there will also be a Java class called "GUI" that is created.

 

In the form view you will see that the there is a top level container, which is a JPanel. The first thing to do is name this JPanel, you can call it "mainPanel" or something like that.

 

Then make the binding class extend JFrame. JFrame is the main window for a swing GUI.

 

Your GUI class should look something like:

public class GUI extends JFrame {

    JPanel mainPanel;
    // some other variables
}

Now, you will need to add the JPanel (mainPanel) that you created in the form view, to this gui:

public class GUI extends JFrame {

    JPanel mainPanel;
    // some other variables

    public GUI(){
        add(mainPanel);
    }
}

Now, in your script when you want to display this GUI you can simply do:

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

    private GUI gui;

    @Override
    public void onStart() {
        gui = new GUI();
        gui.setVisible(true);
    }

    @Override
    public int onLoop() throws InterruptedException {
       return 0;
    }

    @Override
    public void onExit() {
        if(gui != null) {
            gui.setVisible(false);
            gui.dispose();
        }
    }
}

 

I can't seem to get it to work, when I use .setVisible(true), it can't find the symbol.

Link to comment
Share on other sites

Ok, I got it a working a little bit, I needed to add, setVisible in the constructor of the GUI not in my script, the problem is I have a lot of NullPointerExceptions

 


java.lang.NullPointerException
	at java.awt.Container.addImpl(Container.java:1093)
	at java.awt.Container.add(Container.java:1005)
	at javax.swing.JFrame.addImpl(JFrame.java:567)
	at java.awt.Container.add(Container.java:417)
	at GHWoodcutterGUI.<init>(GHWoodcutterGUI.java:12)
	at GHWoodcutter.onStart(GHWoodcutter.java:39)
	at org.osbot.rs07.event.ScriptExecutor.iiIIiiIIiI(lk:265)
	at org.osbot.rs07.event.ScriptExecutor.start(lk:158)
	at org.osbot.qB.run(tm:137)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)
[INFO][Bot #1][05/12 01:56:48 PM]: Terminating script GHWoodcutter...
Edited by GaetanoH
Link to comment
Share on other sites

 

Ok, I got it a working a little bit, I needed to add, setVisible in the constructor of the GUI not in my script, the problem is I have a lot of NullPointerExceptions

 

java.lang.NullPointerException
	at java.awt.Container.addImpl(Container.java:1093)
	at java.awt.Container.add(Container.java:1005)
	at javax.swing.JFrame.addImpl(JFrame.java:567)
	at java.awt.Container.add(Container.java:417)
	at GHWoodcutterGUI.<init>(GHWoodcutterGUI.java:12)
	at GHWoodcutter.onStart(GHWoodcutter.java:39)
	at org.osbot.rs07.event.ScriptExecutor.iiIIiiIIiI(lk:265)
	at org.osbot.rs07.event.ScriptExecutor.start(lk:158)
	at org.osbot.qB.run(tm:137)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)
[INFO][Bot #1][05/12 01:56:48 PM]: Terminating script GHWoodcutter...

 

Can you post your GUI code?

Link to comment
Share on other sites

import javax.swing.*;
import java.awt.*;

/**
 * Created by Gaetano on 12/05/16.
 */
public class WoodcutterGUI extends JFrame{
    private JPanel panel;

    public WoodcutterGUI() {
        super("GHWoodcutter");
        add(panel);
        setVisible(true);
    }
}

It's probably something stupid, haven't made a GUI in ages.

You are using Intellij Form Designer though right?

private JPanel panel;
If that JPanel is not bound to the JPanel in Intelij Form Designer, then when you call:
add(panel);
It will throw a NullPointerException.

Here is a graphical example of what I meant in my previous comment.

Firs you have to provide a name for the top level JPanel in the .form view:

bZq6UVK.png

Then in the bound Java class with the same name you do:

import javax.swing.*;

public class GUI extends JFrame {
    private JPanel mainPanel;

    public GUI(){
        add(mainPanel);
        setTitle("Whatever");
        setVisible(true);
    }
}
This code works perfectly fine.

Don't forget you will also need to set a preferred size on your JFrame otherwise it will be very small:

import javax.swing.*;
import java.awt.*;

public class GUI extends JFrame {
    private JPanel mainPanel;

    public GUI(){
        add(mainPanel);
        setTitle("Whatever");
        setVisible(true);
        setPreferredSize(new Dimension(400, 400));
    }
}
One more important thing.

If you are uploading this script to the SDN, and you are using Intellij Form Designer, you will need to make Intellij compile the .form into Java code in your class.

If you don't do this, when you copy over your GUI class file, none of your GUI code will be there, and it will break.

To do this you need to go to:

File -> Settings -> Editor -> GUI Designer

And change the option "Generate GUI into:"'s value to Java source code.

2EWCzFf.png

And then make the project

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

You are using Intellij Form Designer though right?

private JPanel panel;
If that JPanel is not bound to the JPanel in Intelij Form Designer, then when you call:
add(panel);
It will throw a NullPointerException.

Here is a graphical example of what I meant in my previous comment.

Firs you have to provide a name for the top level JPanel in the .form view:

bZq6UVK.png

Then in the bound Java class with the same name you do:

import javax.swing.*;

public class GUI extends JFrame {
    private JPanel mainPanel;

    public GUI(){
        add(mainPanel);
        setTitle("Whatever");
        setVisible(true);
    }
}
This code works perfectly fine.

Don't forget you will also need to set a preferred size on your JFrame otherwise it will be very small:

import javax.swing.*;
import java.awt.*;

public class GUI extends JFrame {
    private JPanel mainPanel;

    public GUI(){
        add(mainPanel);
        setTitle("Whatever");
        setVisible(true);
        setPreferredSize(new Dimension(400, 400));
    }
}
One more important thing.

If you are uploading this script to the SDN, and you are using Intellij Form Designer, you will need to make Intellij compile the .form into Java code in your class.

If you don't do this, when you copy over your GUI class file, none of your GUI code will be there, and it will break.

To do this you need to go to:

File -> Settings -> Editor -> GUI Designer

And change the option "Generate GUI into:"'s value to Java source code.

2EWCzFf.png

And then make the project

 

 

I'll try it out tonight, thanks for the good tutorial :)

Link to comment
Share on other sites

You are using Intellij Form Designer though right?

private JPanel panel;
If that JPanel is not bound to the JPanel in Intelij Form Designer, then when you call:
add(panel);
It will throw a NullPointerException.

Here is a graphical example of what I meant in my previous comment.

Firs you have to provide a name for the top level JPanel in the .form view:

bZq6UVK.png

Then in the bound Java class with the same name you do:

import javax.swing.*;

public class GUI extends JFrame {
    private JPanel mainPanel;

    public GUI(){
        add(mainPanel);
        setTitle("Whatever");
        setVisible(true);
    }
}
This code works perfectly fine.

Don't forget you will also need to set a preferred size on your JFrame otherwise it will be very small:

import javax.swing.*;
import java.awt.*;

public class GUI extends JFrame {
    private JPanel mainPanel;

    public GUI(){
        add(mainPanel);
        setTitle("Whatever");
        setVisible(true);
        setPreferredSize(new Dimension(400, 400));
    }
}
One more important thing.

If you are uploading this script to the SDN, and you are using Intellij Form Designer, you will need to make Intellij compile the .form into Java code in your class.

If you don't do this, when you copy over your GUI class file, none of your GUI code will be there, and it will break.

To do this you need to go to:

File -> Settings -> Editor -> GUI Designer

And change the option "Generate GUI into:"'s value to Java source code.

2EWCzFf.png

And then make the project

 

 

Is there any way to do it after I've created the form? I have my Woodcutter and the GUI setup but forgot to check Generate into Java source code...

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...