Java Programming Tutorial – 69 – Drop Down List Program

///Java Programming Tutorial – 69 – Drop Down List Program

Java Programming Tutorial – 69 – Drop Down List Program

FavoriteLoadingAdd to favorites

Facebook –
GitHub –
Google+ –
LinkedIn –
reddit –
Support –
thenewboston –
Twitter –

source

Java468x60
By |2019-10-08T23:01:13+00:00October 8th, 2019|Java Video Tutorials|39 Comments

39 Comments

  1. Korbin Koch October 8, 2019 at 11:01 pm - Reply

    heh.. nice

  2. Grease quala October 8, 2019 at 11:01 pm - Reply

    If you do not plan to build Enterprise grade application, do not learn Java. It will be useless. BTW; Why am I watching an excellent tutorial after 10 years !!!

  3. Ryan Goo October 8, 2019 at 11:01 pm - Reply

    69

  4. S. Abdelouahab October 8, 2019 at 11:01 pm - Reply

    How do you change these pictures with a String? Please help!

  5. Dev October 8, 2019 at 11:01 pm - Reply

    please provide the source code

  6. KozakTW October 8, 2019 at 11:01 pm - Reply

    You have made 'a lot of tutorials' but noticed it only on 69th. Easy enough:)

  7. Bruno Ribeiro October 8, 2019 at 11:01 pm - Reply

    I claimed the like 969 !!!! Ohhhhh yes baby… on video 69!!

  8. Jiff Pop October 8, 2019 at 11:01 pm - Reply

    lol 69

  9. Tuyết Lê October 8, 2019 at 11:01 pm - Reply

    I had a problem
    Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:217)
    at javabasic.comboBox.<init>(comboBox.java:26)
    at javabasic.testComboBox.main(testComboBox.java:16)
    C:UsersADMINNAppDataLocalNetBeansCache8.2executor-snippetsrun.xml:53: Java returned: 1
    BUILD FAILED (total time: 2 seconds)
    help me please

  10. P092O9901O09 October 8, 2019 at 11:01 pm - Reply

    I'd rather write a handler class since the box.addItemListener method call just totally confused me

  11. Feyisa Daniel October 8, 2019 at 11:01 pm - Reply

    tnx very much. i got what i need

  12. MIO9 October 8, 2019 at 11:01 pm - Reply

    Java 8 news: The anonymous class can be expressed with lambda expressions

  13. keshav ganguly October 8, 2019 at 11:01 pm - Reply

    you are the best man on earth..thanks a lot

  14. C Liviu October 8, 2019 at 11:01 pm - Reply

    Why does the fileame array has to be static? I removed static in my program and it works just fine.

  15. Rey Robert Castro October 8, 2019 at 11:01 pm - Reply

    Fixed Error: ArrayOutOfBounds
    First: Delete the Icon[] icons array And
    Second: replace the itemStateChanged()method with this one:

    public void itemStateChanged(ItemEvent e) {
    int selectedIndex = box.getSelectedIndex();
    picture.setIcon(new ImageIcon(getClass().getResource(filename[selectedIndex])));
    index.setText(filename[selectedIndex]);
    }
    });

  16. Mahesh S October 8, 2019 at 11:01 pm - Reply

    what is that .getClass().getResources() can anyone explain me plzzzzzzzzzz??

  17. kaveri roy October 8, 2019 at 11:01 pm - Reply

    the program is running nicely with the first pic as default pic but whenever i am trying to click on the next image it is throwing an array index out of bound exception and it is showing exception on this line

    picture.setIcon(pics[box.getSelectedIndex()]);

  18. Skander Mselmi October 8, 2019 at 11:01 pm - Reply

    u have to update this video specialy because it doesn't work anymore !! and i think this important nd thnks !!

  19. Jebaited October 8, 2019 at 11:01 pm - Reply

    hi i'm just new to java. how do you set a sub-combobox's contents depending on what the user picked in the main-combobox

  20. Mahgoub Abbas October 8, 2019 at 11:01 pm - Reply

    i get to choose which image to view but none of the images appears

  21. Mahgoub Abbas October 8, 2019 at 11:01 pm - Reply

    i dont get the methods for item and actionListner how do they have methods

  22. Samson Zheng October 8, 2019 at 11:01 pm - Reply

    6:52 how would my if statement look like if i have an array of entries?
    my jcombobox shows a list of pokemon names.
    is there a way for java to recognize that i clicked on array[x] so that i can use that x to call array3[x]?

  23. Evgeni Kuzmov October 8, 2019 at 11:01 pm - Reply

    What is ItemEvent event? We didnt declare it

  24. TheUltimateProgrammer October 8, 2019 at 11:01 pm - Reply

    The Image does not change. Here is my code:public class Gui extends JFrame{
     private JComboBox box;
     private JLabel pic;
     private static String filename[] = {"b.png","x.png"};
     private Icon pics[] = {new ImageIcon(getClass().getResource(filename[0])),new ImageIcon(getClass().getResource(filename[0])),};
     public Gui(){
         super("My pics");
         setLayout(new FlowLayout());
         JComboBox<String> box = new JComboBox<>(filename);
         pic = new JLabel(pics[0]);
         add(box);
         box.addItemListener(
           new ItemListener(){     public void itemStateChanged(ItemEvent e) {
             if (e.getStateChange() == ItemEvent.SELECTED){
              pic.setIcon(pics[box.getSelectedIndex()]);
              
              
             }
            
          
         }}
           );
                 pic = new JLabel(pics[0]);
                 add(pic);
      }
    }

  25. Brandon Guerrero October 8, 2019 at 11:01 pm - Reply

    This was an awesome tutorial! I do have a question though. I got the general idea of what you were doing here, but is there any way that I could do this with .wav files instead of pictures? Also I want to populate my drop down menu options with items that I have in an XML file. When a user selects one of those items, then they should be able to have access to the .wav files that were called from the XML. Does that make sense? I know it's a tricky question, but I would really appreciate any help!

  26. Sasa October 8, 2019 at 11:01 pm - Reply

    This is my first class:

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

    public class JComboBox1 extends JFrame {

    private JComboBox box;
    private JLabel picture;

    private static String[] filename = {"Uzorak1.png" , "Uzorak2.png"}; //Those are the picture names that I have in eclipse

    private Icon[] pics = {new ImageIcon(getClass().getResource(filename[0])), new ImageIcon(getClass().getResource(filename[1]))};

    public JComboBox1 () {
    super ("The title of the window");
    setLayout (new FlowLayout ());

    JComboBox<String> box = new JComboBox<>(filename);

    box.addItemListener(
    new ItemListener () {
    public void itemStateChanged (ItemEvent events) {

    if (events.getStateChange() == ItemEvent.SELECTED)
    picture.setIcon(pics[box.getSelectedIndex()]);
    }
    }
    );

    add (box);
    picture = new JLabel (pics [0]);
    add (picture);
    }
    }

    This is my secound class:

    import javax.swing.JFrame;

    class JComboBox2 {

    public static void main (String[] args) {

    JComboBox1 objectP = new JComboBox1();

    objectP.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    objectP.setSize(300,200);

    objectP.setVisible (true);
    }
    }

    Whwn I run program I get this error:

    Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at JComboBox1.<init>(JComboBox1.java:14)
    at JComboBox2.main(JComboBox2.java:7)

    Please help????

  27. Eugene Kuzmov October 8, 2019 at 11:01 pm - Reply

    but you promised… 🙂

  28. rtc October 8, 2019 at 11:01 pm - Reply

    ERROR: Java.lang.NullPointerException !!…It says null(in javax.swing.ImageIcon)…what should I do?!…for the record I use BlueJ

  29. Eric Barnes October 8, 2019 at 11:01 pm - Reply

    My pictures show up huge and takes over entire window, while his sits small and neat in the window. How can I get my pictures to show up smaller? HELLLPPPPP

  30. Isaac Duncan October 8, 2019 at 11:01 pm - Reply

    If you're using Java 7 or above: Replace
    box = new JComboBox(filename);
    with:
    JComboBox<String> box = new JComboBox<>(filename);
    It's something to do with Generics

  31. krishna prasad October 8, 2019 at 11:01 pm - Reply

    its giving the same output even if put the line: if(event.getStateChange()==ItemEvent.SELECTED)
    or the line : if(event.getStateChange()==ItemEvent.DESELECTED)
    or no line at all

  32. lupdelup October 8, 2019 at 11:01 pm - Reply

    I stuck with adding action listeners for all of the previous tutorials and it seems to have still worked. comboBox is my JComboBox object, images is my pics array, and comboBoxPic is my JLabel where i want the picture to display on my GUI. The following code works for the event handling:

    comboBox.addActionListener(
    new ActionListener(){ // new instance of class we normally implement
    public void actionPerformed(ActionEvent e){ // override method
    comboBoxPic.setIcon(images[comboBox.getSelectedIndex()]);
    // getSelectedIndex – returns index of item chosen from list
    }
    }
    );

  33. retseh G October 8, 2019 at 11:01 pm - Reply

    Got this error:
    Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at package.Gui.<init>(Gui.java:12)
    at package.apples.main(apples.java:9)

  34. Anil kumar October 8, 2019 at 11:01 pm - Reply

    Anyone with source code ?

  35. Bijoyan Das October 8, 2019 at 11:01 pm - Reply

    put the png images in both "src" and "bin" folders in your workspace to run it successfully.

  36. wildglorypsn October 8, 2019 at 11:01 pm - Reply

    but how on earth could we possibly remember all these big statements..??like the one in the Icon string and the one inside the if statement???

  37. Jason Miller October 8, 2019 at 11:01 pm - Reply

    java was my first programming language and i hate it. Theres so much crap you gotta remember to just to simple shit. Python seems the way to go.

  38. Daniel Andrews October 8, 2019 at 11:01 pm - Reply

    Hey new boston, so I copy everything like so, but I'm still getting an exception saying that says box.<init>(Box.java:10) and stuff

  39. X _ October 8, 2019 at 11:01 pm - Reply

    why bucky sometimes uses static before array and sometimes not?

Leave A Comment

*