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;
                }

                });
            }

Dani AI

Generated

This thread shows two common Swing stumbling blocks: event semantics for combo boxes, and component placement for menus. ran into both while wiring UI behavior, and correctly pushed toward checking containment. Below are concise, practical points to prevent the same surprises and to harden the UI code.

Pick the right event and read the selected value safely. A combo box produces different event types depending on what you need: use an item listener when you must react to selection versus deselection states, and use an action listener when you want a user-triggered “command” from the combo. When reading the selection, treat the returned object with the correct type (or use a typed model) and avoid fragile casts without checks. For patterns and examples see the JComboBox guide: How to Use Combo Boxes.

Menus often appear missing because of placement. A JMenu belongs in a JMenuBar and the bar belongs on the window, not as a random child of an ordinary panel. When menus are created dynamically, make sure the menu bar is installed on the top-level frame and the window is validated and packed so layout managers can size everything. Read the official menu guidance here: How to Use Menus.

Other robust practices: construct and modify Swing components on the Event Dispatch Thread (see the Swing concurrency tutorial), prefer a model object for dynamic combo content, and use layout managers that give components visible space. If debugging, print or inspect the component hierarchy and call validate()/repaint() after structural changes. For threads and EDT rules see: Swing Concurrency.

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.