Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Java Swing GUI not showing up properly.


    dreamwiver

    Recommended Posts

    Hello,

    First of all I apologize if this topic has appeared before, I have been looking for a while but I cannot find anything related.

    I'm trying to add a Java Swing form into my script, which consists in just a textfield and a button. This is just for learning purposes, I know it doesn't have any interesting logic.

    The GUI code:

    package Woodcutter;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class GUI extends JFrame{
        private JPanel principal;
        private JTextField txtName;
        private JButton button1;
    
        public GUI() {
            button1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(button1, txtName.getText()+"Hello");
                }
            });
        }
    
        public static void main(String[] args) {
            GUI g = new GUI();
            g.setContentPane(g.principal);
            g.setSize(300, 400);
            g.pack();
            g.setVisible(true);
        }
    }

    My onStart() method:

        @Override
        public void onStart() {
            log("hi");
    
            SwingUtilities.invokeLater(() -> {
                GUI gui = new GUI();
                gui.setVisible(true);
            });
    
        }

    If I preview the Form in the Form designer, it displays properly, however, when I run the script on the Dreambot client, the form stays in the background without any information printed on it.

    Any suggestion?

    Thank you so much

     

     

    Edited by dreamwiver
    Link to comment
    Share on other sites

    Remove the main method, put that in the constructor method. Also, no need to call a new GUI within itself. You could also move .setVisible to its own method (maybe "show") so you can call it when you want.

    SwingUtilities.invokeLater(() -> {
                GUI gui = new GUI();
                gui.show();
            });

    I also don't extend JFrame, I just declare my own variable for it within my GUI classes.

    frame = new JFrame("Select Task");
    frame.setResizable(false);
    frame.setContentPane(jPane);
    frame.setLocationByPlatform(true);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // Add this so you don't close out DB if you close your GUI

     

    Link to comment
    Share on other sites

    Hello @holic, thank you for the fast response.

    I'm trying to change it with your suggestion:

    public class GUI {
        private JPanel principal;
        private JTextField txtName;
        private JButton button1;
    
        public GUI() {
            button1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(button1, txtName.getText()+"Hello");
                }
            });
            JFrame g = new JFrame();
            g.setContentPane(principal);
            g.setSize(300, 400);
            g.pack();
            g.setLocationByPlatform(true);
            g.setResizable(false);
            g.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // Add this so you don't close out DB if you close your GUIt
            g.setVisible(true);
        }
    }

     

    I removed the main method and put everything in the constructor. The IDE basically complains there's no main method, which makes sense I think (I haven't touched Java for a long time).

    If I add a blank main method, when I run the script, it doesn't invoke any Form.

    Any ideas? Thank you so much

    Link to comment
    Share on other sites

    1 hour ago, dreamwiver said:

    Hello @holic, thank you for the fast response.

    I'm trying to change it with your suggestion:

    public class GUI {
        private JPanel principal;
        private JTextField txtName;
        private JButton button1;
    
        public GUI() {
            button1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(button1, txtName.getText()+"Hello");
                }
            });
            JFrame g = new JFrame();
            g.setContentPane(principal);
            g.setSize(300, 400);
            g.pack();
            g.setLocationByPlatform(true);
            g.setResizable(false);
            g.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // Add this so you don't close out DB if you close your GUIt
            g.setVisible(true);
        }
    }

     

    I removed the main method and put everything in the constructor. The IDE basically complains there's no main method, which makes sense I think (I haven't touched Java for a long time).

    If I add a blank main method, when I run the script, it doesn't invoke any Form.

    Any ideas? Thank you so much

    Hey mate.

    Looks like you haven't initialized the textfield and the button. This should get you up and running.

    public GUI() {
        JFrame g = new JFrame();
        g.setSize(300, 400);
        g.setLocationByPlatform(true);
        g.setResizable(false);
        g.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // Add this so you don't close out DB if you close your GUIt
    
        principal = new JPanel();
        txtName = new JTextField("dreamwiver");
        button1 = new JButton("Click me");
        button1.addActionListener(l -> JOptionPane.showMessageDialog(button1, txtName.getText() + " Hello"));
        principal.add(txtName);
        principal.add(button1);
    
        g.getContentPane().setLayout(new BorderLayout());
        g.getContentPane().add(principal, BorderLayout.NORTH);
        g.pack();
        g.setVisible(true);
    }

     

    Link to comment
    Share on other sites

    11 hours ago, Fluent said:

    Hey mate.

    Looks like you haven't initialized the textfield and the button. This should get you up and running.

    public GUI() {
        JFrame g = new JFrame();
        g.setSize(300, 400);
        g.setLocationByPlatform(true);
        g.setResizable(false);
        g.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // Add this so you don't close out DB if you close your GUIt
    
        principal = new JPanel();
        txtName = new JTextField("dreamwiver");
        button1 = new JButton("Click me");
        button1.addActionListener(l -> JOptionPane.showMessageDialog(button1, txtName.getText() + " Hello"));
        principal.add(txtName);
        principal.add(button1);
    
        g.getContentPane().setLayout(new BorderLayout());
        g.getContentPane().add(principal, BorderLayout.NORTH);
        g.pack();
        g.setVisible(true);
    }

     

    Works like a charm, thank you both!

     

    Here it's the working code

     

    GUI.java

    package Woodcutter;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class GUI extends JFrame{
        private JPanel principal;
        private JTextField txtName;
        private JButton button1;
    
        public GUI() {
            JFrame g = new JFrame();
            g.setSize(300, 400);
            g.setLocationByPlatform(true);
            g.setResizable(false);
            g.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // Add this so you don't close out DB if you close your GUIt
    
            principal = new JPanel();
            txtName = new JTextField("dreamwiver");
            button1 = new JButton("Click me");
            button1.addActionListener(l -> JOptionPane.showMessageDialog(button1, txtName.getText() + " Hello"));
            principal.add(txtName);
            principal.add(button1);
    
            g.getContentPane().setLayout(new BorderLayout());
            g.getContentPane().add(principal, BorderLayout.NORTH);
            g.pack();
            g.setVisible(true);
        }
    
        public static void main(String[] args) {
    
        }
    }

     

    onStart:

        @Override
        public void onStart() {
            SwingUtilities.invokeLater(() -> {
                new GUI();
            });
        }

     

    Edited by dreamwiver
    Link to comment
    Share on other sites

    18 hours ago, dreamwiver said:

    Works like a charm, thank you both!

     

    Here it's the working code

     

    GUI.java

    package Woodcutter;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class GUI extends JFrame{
        private JPanel principal;
        private JTextField txtName;
        private JButton button1;
    
        public GUI() {
            JFrame g = new JFrame();
            g.setSize(300, 400);
            g.setLocationByPlatform(true);
            g.setResizable(false);
            g.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // Add this so you don't close out DB if you close your GUIt
    
            principal = new JPanel();
            txtName = new JTextField("dreamwiver");
            button1 = new JButton("Click me");
            button1.addActionListener(l -> JOptionPane.showMessageDialog(button1, txtName.getText() + " Hello"));
            principal.add(txtName);
            principal.add(button1);
    
            g.getContentPane().setLayout(new BorderLayout());
            g.getContentPane().add(principal, BorderLayout.NORTH);
            g.pack();
            g.setVisible(true);
        }
    
        public static void main(String[] args) {
    
        }
    }

     

    onStart:

        @Override
        public void onStart() {
            SwingUtilities.invokeLater(() -> {
                new GUI();
            });
        }

     

    Excellent 👍 interesting that your IDE wants a main method though, it shouldn't. What env are you using?

    Link to comment
    Share on other sites

    16 hours ago, holic said:

    Excellent 👍 interesting that your IDE wants a main method though, it shouldn't. What env are you using?

    I have a feeling he is using some sort of form builder and then manually editing the code it produces

    Link to comment
    Share on other sites

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now
    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.