-1

How to move to various JFrame with handling mousepressed event along action command and get action command. Java swing code:

//this code belongs to home jframe class

mntmPublicSector= new JMenuItem("Public Sector");
    mntmPublicSector.setActionCommand("public");
    mntmPublicSector.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent arg0) { int select=JOptionPane.showConfirmDialog(null,"if already user login","not user Register now",JOptionPane.YES_NO_OPTION);
            if(select==JOptionPane.YES_OPTION)
            {
                new Login_Page().setVisible(true);
                dispose();
            }
            else
            {
                new Student_Register().setVisible(true);
                dispose();
            } }});

mntmStateSector = new JMenuItem("State Sector");
    mntmStateSector.setActionCommand("state");
    mntmStateSector.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent arg0) { int select=JOptionPane.showConfirmDialog(null,"if already user login","not user Register now",JOptionPane.YES_NO_OPTION);
            if(select==JOptionPane.YES_OPTION)
            {
                new Login_Page().setVisible(true);
                dispose();
            }
            else
            {
                new Student_Register().setVisible(true);
                dispose();
            } }});



mntmApptitude = new JMenuItem("Apptitude");
     mntmApptitude.setActionCommand("app");
    mntmApptitude.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent arg0) { int select=JOptionPane.showConfirmDialog(null,"if already user login","not user Register now",JOptionPane.YES_NO_OPTION);
            if(select==JOptionPane.YES_OPTION)
            {
                new Login_Page().setVisible(true);
                dispose();
            }
            else
            {
                new Student_Register().setVisible(true);
                dispose();
            } }});



/*if i click with if i login,it needs to take to respective jframe [--App_Exam_Register_Panel()--or --State_exam_reg()-- or -- Public_exam_Reg()-- ] */

//this code belongs to login jframe class    
JButton btnLogin = new JButton(iim);
Home hm=new Home();
    btnLogin.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {             
            String st=hm.mntmPublicSector.getActionCommand();
            String st1 =hm.mntmStateSector.getActionCommand();
            String aps=hm.mntmApptitude.getActionCommand();
            //String apss=hm.mntmApptitude
            if(st.equals("public"))
            {
                new Public_exam_Reg().setVisible(true);//jframe class
                dispose();
            }     //switch case
            else if(st1.equals("state"))
            {
                new State_exam_reg().setVisible(true);//jframe class
                dispose();
            }
            else if (aps.equals("Apptitude"))
            {
            new App_Exam_Register_Panel().setVisible(true);//jframe class
                dispose();
            }       }       });
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • am new to stackoverflow , – venkatasubramanianr Sep 17 '16 at 14:45
  • 1
    Please edit your question to include a [mcve] that exhibits the problem you describe and the result you expect. – trashgod Sep 17 '16 at 14:48
  • 1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! – Andrew Thompson Sep 17 '16 at 14:53

1 Answers1

1

Don't use a MouseListener on a JMenuItem.

To handle the click of the menu item you should be using an Action.

Read the section from the Swing tutorial on How to Use Menus for more information and working examples.

Keep a link to the tutorial handle for examples of other Swing basics.

camickr
  • 321,443
  • 19
  • 166
  • 288