luli kuku
luli kuku

Reputation: 21

How can I open a JPanel from a menu?

I'm trying to create a menu and from it I need to open a JPanel. How can I do that?

I want to the user to press "individual details" for example and then for it to open an area where i can add buttons and textfields.


public class Payroll{

public static void main(String[] args) {
    JFrame frame = new JFrame(" Payroll "); 

    //create the employees details menu 
    JMenu employees = new JMenu("Employees");
    employees.setMnemonic(KeyEvent.VK_E);

    // add employees items
        JMenuItem details = new JMenuItem("Individual Details");
        details.addActionListener(new ActionListener( )
        {
            public void actionPerformed(ActionEvent e)
            { 
                /**********missing code is here, how can i open a JPanel from here?**/ 

            }
        });

        employees.add(details);


    //menu bar 
    JMenuBar menuBar = new JMenuBar( ); 
    menuBar.add(employees);


    frame.setJMenuBar(menuBar);
    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    frame.setSize(700,550);
    frame.setVisible(true);

} 

}

Upvotes: 0

Views: 1844

Answers (2)

Lalit Jawale
Lalit Jawale

Reputation: 1246

You can try like below,

JPanel panel = new JPanel();
JButton okButton = new JButton("OK");
panel.add(okButton);
JButton cancelButton = new JButton("Cancel");
panel.add(cancelButton);
frame.add(panel);

Upvotes: 0

Madhawa Priyashantha
Madhawa Priyashantha

Reputation: 9872

you can create a another class which has a jpanel and fields for take userinput .and then create a instance of it inside menuitem actionPerformed event...

for example ;

this is the class which has panel

import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class MyPanel {

    public MyPanel() {
        JFrame f=new JFrame();
        f.setSize(300,200);
        f.setLayout(new GridLayout(1, 1));
        JPanel p=new JPanel();
        p.setLayout(new GridLayout(3, 1, 2, 2));
        JTextField t1=new JTextField(20);
        p.add(t1);
        f.add(p);
        f.setVisible(true);
    }


}

and make a instance of it inside event ..

public class Payroll{

public static void main(String[] args) {
    JFrame frame = new JFrame(" Payroll "); 

    JMenu employees = new JMenu("Employees");
    employees.setMnemonic(KeyEvent.VK_E);

    // add employees items
        JMenuItem details = new JMenuItem("Individual Details");
        details.addActionListener(new ActionListener( )
        {
            public void actionPerformed(ActionEvent e)
            { 
                MyPanel panel=new MyPanel(); // call MyPanel here

            }
        });

        employees.add(details);


    //menu bar 
    JMenuBar menuBar = new JMenuBar( ); 
    menuBar.add(employees);


    frame.setJMenuBar(menuBar);
    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    frame.setSize(700,550);
    frame.setVisible(true);

} 

}

Upvotes: 1

Related Questions