0

For some awkward reason whenever I set my desired frame to Visible(true) inside of my actionPerformed() it prints out the frame, with the title but no components and no colours that I have previous selected.

However, when setting the visibility to true in the top of my current frame(Without having to press the button) it working fine.

This is the frame I'm using:

enter image description here

But this is being printed once I set visibility to true inside the actionPerformed:

enter image description here

I have already got another frame(The main client frame) being set to true inside the actionPerformed() and it works fine. This is the first time this issue happened to me so I'm really not sure what's causing it.

I also assumed that it may be due to the fact that during the actionPerformed the client has to connect to the particular server details but then again, the Frame object is being instantiated as soon as the first frame is instantiated so it should be just fine..

Here is my actionPerformed(), sorry for messy code:

JButton connectButton = new JButton("Connect");
    connectButton.setBackground(SystemColor.activeCaption);
    connectButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            //If any of the first three fields are empty, turn them red
            if(serverField.getText().equalsIgnoreCase("")){
                lblSever.setForeground(new Color(240, 128, 128));
            }
            if(channelField.getText().equalsIgnoreCase("")){
                lblChannel.setForeground(new Color(240, 128, 128));
            }
            if(nicknameField.getText().equalsIgnoreCase("")){
                lblNickname.setForeground(new Color(240, 128, 128));
            }

            //Set Waiting Connection frame to true
            wc.setVisible(true);

            //None of the first three fields are empty
            if(!serverField.getText().equalsIgnoreCase("")
                    && !channelField.getText().equalsIgnoreCase("")
                    && !nicknameField.getText().equalsIgnoreCase("")){

                //Set current frame visibility to false
                setVisible(false);

                //Set connection details
                Main.bot.setServer(serverField.getText());
                Main.bot.setChannel(channelField.getText());
                Main.bot.setNickname(nicknameField.getText());
                Main.bot.setPassword(formattedTextField.getText());

                //Connect to server and channel
                try{
                    Main.bot.connect(serverField.getText());
                    Main.bot.changeNick(nicknameField.getText());
                    Main.bot.joinChannel(channelField.getText());
                }catch(Exception e){ //Catch all errors, if invalid server is thrown, close Waiting Connection frame
                    InvalidServer error = new InvalidServer();
                    e.printStackTrace();
                    wc.setVisible(false);
                    wc.dispose();
                    error.setVisible(true);
                }

                setVisible(false);

                //Custom Timer class
                Timer runTime = new Timer();

                while(runTime.getTimeElapsed() < 5000){
                    //To-Do
                }
                wc.setVisible(false);

                //Instantiate next frame
                MainFrame mainFrame = new MainFrame();
                mainFrame.setVisible(true);

                //Kill current frame
                dispose();
            }
        }
        });
Juxhin
  • 5,068
  • 8
  • 29
  • 55

1 Answers1

2

Some Points:

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • Hey Braj, seen you around before haha. Thanks for commenting first of all. I shall try Swing Timer – Juxhin Jul 05 '14 at 11:17
  • Call `setVisible()` inside the `SwingUtilities.invokeLater()` or `EventQueue.invokeLater()`. – Braj Jul 05 '14 at 11:19
  • So I have, I have my first frame which asks the user for the connection details. Upon pressing connect I simply want to set that current frame to visible, I'm checking out Swing timer but I'm not sure if there's a getTimeElapsed() method or something similar to that – Juxhin Jul 05 '14 at 11:21
  • Please follow the link and take your time to learn more about it. read last option as well. – Braj Jul 05 '14 at 11:22