-5

EDIT: I delayed the execution of "Core.createBot();" using a ScheduledWorker to resolve the problem.

So I made an IRC Bot using Java and I'm now rewriting him to be controllable through a GUI. If a user issues a command through that, the bot sends a message to the channel saying who issued the command. To know who it was, I'm asking the user to set a username right after the start of the application. When clicking a button, the current GUI closes, the bot starts up and the actual GUI is popping up. However it only shows a white screen, nothing else. No JLabels, no JTextFields, nothing. What am I doing wrong?

This is my main class

package bl4ckscor3.bot.bl4ckb0tGUI.core;

import java.awt.Font;
import java.io.IOException;

import javax.swing.JFrame;

import org.pircbotx.Configuration;
import org.pircbotx.PircBotX;
import org.pircbotx.exception.IrcException;

import bl4ckscor3.bot.bl4ckb0tGUI.gui.Gui;
import bl4ckscor3.bot.bl4ckb0tGUI.gui.NameGui;

    public class Core
    {
        public static PircBotX bot;
        public static Gui gui;
        public static String name;
        public static NameGui nameGui;

        public static void main(String args[]) throws IOException, IrcException
        {
            setupNameGui();
        }

        private static void setupNameGui()
        {
            nameGui = new NameGui();

            nameGui.setTitle("Username selection");
            nameGui.setFont(new Font("Arial", 0, 14));
            nameGui.setSize(300, 150);
            nameGui.setLocationRelativeTo(null);
            nameGui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            nameGui.setResizable(false);
            nameGui.setVisible(true);
        }

        public static void setupGui()
        {
            gui = new Gui();

            gui.setTitle("bl4ckb0t");
            gui.setFont(new Font("Arial", 0, 14));
            gui.setSize(800, 800);
            gui.setLocationRelativeTo(null);
            gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            gui.setResizable(false);
            gui.setVisible(true);
        }

        public static void createBot()
        {
            Configuration config = new Configuration.Builder()  
            .setName("bl4ckb0t")
            .setVersion("1.0")
            .setServerHostname("irc.esper.net")
            .setServerPort(6667)
            .setNickservPassword("xxx")
            .setLogin("bl4ckb0t")
            .setAutoNickChange(true)
            .addListener(new BotListener())
            .setMessageDelay(500)
            .buildConfiguration();
            bot = new PircBotX(config);

            try
            {
                bot.startBot();
            }
            catch(IOException e){}
            catch(IrcException e){}
        }
    }

And this is the class where the problem seems to appear:

package bl4ckscor3.bot.bl4ckb0tGUI.gui;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingWorker;

import bl4ckscor3.bot.bl4ckb0tGUI.core.Core;

public class NameGui extends JFrame
{
    private JLabel label = new JLabel();
    private JTextField text = new JTextField();
    private JButton buttonStart = new JButton();
    private JButton buttonStop = new JButton();
    private String textText = "Please insert your username below.";
    private Container cp = getContentPane();

    public NameGui()
    {
        cp.setLayout(null);
        label.setBounds(40, 10, textText.length() * 6, 20);
        label.setText(textText);
        text.setBounds(42, 40, 200, 20);
        buttonStart.setText("Let's start!");
        buttonStart.setBounds(10, 80, 100, 20);
        buttonStart.addActionListener(new ButtonListener());
        buttonStop.setText("I changed my mind.");
        buttonStop.setBounds(130, 80, 145 , 20);
        buttonStop.addActionListener(new ButtonListener());
        cp.add(label);
        cp.add(text);
        cp.add(buttonStart);
        cp.add(buttonStop);
    }

    private class ButtonListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent event)
        {
            switch(event.getActionCommand())
            {
                case "Let's start!":
                    if(Core.name != null)
                        Core.name = text.getText();
                    else
                        Core.name = "Anonymous";

                    Core.nameGui.dispose();
                    Core.setupGui();
                    java.awt.EventQueue.invokeLater(new Runnable() 
                    {
                        @Override
                        public void run() 
                        {
                            Core.createBot();
                        }
                    });
                    break;
                case "I changed my mind.":
                    System.exit(0);
                    break;
                default:
                    System.out.println("Something went wrong: " + event.getActionCommand());
            }
        }
    }
}
bl4ckscor3
  • 11
  • 1
  • 4
  • 2
    Welcome to Stack Overflow, please take the [Tour](http://stackoverflow.com/tour). Please read [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask). Please **edit** your question to include a a minimal example. See [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Do **not** use an offsite link for your code in case the target site is unreachable or goes permanently offline. – DavidPostill Aug 28 '14 at 20:33

1 Answers1

2

Core.createBot();

The above code executes on the Event Dispatch Thread (EDT). I don't know exactly what the Bot does but I'm guessing it sits there waiting for input which means it is blocking the EDT and preventing the GUI from repainting itself.

I would guess the "bot" needs to run on a separate Thread so it doesn't block the EDT. Read the section from the Swing tutorial on Concurrency for more information.

camickr
  • 321,443
  • 19
  • 166
  • 288