Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Action Listener problems!

Status
Not open for further replies.

JProg

Programmer
Apr 4, 2002
88
JP
Hi Everyone,

I have a question concerning the scope of a number of Listener methods. The following code snippet describes what I want to achieve(please see comments in each actionPerformed method). Unfortunately I have know idea of how to make my comments a reality in Java code terms. Any help will be greatly appreciated.

import java.awt.*;
import java.swing.*;
import java.event.*;


public class Untitled1 {
public static void main(String[] args) {
GUIV1 x = new GUIV1();
GUIV2 x = new GUIV2();
}
}

class GUIV1 extends JFrame implements ActionListener{
JButton btn1 = new JButton("Forward");

JLabel lbl1 = new JLabel("Default JFrame");

GUIV1()
{
Container Con = getContentPane();
Con.setLayout(null);
Con.add(btn1);
Con.add(lbl1);

btn1.setBounds(200,200,70,70);
lbl1.setBounds(100,50,100,100);

setSize(450,450);
setLocation(20,20);
setTitle("Default Frame");
setVisible(true);
}

public void actionPerformed(ActionEvent e)
{
/* GUIV1 is setVisible(true) by default, so that this is the first screen the user
sees. The one ActionEvent that is triggered by button 1 on GUIV1 should
result in GUIV1 being made invisible, setVisible(false). Also GUIV2 should
be made visible, so that the user now sees this programs second screen.
*/
}
}

class GUIV2 extends JFrame implements ActionListener{
JButton btn1 = new JButton("Backward");

JLabel lbl2 = new JLabel("User initiated JFrame");

GUIV2()
{
Container Con1 = getContentPane();
Con1.setLayout(null);
Con1.add(btn1);
Con1.add(lbl1);

btn1.setBounds(200,200,70,70);
lbl1.setBounds(100,50,100,100);

setSize(350,350);
setLocation(10,10);
setTitle("User Created Frame");
setVisible(false);
}
public void actionPerformed(ActionEvent e)
{
/* When button 1 of GUIV2 is pressed I would like for GUIV2
to be set invisible, setVisible(false), and GUIV1 should once more be
made visible, setVisible(true).
*/
}
}
 
Below is your code with the required modifications, and comments with each one. This code compiles and runs with the results you stated in your comments.

Now I do not recommend the format of you code, and use null layouts, and the naming conventions used. However for quick testing and learning purposes it is not a big issue.

I hope the below code helps you out.

Rodney

Code:
import java.awt.*;
// changed the import from java.swing to javax.swing
import javax.swing.*;
// change the import from java.event to java.awt.event
import java.awt.event.*;

public class Untitled1
{

    public static void main(String[] args)
    {
        /**
         * changed the name of the GUIV1 from x to v1 to 
         * help in clairity, and
         */
        GUIV1 v1 = new GUIV1();
        /**
         * You want to use show, not setVisable(boolean) 
         * this will get kick off the GUI portion of the 
         * application
         */
        v1.show();

        /**
         * You want this to be a child of GUIV1 so it should
         * be inclosed inside of GUIV1 so that it can control it.
         */
        //GUIV2 x = new GUIV2();
    }
}

/**
 * You should only have one JFrame in most Applications.  
 * It should be the Primary window for your application.  
 * In rare applications that have multiple primary
 * windows, they will have multiple JFrames, one for each.
 */
class GUIV1 extends JFrame implements ActionListener
{
    JButton btn1 = new JButton("Forward");
    JLabel  lbl1 = new JLabel("Default JFrame");
    // Add a class member handle
    GUIV2   v2   = null;

    GUIV1()
    {
        // create the GUIV2 object passing inself as the owner
        v2 = new GUIV2(this);
        Container Con = getContentPane();
        Con.setLayout(null);
        Con.add(btn1);
        Con.add(lbl1);
        // You need to add the action listener to the JComponent
        btn1.addActionListener(this);

        btn1.setBounds(200,200,70,70);
        lbl1.setBounds(100,50,100,100);

        setSize(450,450);
        setLocation(20,20);
        setTitle("Default Frame");
        // Do not need to do this
        //setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
        // You want to hide this Frame and show the Dialog.
        this.hide();
        v2.show();

      /* GUIV1 is setVisible(true) by default, so that    this is the first screen the user
       sees. The one ActionEvent that is triggered by button 1 on GUIV1 should
       result in GUIV1 being made invisible, setVisible(false). Also GUIV2 should
       be made visible, so that the user now sees this programs second screen.
       */
    }
}

/**
 * This class should be a JDialog so that it can be owned by 
 * another JDialog or in this case a JFrame.  All Secondary 
 * windows should be JDialogs, and should have an owner of 
 * either a JFrame or a JDialog.
 */
class GUIV2 extends JDialog implements ActionListener
{
    JButton btn1 = new JButton("Backward");
    JLabel  lbl1 = new JLabel("User initiated JFrame");

    /**
     * You need to implement one of the JDialogs constructors.
     * It is always a good Idea with Dialogs to implement at 
     * least one or more
     * of the JDialog's constructors. 
     */
     
    
    GUIV2(JFrame owner)
    {
        /**
         * You want to super to the JDialog's constructor 
         * to pass it the owner 
         */        
        super(owner);
        Container Con1 = getContentPane();
        Con1.setLayout(null);
        Con1.add(btn1);
        Con1.add(lbl1);
        // You need to add the action listener to the JComponent
        btn1.addActionListener(this);

        btn1.setBounds(200,200,70,70);
        lbl1.setBounds(100,50,100,100);

        setSize(350,350);
        setLocation(10,10);
        setTitle("User Created Frame");
        // Do not need to do this
        //setVisible(false);
    }

    public void actionPerformed(ActionEvent e)
    {
        // You want to hide this Dialog and show the its owner.
        this.hide();
        getOwner().show();

    /* When button 1 of GUIV2 is pressed I would like for GUIV2
     to be set invisible, setVisible(false), and GUIV1 should once more be
     made visible, setVisible(true).
    */
    }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top