Jump to content

JPanel help on script start


Recommended Posts

Hey everyone, first time posting here (new on the scene)

I'm writing up a couple of scripts, and would like to add a JPanel upon starting the script (like some of the Pro scripts) that allows the user to select various options before actually running through the script.

I was reading a similar post, and am able to get the Jpanel displaying correctly. However the script I have does not seem to wait until the user has clicked the 'Start' button on the JPanel UI

I have tried using ChatGPT for suggestions, and it has been suggesting 'CountDownLatches'. Is this what people have been using?

Are there any code examples of one of these JPanles being initialized from the script class that I could compare my code to? - Any help/suggestions would be greatly appreciated 🙂
 

@Override
public boolean onStart(String... strings){
    startTime = System.currentTimeMillis();
    System.out.println("Starting script...");
    latch = new CountDownLatch(1);
    new ScriptSettingsGUI(this, latch);
    try {
        latch.await(); // Wait until latch count reaches zero
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return true;
}

And then this is my JPanel class:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.CountDownLatch;

public class ScriptSettingsGUI extends JFrame {
    private JPanel panel;
    private JLabel label;
    private JComboBox<String> comboBox;
    private JButton button;
    private String selectedLog;
    private CountDownLatch latch;

    public ScriptSettingsGUI(Varrock_West_Fire_Starter bot, CountDownLatch latch) {
        this.latch = latch;
        setTitle("Varrock Fire Starter");
        setSize(300, 100);
        setLocationRelativeTo(null);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        panel = new JPanel();
        panel.setLayout(new FlowLayout());

        label = new JLabel("Select log type: : ");
        panel.add(label);

        String[] logOptions = {"Logs", "Oak logs", "Willow logs", "Yew logs"};
        comboBox = new JComboBox<>(logOptions);
        add(comboBox);
        panel.add(comboBox);

        button = new JButton("Start Burning Logs");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                selectedLog = (String) comboBox.getSelectedItem();
                System.out.println("Start button clicked, selected logs:  " + selectedLog);
                latch.countDown(); // Decrease the latch count
            }
        });

        panel.add(button);
        add(panel);
        setVisible(true);
    }
}
Link to comment
Share on other sites

  • 6 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

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.

×
×
  • Create New...