Crouton Posted April 3, 2016 Share Posted April 3, 2016 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. Quote Link to comment Share on other sites More sharing options...
justanotherkid Posted April 7, 2016 Share Posted April 7, 2016 hey could u show us how to add a text box in the GUI? like for an AIO combat script it would need a box to type the names of the monsters. Thanks Quote Link to comment Share on other sites More sharing options...
Explv Posted April 7, 2016 Author Share Posted April 7, 2016 hey could u show us how to add a text box in the GUI? like for an AIO combat script it would need a box to type the names of the monsters. Thanks http://www.tutorialspoint.com/swing/swing_jtextfield.htm Quote Link to comment Share on other sites More sharing options...
GaetanoH Posted May 11, 2016 Share Posted May 11, 2016 (edited) 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 Edited May 11, 2016 by GaetanoH Quote Link to comment Share on other sites More sharing options...
Explv Posted May 11, 2016 Author Share Posted May 11, 2016 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(); } } } 1 Quote Link to comment Share on other sites More sharing options...
GaetanoH Posted May 11, 2016 Share Posted May 11, 2016 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 Quote Link to comment Share on other sites More sharing options...
Krave Posted May 11, 2016 Share Posted May 11, 2016 Great tutorial. Will come in handy. Quote Link to comment Share on other sites More sharing options...
GaetanoH Posted May 12, 2016 Share Posted May 12, 2016 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. Quote Link to comment Share on other sites More sharing options...
GaetanoH Posted May 12, 2016 Share Posted May 12, 2016 (edited) 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 May 12, 2016 by GaetanoH Quote Link to comment Share on other sites More sharing options...
Explv Posted May 12, 2016 Author Share Posted May 12, 2016 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? Quote Link to comment Share on other sites More sharing options...
GaetanoH Posted May 12, 2016 Share Posted May 12, 2016 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. Quote Link to comment Share on other sites More sharing options...
Explv Posted May 12, 2016 Author Share Posted May 12, 2016 (edited) 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: 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. And then make the project Edited May 12, 2016 by Explv 1 Quote Link to comment Share on other sites More sharing options...
GaetanoH Posted May 12, 2016 Share Posted May 12, 2016 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: 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. And then make the project I'll try it out tonight, thanks for the good tutorial Quote Link to comment Share on other sites More sharing options...
GaetanoH Posted May 18, 2016 Share Posted May 18, 2016 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: 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. 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... Quote Link to comment Share on other sites More sharing options...
Explv Posted May 18, 2016 Author Share Posted May 18, 2016 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... Yes, to generate the source code just 'make' the project. Hit Ctrl F9 or Go to Build -> Make Project 1 Quote Link to comment Share on other sites More sharing options...