1

I am trying to open a New Window by pressing the JButton. Now my actionPerformed method for the Jbutton is just closing the window after pressing it. I want it to open a new window as the old window closes. I know, this question is already had been asked, but I tried lot of things, no one seems to work perfectly.

    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.text.*;
    import java.util.Random;

     public class secondTab extends JFrame 
        {
            static JTabbedPane Pane = new JTabbedPane();

            static JPanel Second = new JPanel();
            static JPanel second = new JPanel(); // This Panel is inside the Second Panel

            static JButton guess1 = new JButton();
            static String words[] = new String[9];

            static Random getRandomWord = new Random();

            static int   rr;
            static  JButton Labels[] = new JButton[10];

     public static void main(String args[]) 
        {
            //construct frame
            new secondTab().show();
        }

     public secondTab() 
        {
            // code to build the form
            setTitle("Shopping Cart");

            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     
            getContentPane().setLayout(new GridBagLayout());

            // position tabbed pane
            GridBagConstraints gridConstraints = new GridBagConstraints();
            gridConstraints.gridx = 1;
            gridConstraints.gridy = 1;
            Pane.setForeground(Color.YELLOW);
            Pane.setBackground(Color.MAGENTA);

            getContentPane().add(Pane, gridConstraints);
            getContentPane().setLayout(new GridBagLayout());


         //  Tabs Starts From Here

             Second.setLayout(new GridBagLayout());

             words[1] = "JAVA";
             words[2] = "FLOAT";
             words[3] = "VOID";
             words[4] = "MAIN";
             words[5] = "STATIC";
             words[6] = "FINAL";
             words[7] = "PRIVATE";
             words[8] = "CHAR";
             words[9] = "IF";


             rr = getRandomWord.nextInt(10);

             for(int i = 1; i <=words[rr].length(); i++)
                {
                    Labels[i] = new JButton();
                    gridConstraints.gridx = 0;
                    gridConstraints.gridy = 0;
                    second.add(Labels[i]);
                }  


            gridConstraints.gridx= 0;
            gridConstraints.gridx= 0;
            Second.add(second, gridConstraints);


            guess1.setText("New Word");
            gridConstraints.gridx = 0;
            gridConstraints.gridy = 2;
            Second.add(guess1, gridConstraints);

Here is my button's Action Performed method

            // Action Performed method for the JButton guess1
            guess1.addActionListener(new ActionListener() 
                {
                    public void actionPerformed(ActionEvent e) 
                        {
                            dispose();
                        }
                });



            Pane.addTab("Game ", Second); 

            pack();
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            setBounds((int) (0.5 * (screenSize.width - getWidth())), (int) (0.5 * 
                    (screenSize.height - getHeight())), getWidth(), getHeight());
        }

    }
CrazyPixi
  • 95
  • 3
  • 7
  • Don't use static variables. Use standard Java naming conventions. Variable names should not start with an upper case character. – camickr May 22 '13 at 04:48
  • @camickr: From all this, the only thing that bothered you was variables starting with upper-case letter ? :D – gkalpak May 22 '13 at 04:51

6 Answers6

4

Just add your new panel statement after the dispose(). for example : add

guess1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
                new JFrame("New Window").show();
            }
        });

and one more point - you have created array of 9 element and you are trying to insert at 10 index that throws ArrayIndexOutOfBound exception.

Tej Kiran
  • 2,218
  • 5
  • 21
  • 42
  • Actually, I want my program to run again by pressing button. For example as the window closes, same program will run again automatically... – CrazyPixi May 22 '13 at 04:56
2

You have to remove static keyword for your button and other controls, then you can do this

     JTabbedPane Pane = new JTabbedPane();
     JPanel Second = new JPanel();
     JPanel second = new JPanel(); // This Panel is inside the Second Panel
     JButton guess1 = new JButton();
     String words[] = new String[10];
     Random getRandomWord = new Random();
     int rr;
     JButton Labels[] = new JButton[10];

You have to create new instance of same class and show it

        guess1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
                new secondTab().show();
            }
        });
Tej Kiran
  • 2,218
  • 5
  • 21
  • 42
1

So, rather then closing the current window and creating a new instance of the window, which is just annoying (and a little lazy).

You should provide a means by which you can reset the state of your UI.

You should do this via some method call that resets the internal state and updates the UI.

You could accomplish the same thing by removing the "game" panel, creating a new instance of it and adding it back to the frame.

Blinking frames is a bad idea - IMHO

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • https://www.youtube.com/watch?v=Tx5QsET9IWs .. please watch that video, start the video at 0.46 second, In this video, when the person clicks on the button "Get a new Word" Program closes & automatically starts – CrazyPixi May 22 '13 at 05:15
  • Sooo, you don't actually need to close and open a new window, just refresh it's state? – MadProgrammer May 22 '13 at 05:17
0
Hi,

        Please try like this.. It may help..

guess1.addActionListener(new ActionListener() 
                {
                    public void actionPerformed(ActionEvent e) 
                        {
                              dispose();
                             new AnotherJFrame();
                        }
                }
---------------------------------------------

import javax.swing.JFrame;
import javax.swing.JLabel;

public class AnotherJFrame extends JFrame
{
    public AnotherJFrame()
    {
        super("Another GUI");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        add(new JLabel("Empty JFrame"));
        pack();
        setVisible(true);
    }
}
Sangeetha
  • 37
  • 1
  • 7
  • Actually, I want my program to run again by pressing button. For example as the window closes, same program will run again automatically. May be there is any method, which can refresh the Panel – CrazyPixi May 22 '13 at 04:58
0

I see your question has been answered, so this does not address your problem. I just thought I'd share some things you might want to condider fixing:

  1. Don't use show()/hide(), because they are deprecated. [More info]
    Use setVisible(true/false) respectively. [More info]

  2. You don't have to add a new 'GridBagLayout()' before adding each component into the same container. I.e., you can remove the second getContentPane().setLayout(new GridBagLayout()); [More info]

  3. Just make sure you understand that array-indices in Java (and all programming languages I know od - which isn't that much...but still) are zero-based ! [More info]

  4. In order to find the center of the "usable" screen size Toolkit.getDefaultToolkit().getScreenSize(); isn't enough, since it includes screen-insets (e.g. non-visible margins, taskbars etc). You need to also use Toolkit.getDefaultToolkit().getScreenInsets(GraphicsConfiguration); and substract left and right insets from screen-width as well as top and bottom insets from screen-height.

  5. If all you need is center a JFrame on the screen, you can simply use setLocationRelativeTo(null). [More info]

gkalpak
  • 47,844
  • 8
  • 105
  • 118
-1

see the below link... It may help for you..

Java swing application, close one window and open another when button is clicked

Community
  • 1
  • 1
Sangeetha
  • 37
  • 1
  • 7
  • https://www.youtube.com/watch?v=Tx5QsET9IWs .. please watch that video, start the video at 0.46 second, In this video, when the person clicks on the button "Get a new Word" Program closes & automatically starts – CrazyPixi May 22 '13 at 05:17
  • Thanx... for your help....!!, I know you did lot of searcher, really thankful for your effort.. but problem is solved now...!! – CrazyPixi May 22 '13 at 05:21