0

I want to move from one JFrame to another JFrame after pressing JButton named as login. I am attaching my both frame code below, if there is correction or any one want to give me instruction or guideline then can give.

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Login implements ActionListener {
private JFrame jFrame = null;  
private JPanel jContentPane = null;
private JLabel id = null;
private JTextField userid = null;
private JLabel pass = null;
private JPasswordField password = null;
private JButton login = null;       
    private JFrame getJFrame() 
    {
        if (jFrame == null) 
        {
            jFrame = new JFrame();
    jFrame.setSize(new Dimension(346, 301));
            jFrame.setResizable( false );
    jFrame.setTitle("Shree Datta Digambar");
            jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jFrame.setContentPane(getJContentPane());
        }
        return jFrame;
}
    private JPanel getJContentPane() 
    {
        if (jContentPane == null) 
        {
            pass = new JLabel();
    pass.setBounds(new Rectangle(21, 105, 116, 27));
        pass.setText("Password");
    id = new JLabel();
            id.setBounds(new Rectangle(21, 54, 119, 26));
    id.setText("Enter User Id");
            jContentPane = new JPanel();
    jContentPane.setLayout(null);
    jContentPane.add(id, null);
    jContentPane.add(getUserid(), null);
            jContentPane.add(pass, null);
    jContentPane.add(getPassword(), null);
    jContentPane.add(getLogin(), null);
        }
        return jContentPane;
}
    private JTextField getUserid() 
    {
        if (userid == null) 
        {
            userid = new JTextField();
    userid.setBounds(new Rectangle(164, 53, 140, 26));
        }
        return userid;
}   
private JPasswordField getPassword() 
    {
        if (password == null) 
        {
            password = new JPasswordField();
    password.setBounds(new Rectangle(165, 106, 137, 27));
        }
        return password;
}
private JButton getLogin() 
    {
        if (login == null) 
        {
            login = new JButton();
    login.setBounds(new Rectangle(165, 169, 136, 27));
    login.setText("Login");
            login.addActionListener((ActionListener) this);
        }
        return login;
}

public static void main(String[] args) 
    {
        Login l=new Login();
        JFrame f=new JFrame();           
        f=l.getJFrame();
        f.setLocation(300,150);
        f.setVisible(true);
    }
    public void actionPerformed(ActionEvent e) 
    {
        if(e.getSource()==login)
        {
            if(userid.getText().equals("")||password.getText().equals(""))
            {
                JOptionPane.showMessageDialog(null, "Please Enter Details");
            }
            else
            {
                if(userid.getText().equals("digambar")&&password.getText().equals("dalvi"))
                {                                 
                    JOptionPane.showMessageDialog(null,"Welcome To Customer Details of Papers");
                    getJFrame().dispose();  
                    JMenuBar menuBar = new JMenuBar();
                    MainMenu f = new MainMenu();                              
                }
                else
                {
                    JOptionPane.showMessageDialog(null,"Sorry Please Enter Valid Details");
                    userid.setText("");
                    password.setText("");
                }
            }
        }

    }       
}

second frame code is

       import java.awt.event.ActionEvent;
       import java.awt.event.ActionListener;
       import javax.swing.JFrame;
       import javax.swing.JMenu;
       import javax.swing.JMenuBar;
       import javax.swing.JMenuItem;
       public class MainMenu extends JFrame implements ActionListener
       {  
public static void main(String[] args) 
{
    MainMenu f=new MainMenu();
    f.setVisible(true);
    f.setSize(646,401);
    f.setResizable( false );
    f.setTitle("Shree Datta Digambar");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JMenuBar menuBar = new JMenuBar();
    f.setJMenuBar(menuBar);

    JMenu customer=new JMenu("Customer");
    menuBar.add(customer);
    JMenu paper=new JMenu("Paper");
    menuBar.add(paper);
    JMenu edit=new JMenu("Edit");
    menuBar.add(edit);              
    JMenu view=new JMenu("View");
    menuBar.add(view); 
    JMenu bill=new JMenu("Bill");
    menuBar.add(bill);
    JMenu help=new JMenu("Help");
    menuBar.add(help);

    JMenuItem newItem = new JMenuItem("New Customer");
    customer.add(newItem);
    JMenuItem deleteItem = new JMenuItem("Delete Customer");
    customer.add(deleteItem);

    JMenuItem addpaperItem = new JMenuItem("Add Paper");
    paper.add(addpaperItem);
    JMenuItem deletepaperItem = new JMenuItem("Delete Paper");
    paper.add(deletepaperItem);

    JMenuItem customer_detail = new JMenuItem("Customer Detail");
    edit.add(customer_detail);
    JMenuItem paper_detail = new JMenuItem("Paper Detail");
    edit.add(paper_detail);

    JMenuItem perticular_customer_detail = new JMenuItem("Perticular Customer Detail");
    view.add(perticular_customer_detail);

    JMenuItem customer_bill = new JMenuItem("Customer Bills");
    bill.add(customer_bill);

    newItem.addActionListener(new MainMenu());
    deleteItem.addActionListener(new MainMenu());
    addpaperItem.addActionListener(new MainMenu());
    deletepaperItem.addActionListener(new MainMenu());
    customer_detail.addActionListener(new MainMenu());
    paper_detail.addActionListener(new MainMenu());
    perticular_customer_detail.addActionListener(new MainMenu());
    customer_bill.addActionListener(new MainMenu());

    customer.addActionListener(new MainMenu());        
    paper.addActionListener(new MainMenu());
    edit.addActionListener(new MainMenu());
    view.addActionListener(new MainMenu());
    bill.addActionListener(new MainMenu());
    help.addActionListener(new MainMenu());
}
@Override
public void actionPerformed(ActionEvent e) {
    throw new UnsupportedOperationException("Not supported yet.");
}
}
Devendra
  • 63
  • 11
  • 2
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) Use a modal `JDialog` for one of them. – Andrew Thompson Jun 09 '14 at 04:21
  • *"..i want to move from one jframe to another jframe after pressing jbutton named as login."* So.. what's the problem? Or do you just want us to finish the code for you? *"I am attaching my both jframe code below, if there is correction or any one want to give me instruction or guideline then can give."* That is more a matter for [code review](http://codereview.stackexchange.com/). – Andrew Thompson Jun 09 '14 at 04:24
  • 1
    The method getText() from the type JPasswordField is deprecated. use getPassword(). – Nidheesh Jun 09 '14 at 04:29
  • 1
    BTW Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead [use layout managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html), or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Jun 09 '14 at 05:11
  • Consider using a dialog or CardLayout instead – MadProgrammer Jun 09 '14 at 05:45

3 Answers3

1

You can make use of constructors of a class. Constructors are used to initialize objects. I have used constructors. Please find updated code.

Login.java

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.*;

public class Login implements ActionListener {

private JFrame jFrame = null;  
private JPanel jContentPane = null;
private JLabel id = null;
private JTextField userid = null;
private JLabel pass = null;
private JPasswordField password = null;
private JButton login = null;       

    //Constructor   
public Login() {

     pass = new JLabel();
     pass.setBounds(new Rectangle(21, 105, 116, 27));
     pass.setText("Password");
     id = new JLabel();
     id.setBounds(new Rectangle(21, 54, 119, 26));
     id.setText("Enter User Id");

     userid = new JTextField();
     userid.setBounds(new Rectangle(164, 53, 140, 26));

     password = new JPasswordField();
     password.setBounds(new Rectangle(165, 106, 137, 27));

     login = new JButton();
     login.setBounds(new Rectangle(165, 169, 136, 27));
     login.setText("Login");
     login.addActionListener((ActionListener) this);

     jContentPane = new JPanel();
     jContentPane.setLayout(null);
     jContentPane.add(id, null);
     jContentPane.add(userid, null);
     jContentPane.add(pass, null);
     jContentPane.add(password, null);
     jContentPane.add(login, null);


    jFrame = new JFrame();
    jFrame.setSize(new Dimension(346, 301));
    jFrame.setResizable( false );
    jFrame.setTitle("Shree Datta Digambar");
    jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jFrame.setContentPane(jContentPane);
    jFrame.setLocation(300,150);
    jFrame.setVisible(true);
    }
    public void actionPerformed(ActionEvent e) 
    {
        if(e.getSource()==login)
        {
            if(userid.getText().equals("")||password.getPassword().equals(""))
            {
                JOptionPane.showMessageDialog(null, "Please Enter Details");
            }
            else
            {
                if(userid.getText().equals("digambar")&&  Arrays.equals(password.getPassword(), "dalvi".toCharArray()))
                {                                 
                    JOptionPane.showMessageDialog(null,"Welcome To Customer Details of Papers");
                    this.jFrame.dispose();  
                    MainMenu f = new MainMenu();
                }
                else
                {
                    JOptionPane.showMessageDialog(null,"Sorry Please Enter Valid Details");
                    userid.setText("");
                    password.setText("");
                }
            }
        }

    } 

   public static void main(String[] args){
        Login l=new Login();
    }
}

MainMenu.java

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class MainMenu extends JFrame implements ActionListener
{ 
        //Constructor   
    public MainMenu() 
    {
        JMenuBar menuBar = new JMenuBar();
        this.setJMenuBar(menuBar);
        this.setVisible(true);
        this.setSize(646,401);
        this.setResizable( false );
        this.setTitle("Shree Datta Digambar");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JMenu customer=new JMenu("Customer");
        menuBar.add(customer);
        JMenu paper=new JMenu("Paper");
        menuBar.add(paper);
        JMenu edit=new JMenu("Edit");
        menuBar.add(edit);              
        JMenu view=new JMenu("View");
        menuBar.add(view); 
        JMenu bill=new JMenu("Bill");
        menuBar.add(bill);
        JMenu help=new JMenu("Help");
        menuBar.add(help);

       JMenuItem newItem = new JMenuItem("New Customer");
        customer.add(newItem);
        JMenuItem deleteItem = new JMenuItem("Delete Customer");
        customer.add(deleteItem);

        JMenuItem addpaperItem = new JMenuItem("Add Paper");
        paper.add(addpaperItem);
        JMenuItem deletepaperItem = new JMenuItem("Delete Paper");
        paper.add(deletepaperItem);

        JMenuItem customer_detail = new JMenuItem("Customer Detail");
        edit.add(customer_detail);
        JMenuItem paper_detail = new JMenuItem("Paper Detail");
        edit.add(paper_detail);

        JMenuItem perticular_customer_detail = new JMenuItem("Perticular Customer Detail");
        view.add(perticular_customer_detail);

        JMenuItem customer_bill = new JMenuItem("Customer Bills");
        bill.add(customer_bill);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    public static void main(String arg[])
    {
        MainMenu menu1= new MainMenu();
    }
    }
Nidheesh
  • 662
  • 1
  • 8
  • 18
1

Instead of separate frames, you could construct your UI based on JPanels and either use CardLayout to switch between them or a JDialog for the login panel and a JFrame for the main panel

The main point is you can separate the controlling mechanism from the UI components, making it easier to change the path of logic should you have to.

Take a look at How to Use CardLayout and How to Make Dialogs for more details.

You should also make appropriate use of layout managers

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

You can use anonymous inner classes for your JMenuItem,

 JMenuItem newItem = new JMenuItem("New Customer");  
 newItem.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               MainMenu.this.dispose();
               New_Customer nn = new New_Customer() {};
           }
       });

or

Let MainMenu implement the ActionListener interface (class MainMenu implements ActionListener)

JMenuItem newItem = new JMenuItem("New Customer");
newItem.addActionListener(this);
newItem.setActionCommand("New Customer");

public void actionPerformed(ActionEvent e) 
{
    if(e.getActionCommand().equals("New Customer"))
    {
        System.out.println("clicked new customer menu");
        MainMenu.this.dispose();
        New_Customer nn = new New_Customer() {};
    }
}
Nidheesh
  • 662
  • 1
  • 8
  • 18
  • yes its works thank you sir.. if i want to do same inner classes coding for another menuitems for them can i do same coding or is their any other way to do coding. – Devendra Jun 11 '14 at 09:01
  • use .equals() for checking strings are equal – Nidheesh Jun 11 '14 at 09:30
  • add actionlistener `newItem.addActionListener(this);` & set action command `setActionCommand("New Customer");` – Nidheesh Jun 11 '14 at 09:39