bensjomic 0 Newbie Poster

Hi all,

I'm trying for a long time, to make a program working in java.
I would load a JPanel in my java programm when i press on a menu item.

Now i have made a function ChangeRoot(); in the main class.
After that he must change the contentPane();.
Can someone help me please?

here are the classes

Main.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package schoolsoftware;

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

/**
 *
 * @author Gebruiker
 */
public class Main extends JFrame{

    

    public Main()
    {
        
    }
    
//Container contentPane;
    public void changeRoot()
    {
       
        Container test = this.getContentPane();
       
        test.add(new WijzigAccount());
        test.repaint();
        test.setVisible(true);
        System.out.println("Menu item[] was pressed.");
      
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        JFrame frame = new JFrame("School beheer");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new JLabel("test"));
        frame.add(new NieuwAccount());
        frame.setJMenuBar(new Menu());
        
        
        frame.pack();
        frame.setSize(500,600);
        frame.setVisible(true);

    }

}

my class with the menu is

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package schoolsoftware;


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


/**
 *
 * @author Gebruiker
 */
public class Menu extends JMenuBar{

    String[] fileItems = new String[] {"Exit"};
    String[] accountItems = new String[] { "Nieuw account", "Zoeken", "Overzicht" };
    char[] fileShortcuts = {'X'};
    char[] accountShortcuts = { 'N','Z','O' };

    public Menu()
    {
        JMenu fileMenu = new JMenu( "Bestand" );
        JMenu accountMenu = new JMenu( "Accounts" );
        JMenu subMenu = new JMenu( "SubMenu" );
        JMenu subMenu2 = new JMenu( "SubMenu2" );

        // Samenvoegen
        ActionListener printListener = new ActionListener(){
            public void actionPerformed(ActionEvent event){
                System.out.println("Menu item[" + event.getActionCommand() + "] was pressed.");

              Main main = new Main();
             
              main.changeRoot();
            }
        };

        // Menu items uit File lijst
        for(int i=0; i< fileItems.length; i++)
        {
            JMenuItem item = new JMenuItem(fileItems[i],fileShortcuts[i]);
            item.addActionListener(printListener);
            fileMenu.add(item);
        }

        // Menu items uit account lijst met keyboard fuctionaliteit
        for(int i=0; i< accountItems.length; i++)
        {
            JMenuItem item = new JMenuItem(accountItems[i]);
            item.setAccelerator(KeyStroke.getKeyStroke(accountShortcuts[i],
                    Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(), false));
            item.addActionListener(printListener);
            accountMenu.add(item);
        }

        add(fileMenu);
        add(accountMenu);

    }
}

Hopefully someone can help me to change the NieuwAccount() jpanel with the WijzigAccount() panel.

Thanks anyway :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.