Results 1 to 6 of 6
  1. #1
    Coiby is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default user enable MenuItem

    Hi, guys!

    I'm programming to solve a 3X3 Rubik's Cube using Evolutionary Algorithm. Now there's another problem with GUI.

    I want to enable MenuItem mi1_3 (back) after MenuItem mi1_1(initialise) is clicked.

    Thanks!

    The structure of my program:
    Java Code:
    public class test extends Frame implements ActionListener {
      public static void main(String[] args)  {new test();}
      test()
      {
    	  super("Rubik's Cube");
              AnimCube obj = new AnimCube();
    	  MenuBar mb;
    	  Menu m1;
    	  MenuItem mi1_1,mi1_2,mi1_3;
    	  addWindowListener(new WindowAdapter() 
          {public void windowClosing(
                  WindowEvent e){System.exit(0);}});
    	  //Build the menu bar
    	  mb = new MenuBar();
    	  setMenuBar(mb);
    	  //Build menus in the menu bar
    	  m1 = new Menu("Menu 1");
    	  mb.add(m1);
    	  mi1_1 = new MenuItem("initialise");
    	  m1.add(mi1_1);
    	  mi1_2 = new MenuItem("solve");
    	  m1.add(mi1_2);
    	  mi1_3 = new MenuItem("back");
    	  m1.add(mi1_3);
              mi1_1.setActionCommand("init");
    	  mi1_2.setActionCommand("solve");
    	  m1.add(mi1_3);
    	  mi1_3.setActionCommand("back");
    	  mi1_1.addActionListener(obj);
    	  mi1_2.addActionListener(obj);
    	  mi1_3.addActionListener(obj);
    	  setSize(600,500);
    	  add("Center",new AnimCube());
    	  setVisible(true);
      }
    /**
    * This class deals with solving a scrambled Cube, 3d-projections ..
    */
      class AnimCube extends Panel
           implements Runnable, MouseListener, MouseMotionListener {
           public void paint(Graphics g) {
            .....
           }
            public void mouseExited(MouseEvent e) {
            .....
            }
            public void actionPerformed(ActionEvent e) {
    	// TODO Auto-generated method stub
    	if(e.getActionCommand()=="init"){
    	   c = Random_Run_Cube_3();
    	   repaint();
    	}
    	else if(e.getActionCommand()=="back"){
    		clear();
    	        repaint();
    	}
    	else if(e.getActionCommand()=="solve") {
    	   Solving_Cube_Final_3(c);
    	   sol_ena = false;
    	}
    	
    }

  2. #2
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    First, disable m1_3 right after you init it in test(), and then in the actionPerformed() branch for m1_1, call m1_3.setEnabled(true).

  3. #3
    Coiby is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by NRitH View Post
    First, disable m1_3 right after you init it in test(), and then in the actionPerformed() branch for m1_1, call m1_3.setEnabled(true).
    But how can you get the instance of the class for m1_3 in the actionPerformed() branch?

  4. #4
    Dinesh_rockz is offline Member
    Join Date
    Jan 2011
    Location
    India
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by Coiby View Post
    But how can you get the instance of the class for m1_3 in the actionPerformed() branch?
    change into global declaration area...


    Java Code:
    import java.awt.*;import java.awt.event.*;
    public class test extends Frame {
     MenuItem mi1_1,mi1_2,mi1_3;
      public static void main(String[] args)  {new test();}
      test()
      {
      super("Rubik's Cube");
              AnimCube obj = new AnimCube();
    	  MenuBar mb;
    	  Menu m1;
    	  addWindowListener(new WindowAdapter() 
          {public void windowClosing(
                  WindowEvent e){System.exit(0);}});
    	  //Build the menu bar
    	  mb = new MenuBar();
    	  setMenuBar(mb);
    	  //Build menus in the menu bar
    	  m1 = new Menu("Menu 1");
    	  mb.add(m1);
    	  mi1_1 = new MenuItem("initialise");
    	  m1.add(mi1_1);
    	  mi1_2 = new MenuItem("solve");
    	  m1.add(mi1_2);
    	  mi1_3 = new MenuItem("back");
              mi1_3.setEnabled(false);
    	  m1.add(mi1_3);
              mi1_1.setActionCommand("init");
    	  mi1_2.setActionCommand("solve");
    	  m1.add(mi1_3);
    	  mi1_3.setActionCommand("back");
    	  mi1_1.addActionListener(obj);
    	  mi1_2.addActionListener(obj);
    	  mi1_3.addActionListener(obj);
    	  setSize(600,500);
    	  add("Center",new AnimCube());
    	  setVisible(true);
      }	
    /**
    * This class deals with solving a scrambled Cube, 3d-projections ..
    */
      class AnimCube extends Panel
           implements  ActionListener {
           public void paint(Graphics g) {
           // .....
           }
            public void mouseExited(MouseEvent e) {
            //.....
            }
          public void actionPerformed(ActionEvent e) {
    	// TODO Auto-generated method stub
    	if(e.getActionCommand()=="init"){
    System.out.println("in");
     mi1_3.setEnabled(true);
    
    	  // c = Random_Run_Cube_3();
    	   repaint();
    	}
    	else if(e.getActionCommand()=="back"){
    		//clear();
    System.out.println("ba");
    	        repaint();
    	}
    	else if(e.getActionCommand()=="solve") {
    System.out.println("ss");
    	   //Solving_Cube_Final_3(c);
    	   //sol_ena = false;
    	}
    }
      }
    }
    please try refer Java API Docs
    Last edited by Dinesh_rockz; 02-26-2011 at 04:14 AM.

  5. #5
    Coiby is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Dinesh_rockz View Post
    change into global declaration area...


    Java Code:
    import java.awt.*;import java.awt.event.*;
    public class test extends Frame {
     MenuItem mi1_1,mi1_2,mi1_3;
      public static void main(String[] args)  {new test();}
      test()
      {
      super("Rubik's Cube");
              AnimCube obj = new AnimCube();
    	  MenuBar mb;
    	  Menu m1;
    	  addWindowListener(new WindowAdapter() 
          {public void windowClosing(
                  WindowEvent e){System.exit(0);}});
    	  //Build the menu bar
    	  mb = new MenuBar();
    	  setMenuBar(mb);
    	  //Build menus in the menu bar
    	  m1 = new Menu("Menu 1");
    	  mb.add(m1);
    	  mi1_1 = new MenuItem("initialise");
    	  m1.add(mi1_1);
    	  mi1_2 = new MenuItem("solve");
    	  m1.add(mi1_2);
    	  mi1_3 = new MenuItem("back");
              mi1_3.setEnabled(false);
    	  m1.add(mi1_3);
              mi1_1.setActionCommand("init");
    	  mi1_2.setActionCommand("solve");
    	  m1.add(mi1_3);
    	  mi1_3.setActionCommand("back");
    	  mi1_1.addActionListener(obj);
    	  mi1_2.addActionListener(obj);
    	  mi1_3.addActionListener(obj);
    	  setSize(600,500);
    	  add("Center",new AnimCube());
    	  setVisible(true);
      }	
    /**
    * This class deals with solving a scrambled Cube, 3d-projections ..
    */
      class AnimCube extends Panel
           implements  ActionListener {
           public void paint(Graphics g) {
           // .....
           }
            public void mouseExited(MouseEvent e) {
            //.....
            }
          public void actionPerformed(ActionEvent e) {
    	// TODO Auto-generated method stub
    	if(e.getActionCommand()=="init"){
    System.out.println("in");
     mi1_3.setEnabled(true);
    
    	  // c = Random_Run_Cube_3();
    	   repaint();
    	}
    	else if(e.getActionCommand()=="back"){
    		//clear();
    System.out.println("ba");
    	        repaint();
    	}
    	else if(e.getActionCommand()=="solve") {
    System.out.println("ss");
    	   //Solving_Cube_Final_3(c);
    	   //sol_ena = false;
    	}
    }
      }
    }
    please try refer Java API Docs
    I've find the way:) Just pass MenuItem mi1_2 as the parameter for the AnimCube class, then AnimCUbe can get the instant of MenuItem mi1_2.

    Thanks anyway!

  6. #6
    Coiby is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Dinesh_rockz View Post
    change into global declaration area...


    Java Code:
    import java.awt.*;import java.awt.event.*;
    public class test extends Frame {
     MenuItem mi1_1,mi1_2,mi1_3;
      public static void main(String[] args)  {new test();}
      test()
      {
      super("Rubik's Cube");
              AnimCube obj = new AnimCube();
    	  MenuBar mb;
    	  Menu m1;
    	  addWindowListener(new WindowAdapter() 
          {public void windowClosing(
                  WindowEvent e){System.exit(0);}});
    	  //Build the menu bar
    	  mb = new MenuBar();
    	  setMenuBar(mb);
    	  //Build menus in the menu bar
    	  m1 = new Menu("Menu 1");
    	  mb.add(m1);
    	  mi1_1 = new MenuItem("initialise");
    	  m1.add(mi1_1);
    	  mi1_2 = new MenuItem("solve");
    	  m1.add(mi1_2);
    	  mi1_3 = new MenuItem("back");
              mi1_3.setEnabled(false);
    	  m1.add(mi1_3);
              mi1_1.setActionCommand("init");
    	  mi1_2.setActionCommand("solve");
    	  m1.add(mi1_3);
    	  mi1_3.setActionCommand("back");
    	  mi1_1.addActionListener(obj);
    	  mi1_2.addActionListener(obj);
    	  mi1_3.addActionListener(obj);
    	  setSize(600,500);
    	  add("Center",new AnimCube());
    	  setVisible(true);
      }	
    /**
    * This class deals with solving a scrambled Cube, 3d-projections ..
    */
      class AnimCube extends Panel
           implements  ActionListener {
           public void paint(Graphics g) {
           // .....
           }
            public void mouseExited(MouseEvent e) {
            //.....
            }
          public void actionPerformed(ActionEvent e) {
    	// TODO Auto-generated method stub
    	if(e.getActionCommand()=="init"){
    System.out.println("in");
     mi1_3.setEnabled(true);
    
    	  // c = Random_Run_Cube_3();
    	   repaint();
    	}
    	else if(e.getActionCommand()=="back"){
    		//clear();
    System.out.println("ba");
    	        repaint();
    	}
    	else if(e.getActionCommand()=="solve") {
    System.out.println("ss");
    	   //Solving_Cube_Final_3(c);
    	   //sol_ena = false;
    	}
    }
      }
    }
    please try refer Java API Docs
    Sorry I just don't notice this reply. It's a good way to take AnimCube class as inner class for Rubik. Thanks!

Similar Threads

  1. Replies: 2
    Last Post: 02-21-2011, 11:25 AM
  2. textfield on a menuitem
    By cotarelo in forum AWT / Swing
    Replies: 1
    Last Post: 06-29-2010, 04:11 PM
  3. empty menuItem in the menu
    By anix in forum New To Java
    Replies: 8
    Last Post: 06-08-2010, 06:59 PM
  4. various actions in menuitem of popupmenu
    By Peggy in forum Java Applets
    Replies: 0
    Last Post: 12-11-2008, 11:50 AM
  5. MenuItem
    By Sarinam in forum New To Java
    Replies: 1
    Last Post: 07-15-2008, 10:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •