dreamwiver 15 Posted December 23, 2022 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
holic 238 Posted December 23, 2022 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
dreamwiver 15 Author Posted December 24, 2022 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
Fluent 5 Posted December 25, 2022 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); }
dreamwiver 15 Author Posted December 25, 2022 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(); }); }
holic 238 Posted December 26, 2022 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?
Fluent 5 Posted December 26, 2022 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
Recommended Posts
Archived
This topic is now archived and is closed to further replies.