What is wrong with my code?

String[]  movieString = {"movie1", "movie2", "movie3", "movie4"};
      
    JComboBox movieList = new JComboBox(movieString);
    movieList.addActionListener( 
        new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
      JComboBox a = (JComboBox) e.getSource();
      //String movies = (String)a.getSelectedItem();
         if (e.isSelected());
    
                  return subpanel;
                }

                });
            }

Recommended Answers

All 8 Replies

What is subpanel? Up till line 9 your code seems ok to me, after that I have no idea what you are trying to do.

Here is my subpanel

JPanel fourthQuestion ()
  {      
    JPanel subpanel = new JPanel ();
    subpanel.setLayout (new GridLayout (3,1));

My problem is how to implement the actionListener...

if e.isSelected makes no sense because you already know which item is selected, and you already know that it's selected because otherwise the actionPerformed method wouldn't have been called. And if you want help writing the rest of the method you should probably mention what you want it to do, which you never said. Do you think we know what your program's requirements are?

I believe I got it working now...

String[]  movieString = {"Lethal Weapon IV", "Titanic", "Saving Private Ryan", 
                        "Le Art Movie avec subtitles"};
        
   JComboBox movieList = new JComboBox(movieString);
    movieList.addActionListener( 
        new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
      JComboBox a = (JComboBox) e.getSource();
      String movies = (String)a.getSelectedItem();
           }
        }
       ); 
        subpanel.add(movieList);  
        return subpanel;
}

another method I am having some trouble with. I am trying to add a menu and 2 menu items. The problem is: it is not showing the menu...

JMenu makeHelpMenu()
    {
    JMenu helpMenu = new JMenu("Help");
    
     //Help menu item
   JMenuItem help = new JMenuItem("Help");
    help.addActionListener(
       new ActionListener(){
          public void actionPerformed(ActionEvent a)
            {
              Help();
           }
        }
        );
        helpMenu.add(help);
        
    
   //create the item about
   JMenuItem about = new JMenuItem("About");
   about.addActionListener(
    new ActionListener(){
        public void actionPerformed(ActionEvent a)
        {
            about();
        }
    }
   );
    helpMenu.add(about);
    
    return helpMenu;
}



  // Process data when ready.
  
  void Help()
  {
      System.out.print("You are on your own");
    }
    
    void about()
    {
        System.out.println("This is a test");
        System.out.println(" Easy to understand");
    }

Did you add the helpMenu to your JPanel? You didn't provide enough code to see what's going on. The method you posted looks fine but I'm not sure if you ever added the helpMenu (that was returned) to anything. PS after you add helpMenu to the JPanel you might want to call revalidate()

You're right. I completely forgot to add it. Problem solved as well.

Cool. Mark the thread as solved please.

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.