Question

Ok so trust me when I tell you that I have done my due diligence on this one. I've been trying to debug this for the longest time now and I've looked over the code again and again. I've checked Stack Overflow and nobody has had this problem.

So I am trying to create a menu in Java Swing. This menu is to be on a JPanel. I know that by convention it is supposed to be on a JFrame but it would be best for my circumstances on a JPanel. The problem is that when I click on the "Functions" menu, and try to hover my mouse over to one of the Menu items in it, the menu automatically disappears. I tried to implement the same code on a JFrame but the same thing happened.

So without further ado, Here is the code:

public static String[] getRGBInputsFromDialogBox(String title) {
    JTextField rField = new JTextField(5);
    JTextField gField = new JTextField(5);
    JTextField bField=new JTextField(5);

    JPanel myPanel = new JPanel(new FlowLayout());
    myPanel.add(new JLabel("R(x,y): "));
    myPanel.add(rField);
    myPanel.add(Box.createHorizontalStrut(15)); // a spacer
    myPanel.add(new JLabel("G(x,y): "));
    myPanel.add(gField);
    myPanel.add(Box.createHorizontalStrut(15));//spacer
    myPanel.add(new JLabel("B(x,y): "));
    myPanel.add(bField);



    String[] myClasses=ClassInfo.getClasses();                                
    //  { { { { } , { } } , { { } , { } } } , { { { } , { } } , { { } , { } } } }

    //Creating Menu Bar
    JMenuBar menuBar=new JMenuBar();
        JMenu functionsMenu=new JMenu("Functions");
            JMenuItem[] classes=new JMenuItem[myClasses.length];
            for(int i=0;i<ClassInfo.classes.length;i++) {
                String[][] classMethods=ClassInfo.showMethodsForClass(ClassInfo.classes[i]);//  { {static} , {instance} }
                final String className=ClassInfo.classes[i].getSimpleName();
                JMenuItem[] staticMethodsItem=new JMenuItem[classMethods[0].length];
                JMenuItem[] instanceMethodsItem=new JMenuItem[classMethods[1].length];
                JMenuItem staticMenuItem=new JMenuItem("Static methods");
                JMenuItem instanceMenuItem=new JMenuItem("Instance methods");
                JMenu staticMenu=new JMenu();
                staticMenuItem.add(staticMenu);
                JMenu instanceMenu=new JMenu();
                instanceMenuItem.add(instanceMenu);

                for(int a=0;a<classMethods[0].length;a++)   {
                    final String methodName=classMethods[0][a];
                    staticMethodsItem[a]=new JMenuItem(methodName);
                    staticMethodsItem[a].addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e)  {
                            ClassInfo.copyToClipBoard(ClassInfo.staticWriteableString(methodName,className));
                        }
                    });
                    staticMenu.add(staticMethodsItem[a]);
                }

                for(int a=0;a<classMethods[1].length;a++)   {
                    final String methodName=classMethods[1][a];
                    instanceMethodsItem[a]=new JMenuItem(methodName);
                    instanceMethodsItem[a].addActionListener(new ActionListener()   {
                        public void actionPerformed(ActionEvent e)  {
                            ClassInfo.copyToClipBoard(ClassInfo.instanceWriteableString(methodName));
                        }
                    });
                    instanceMenu.add(instanceMethodsItem[a]);
                }

                classes[i]=new JMenuItem(className);
                JMenu methodTypeMenu=new JMenu();
                methodTypeMenu.add(staticMenuItem); //add static method submenu to the new class submenu
                methodTypeMenu.add(instanceMenuItem);//add instance method submenu to the new class submenu
                classes[i].add(methodTypeMenu); //Make the new class a submenu
                functionsMenu.add(classes[i]); //Add a new class in the functions menu
            }
    menuBar.add(functionsMenu);
    myPanel.add(menuBar);

    int result = JOptionPane.showConfirmDialog(null, myPanel, title, JOptionPane.OK_CANCEL_OPTION);
    if (result == JOptionPane.OK_OPTION) {
        String[] arr= {rField.getText(),gField.getText(),bField.getText()};
        return arr;
    }
    return null;
}

Its quite a bit I know. Any kind of help at all would be greatly appreciated. Thanks a lot!

EDIT: As per request, here is my courseInfo class:

public class ClassInfo {

    public static Class<?>[] classes={Outputer.class,Vector3.class};
/*  
    public static void main(String[] args)  {
        System.out.println(classes[0].getMethods()[4].toGenericString());
        //public static int Outputer.fib(int) for static methods
        //public double Outputer.getOutputB(int,int) for instance methods
    }
*/  
    public static String[] getClasses() {
        final JFrame classesFrame=new JFrame("Select a Class You want to Use");
        String[] classNames=new String[classes.length];
        for(int i=0;i<classes.length;i++)   {
            classNames[i]=classes[i].getName();
        }
        return classNames;
/*  
        SpinnerListModel listModel=new SpinnerListModel(classNames); 
        JPanel spinnerPanel= new JPanel();
        JPanel buttonPanel=new JPanel();
        JLabel spinnerLabel=new JLabel("Choose a Class Whose Methods You Want to Inspect");
        final JSpinner classSpinner = new JSpinner(listModel);
        spinnerPanel.add(spinnerLabel);
        spinnerPanel.add(classSpinner);

        JButton inspectButton=new JButton("Inspect Methods");
        buttonPanel.add(inspectButton);

        inspectButton.addActionListener(new ActionListener()    {
            public void actionPerformed(ActionEvent e)  {
                for(int i=0;i<classes.length;i++)   {
                    if(classes[i].getName().equals(classSpinner.getValue()))    {
                        showMethodsForClass(classes[i]);
                        classesFrame.dispose();
                        break;
                    }
                }
            }
        });

        classesFrame.add(spinnerPanel);
        classesFrame.add(buttonPanel);
        classesFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        classesFrame.pack();
        classesFrame.setVisible(true);
*/
    }//End Of Method----------------------------------------------------------------------------------------------------------------

    public static String[][] showMethodsForClass(Class<?> myClass)  {
        final String className=myClass.getSimpleName();
        Method[] methods=myClass.getMethods();
        String[] staticMethodStrings1=new String[methods.length];
        String[] instanceMethodStrings1=new String[methods.length];
        int statCount=0,instCount=0;
        for(int i=0;i<methods.length;i++)   {
            String method=methods[i].toGenericString();
            System.out.println(method); //For Debugging
            if(!method.contains("private") && !method.contains("java."))    {
                if(methodIsStatic(method))  {
                    staticMethodStrings1[statCount]=method;
                    statCount++;
                } else  {
                    instanceMethodStrings1[instCount]=method;
                    instCount++;
                }
            }
        } //End of for loop

        String[] staticMethods=new String[statCount];
        String[] instanceMethods=new String[instCount];

        for(;statCount>0;statCount--)   {
            staticMethods[statCount-1]=staticMethodStrings1[statCount-1];
        }

        for(;instCount>0;instCount--)   {
            instanceMethods[instCount-1]=instanceMethodStrings1[instCount-1];
        }
        String[][] arr= {staticMethods,instanceMethods};
        return arr;
/*      JFrame frame=new JFrame("Select a Method to Copy");
        frame.setLayout(new GridLayout());

        JPanel staticPanel=new JPanel();
        JPanel instancePanel=new JPanel();
        final JComboBox staticMethodsBox=new JComboBox(staticMethods);
        final JComboBox instanceMethodsBox=new JComboBox(instanceMethods);
        JButton copyStaticButton=new JButton("Copy this Static Method");
        JButton copyInstanceButton=new JButton("Copy this Instance Method");

        copyStaticButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)  {
                copyToClipBoard(staticWriteableString((String)staticMethodsBox.getSelectedItem(),className));
            }
        });

        copyInstanceButton.addActionListener(new ActionListener()   {
            public void actionPerformed(ActionEvent e)  {
                copyToClipBoard(instanceWriteableString((String)staticMethodsBox.getSelectedItem()));
            }
        });


        staticPanel.add(staticMethodsBox);
        staticPanel.add(copyStaticButton);

        instancePanel.add(instanceMethodsBox);
        instancePanel.add(copyInstanceButton);

        frame.add(staticPanel);
        frame.add(instancePanel);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
*/      


    }//End Of Method----------------------------------------------------------------------------------------------------------------

    private static boolean methodIsStatic(String methodString)  {
        return methodString.contains("static ");
    }//End Of Method----------------------------------------------------------------------------------------------------------------

    //public static int Outputer.fib(int) for static methods
    //public double Outputer.getOutputB(int,int) for instance methods

    public static String staticWriteableString(String methodStr,String className)   {
        return methodStr.substring(methodStr.indexOf('.')-className.length());
    }

    public static String instanceWriteableString(String methodStr)  {
        return methodStr.substring(methodStr.indexOf('.'));
    }

    public static void copyToClipBoard(String myString) {
        StringSelection stringSelection = new StringSelection (myString);
        Clipboard clpbrd = Toolkit.getDefaultToolkit ().getSystemClipboard ();
        clpbrd.setContents (stringSelection, null);
    }

}//End of Class---------------------------------------------------------------------------------------------------------------------
Was it helpful?

Solution

I think I understood the problem. You are getting mixed with JMenu and JMenuItem. This is the structure:

JMenuBar has a JMenu/JMenuItem has a JMenu/JMenuItem

Change your internal for loop to this and it should work:

JMenuBar menuBar=new JMenuBar();
JMenu functionsMenu=new JMenu("Functions");
JMenuItem[] classes=new JMenuItem[myClasses.length];
for(int i=0;i<ClassInfo.classes.length;i++) {
    String[][] classMethods=ClassInfo.showMethodsForClass(ClassInfo.classes[i]);//  { {static} , {instance} }
    final String className=ClassInfo.classes[i].getSimpleName();
    JMenuItem[] staticMethodsItem=new JMenuItem[classMethods[0].length];
    JMenuItem[] instanceMethodsItem=new JMenuItem[classMethods[1].length];
    JMenu staticMenu=new JMenu("Static Methods");
    JMenu instanceMenu=new JMenu("Instance Methods");

    for(int a=0;a<classMethods[0].length;a++)   {
        final String methodName=classMethods[0][a];
        staticMethodsItem[a]=new JMenuItem(methodName);
        staticMethodsItem[a].addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)  {
                ClassInfo.copyToClipBoard(ClassInfo.staticWriteableString(methodName,className));
            }
        });
        staticMenu.add(staticMethodsItem[a]);
    }

    for(int a=0;a<classMethods[1].length;a++)   {
        final String methodName=classMethods[1][a];
        instanceMethodsItem[a]=new JMenuItem(methodName);
        instanceMethodsItem[a].addActionListener(new ActionListener()   {
            public void actionPerformed(ActionEvent e)  {
                ClassInfo.copyToClipBoard(ClassInfo.instanceWriteableString(methodName));
            }
        });
        instanceMenu.add(instanceMethodsItem[a]);
    }

    classes[i]=new JMenu(className);
    classes[i].add(staticMenu); //add static method submenu to the new class submenu
    classes[i].add(instanceMenu);//add instance method submenu to the new class submenu
    functionsMenu.add(classes[i]); //Add a new class in the functions menu
}
menuBar.add(functionsMenu);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top