Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: JButton open new window

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JButton open new window

    Been sitting here at my computer for about 13 hours and I think my eyes are bleeding.
    I found a little gui editor I love called GuiGenie.
    It works perfect for creating the window with the buttons and all that good stuff.
    The problem is i want to click a button in my first menu and have it open my other menu i made.
    I just starting programming 4 weeks ago so I'm a complete noob.
    I have a feeling its messing up because of the main methods but I have no idea and 13 hours of sitting here trying millions of things is making me go crazy : )
    here is what i got so far
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
     
    public class MyPanel extends JPanel {
        private JTextField How;
        private JLabel jcomp2;
        private JLabel jcomp3;
        private JButton jcomp4;
     
        public MyPanel() {
            //construct components
            How = new JTextField (1);
            jcomp2 = new JLabel ("How long were you parked?");
            jcomp3 = new JLabel ("Minutes");
            jcomp4 = new JButton ("openNewWindow");
     
            //adjust size and set layout
            setPreferredSize (new Dimension (315, 85));
            setLayout (null);
     
            //add components
            add (How);
            add (jcomp2);
            add (jcomp3);
            add (jcomp4);
     
            //set component bounds (only needed by Absolute Positioning)
            How.setBounds (245, 50, 60, 25);
            jcomp2.setBounds (35, 30, 185, 50);
            jcomp3.setBounds (250, 30, 60, 20);
            jcomp4.setBounds (0, 0, 315, 25);
     
    		   jcomp4.addActionListener( new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
     
                }
            });
        }
     
     
        public static void main (String[] args) {
            JFrame frame = new JFrame ("MyPanel");
            frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add (new MyPanel());
            frame.pack();
            frame.setVisible (true);
        }
    }

    When the button is pressed, I want it to open this new window

    //Generated by GuiGenie - Copyright (c) 2004 Mario Awad.
    //Home Page http://guigenie.cjb.net - Check often for new versions!
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
     
    public class MyPanel2 extends JPanel {
        private JButton jcomp1;
        private JButton jcomp2;
        private JButton jcomp3;
        private JTextField jcomp4;
     
        public MyPanel2() {
            //construct components
            jcomp1 = new JButton ("test1");
            jcomp2 = new JButton ("test2");
            jcomp3 = new JButton ("test3");
            jcomp4 = new JTextField (5);
     
            //adjust size and set layout
            setPreferredSize (new Dimension (395, 156));
            setLayout (null);
     
            //add components
            add (jcomp1);
            add (jcomp2);
            add (jcomp3);
            add (jcomp4);
     
            //set component bounds (only needed by Absolute Positioning)
            jcomp1.setBounds (20, 45, 100, 25);
            jcomp2.setBounds (135, 60, 100, 25);
            jcomp3.setBounds (260, 35, 100, 25);
            jcomp4.setBounds (105, 115, 100, 25);
        }
     
     
        public static void main (String[] args) {
            JFrame frame = new JFrame ("MyPanel");
            frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add (new MyPanel2());
            frame.pack();
            frame.setVisible (true);
        }
    }

    If anyone could help I would appreciate it greatly!!
    I have a lot of respect for you pros out there because if you are a pro at this, you are probably smarter than 99.9% of the world.
    This stuff hurts my brain.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JButton open new window

    i want to click a button in my first menu and have it open my other menu i made.
    For the program to execute a method in response to a click on a button, the button needs to have a listener added to it. So first you should work on getting that to work.

    Where do you want the MyPanel2 class to be viewed when it is created? In a new JFrame or where? It has to be added to a container that displays on the screen. A JPanel does not display by itself.

    BTW The varaible names don't reflect the purpose and usage of the components and makes the code harder to read: jcompX ??? Why not give them meaningful names: testBtn1, openBtn etc
    Last edited by Norm; June 30th, 2012 at 06:52 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JButton open new window

    1. for the class with the JButton put "implements MouseListener" after it says "extends JPanel" and import the mouse listener class
    2. in the constructor write, this.addMouseListener(this); and for your button write myButton.addMouseListener(this); replacing myButton with whatever yours is called.
    3. then in the actionPerformedMouseClicked method that you must have because you're implementing MouseListener, have a check for the button. to see if its the one you want to make open the other menu, would look something like this, "if(e.getComponent().equals(myButton)) //open menu;
    4. Not sure if you want a whole new window or just to replace whats in your JFrame, if you want a whole new window in the if statement mentioned above instantiate the class that extends JFrame holds your other menu, it would look something like the first one. If you want the Panel to be switched just change what panel is displayed in the original JFrame

Similar Threads

  1. how to open a popup window of specific size in asp
    By btebo in forum Other Programming Languages
    Replies: 5
    Last Post: May 26th, 2012, 06:49 PM
  2. Im trying to open a new GUI window by clicking on a button
    By amzwans in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 10th, 2011, 11:33 PM
  3. [SOLVED] How to stop the same window from having more than one instance of it open at a time.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 9th, 2011, 05:47 PM
  4. How do i have a jButton Open a URL?
    By dragon40226 in forum Java Theory & Questions
    Replies: 2
    Last Post: March 7th, 2011, 04:21 AM
  5. How to press button to open another window
    By vkokaram in forum AWT / Java Swing
    Replies: 1
    Last Post: July 18th, 2010, 03:51 PM