Java combobox 📑

Sdílet
Vložit
  • čas přidán 24. 08. 2020
  • Java, combobox, JComboBox, GUI, swing, tutorial, beginners,
    #Java #combobox #JComboBox #GUI #swing #tutorial #beginners
    //***********************************************
    public class Main{
    public static void main(String[] args) {
    // JComboBox = A component that combines a button or editable field and a drop-down list
    new MyFrame();
    }
    }
    //***********************************************
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class MyFrame extends JFrame implements ActionListener{
    JComboBox comboBox;
    MyFrame(){
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(new FlowLayout());
    String[] animals = {"dog","cat","bird"};
    comboBox = new JComboBox(animals);
    comboBox.addActionListener(this);
    //comboBox.setEditable(true);
    //System.out.println(comboBox.getItemCount());
    //comboBox.addItem("horse");
    //comboBox.insertItemAt("pig", 0);
    //comboBox.setSelectedIndex(0);
    //comboBox.removeItem("cat");
    //comboBox.removeItemAt(0);
    //comboBox.removeAllItems();
    this.add(comboBox);
    this.pack();
    this.setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
    if(e.getSource()==comboBox) {
    System.out.println(comboBox.getSelectedItem());
    //System.out.println(comboBox.getSelectedIndex());
    }
    }
    }
  • Věda a technologie

Komentáře • 76

  • @BroCodez
    @BroCodez  Před 3 lety +15

    //***********************************************
    public class Main{
    public static void main(String[] args) {

    // JComboBox = A component that combines a button or editable field and a drop-down list

    new MyFrame();
    }
    }
    //***********************************************
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class MyFrame extends JFrame implements ActionListener{
    JComboBox comboBox;

    MyFrame(){
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(new FlowLayout());

    String[] animals = {"dog","cat","bird"};

    comboBox = new JComboBox(animals);
    comboBox.addActionListener(this);

    //comboBox.setEditable(true);
    //System.out.println(comboBox.getItemCount());
    //comboBox.addItem("horse");
    //comboBox.insertItemAt("pig", 0);
    //comboBox.setSelectedIndex(0);
    //comboBox.removeItem("cat");
    //comboBox.removeItemAt(0);
    //comboBox.removeAllItems();

    this.add(comboBox);
    this.pack();
    this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
    if(e.getSource()==comboBox) {
    System.out.println(comboBox.getSelectedItem());
    //System.out.println(comboBox.getSelectedIndex());
    }
    }
    }

  • @ricardocarvalho5221
    @ricardocarvalho5221 Před 3 lety +23

    Hey Bro, I'm from Brazil and I want to thank you for all your videos about java. I have been learning a lot with BroCode channel. Thank you!

  • @sirekay4292
    @sirekay4292 Před 2 lety +1

    Danke schon bruder. Your explanation was straight to the point and clear. Also thanks for the code.

  • @justinbanza4751
    @justinbanza4751 Před 2 lety +1

    Thank you very much for this video
    very clear!

  • @chchong68
    @chchong68 Před 2 lety

    Thank you very much for sharing this video, easy to understand.

  • @peoso1
    @peoso1 Před 3 lety +2

    Great video, thanks

  • @mohamadalrefaai2006
    @mohamadalrefaai2006 Před 3 lety +2

    You are the best!! :)

  • @garrickreynolds2015
    @garrickreynolds2015 Před 2 lety +1

    420th like 😎
    your videos really help bro! Thank you!

  • @tristisludos
    @tristisludos Před 3 lety +2

    Doing my Part to defeat the Algorithm.
    Keep it up mate!!!

  • @servantofthemosthigh9250
    @servantofthemosthigh9250 Před 3 lety +1

    this is what I call quality content

  • @alexshepel5599
    @alexshepel5599 Před 3 lety +2

    Nice, bro!

  • @kalashacharya9821
    @kalashacharya9821 Před 2 lety

    Love this.

  • @dupaulmpoyiart5464
    @dupaulmpoyiart5464 Před 2 lety +1

    i have become a good developer in java because your video are extra ordinaire, very claire:
    thantyou bro, God bless you a lot

  • @eugenezuev7349
    @eugenezuev7349 Před 2 měsíci

    Thank you for all your work

  • @K-tf5ph
    @K-tf5ph Před 3 lety +9

    Very clean explanation. It was really helpful for me, thanks ✓

    • @BroCodez
      @BroCodez  Před 3 lety +3

      thank you for watching Kaymak!

  • @henriquefelinimena3693
    @henriquefelinimena3693 Před 3 lety +1

    thank you for the content

  • @OmerFaruk-pu6cd
    @OmerFaruk-pu6cd Před 3 lety +4

    you saved my day tnx

  • @amireskandari3087
    @amireskandari3087 Před 2 lety

    Thanks that was helpful 👍

  • @pavelkvasnicka6856
    @pavelkvasnicka6856 Před rokem

    This is the best Java tutorial for beginners, so you can learn Java and English in one hit. Please keep going! I vote for Java advance tutorial. Thanks a lot Bro

  • @hamza98888
    @hamza98888 Před rokem

    coooooool , best tutorials ever , good job bro code

  • @nonoco44
    @nonoco44 Před 3 lety +2

    I love your gui vidios they are so helpful

  • @speklex779
    @speklex779 Před 8 měsíci

    NIce video

  • @rares1263
    @rares1263 Před 3 lety +1

    This was verry helpfull

  • @jamiecarrer9212
    @jamiecarrer9212 Před 3 lety

    thx
    Ur the best

  • @__deepu__pb31
    @__deepu__pb31 Před rokem +1

    Thanks brother 😊

  • @amanthakur6913
    @amanthakur6913 Před 3 lety +2

    please do tutorial of jdbc also and make a project using Java Swing and JDBC

  • @senexavis9920
    @senexavis9920 Před 3 lety +1

    Very good.

  • @umutyananer7055
    @umutyananer7055 Před rokem

    Great

  • @CaptainOfTheCrapShak
    @CaptainOfTheCrapShak Před rokem

    ty

  • @ibrahimylmaz8378
    @ibrahimylmaz8378 Před 2 lety

    thanks bro

  • @user-td4rh7eu1g
    @user-td4rh7eu1g Před měsícem

    Thanks bro

  • @Amangupta-rh7cy
    @Amangupta-rh7cy Před 3 lety +2

    Good video

  • @twojaStara698
    @twojaStara698 Před 3 lety +4

    Subscribed. Please do more tut of Gui in java

  • @kemann3815
    @kemann3815 Před 2 lety

    Da best

  • @af607
    @af607 Před 2 lety

    gooood job

  • @pierotorchio1945
    @pierotorchio1945 Před 3 lety

    how can I sort alphabetically items inside a combobox ? Thanks

  • @MrLoser-ks2xn
    @MrLoser-ks2xn Před 2 lety

    Thanks

  • @PMe-my1td
    @PMe-my1td Před 3 lety

    Hey Bro! Question for you!
    How do you setSelectedItem(String_var_here) from another class calling the JcomboBox. It doesnt show the change on the GUI and the box is 'frozen'.

  • @birukzerihun6615
    @birukzerihun6615 Před 2 lety

    keep it on

  • @zari_723
    @zari_723 Před rokem

    hi, have a great Sunday

  • @o0QuAdSh0t0o
    @o0QuAdSh0t0o Před 2 lety +1

    How to store user’s selection in a variable

  • @jaybartgis5148
    @jaybartgis5148 Před 3 lety

    Oh never mind never mind I got it. For the actionPerformed method, you would then continue it with numerous if statements and say "if(animals.getSelectedIndex()==0 {sysout("German Sheppard")" i see now, i get it

    • @AnthonyR007
      @AnthonyR007 Před 3 lety

      You should’ve replied this to your original comment lol

  • @joaquingutierrez7296
    @joaquingutierrez7296 Před 2 lety

    Can i change the color of scroll? How?

  • @greeneggsandmushrooms9855

    wombo combo

  • @Artimer
    @Artimer Před 3 lety +3

    how much do you program every day?

    • @BroCodez
      @BroCodez  Před 3 lety +1

      I mostly code exclusively for these videos almost daily, but I do have other projects I work on occasionally

  • @jaybartgis5148
    @jaybartgis5148 Před 3 lety

    4:25 so what if you want the getSelectedItem to show different text that corresponds to each string on the GUI? So let's say I want to click dog and I want the sysout to say "German Sheppard", how you do that?
    Thanks for the reply and your free educational videos

    • @AnthonyR007
      @AnthonyR007 Před 3 lety

      If statement, in the action listener, if the text is dog, output German shepard

    • @jaybartgis5148
      @jaybartgis5148 Před 3 lety

      @@AnthonyR007 so i did exactly that but now i have hundreds of if statements for every single dog breed and it looks totally all unprofessional and not "programmer like". I'm trying to make an array of dog breeds and just writing one if statements with the array number in the parameters

    • @jaybartgis5148
      @jaybartgis5148 Před 3 lety

      @@AnthonyR007 i can't quite get it to work because I'm not trained or a professional so i guess I'll just continue writing hundreds of if statements like a noob lol

    • @AnthonyR007
      @AnthonyR007 Před 3 lety

      @@jaybartgis5148 you could use a switch case statement with the indexes?

    • @AnthonyR007
      @AnthonyR007 Před 3 lety

      It’ll still be a lot of code, but it will be less resource intensive

  • @3r3h17
    @3r3h17 Před 2 lety

    Can I add a JComboBox on a JFrame with null Layout or a BorderLayout?

    • @Michel-dx1bn
      @Michel-dx1bn Před 2 lety +1

      Yes, in this video he used a border layout ( If you don't set a layout for a JFrame, it will use a BorderLayout by default )
      Also it works for null layout but don't forget to set the bounds and pack the frame.

  • @killermint_1662
    @killermint_1662 Před 3 lety +2

    Oh. So this isn’t minecraft

  • @user-kh6rp6yx1j
    @user-kh6rp6yx1j Před měsícem

    Hey.

  • @miguelosvaldofloresdomingu8911

    Hi, guys. Can someone help me with one situation I noticed? When I use the setEditable method and type one item that isn't the first one it executes the action performed method twice. This doesn't happen when I type in the comboBox the first Item.
    I'm goin' to let the code here in case anyone knows what is wrong here, and how can I make it work better.

    • @miguelosvaldofloresdomingu8911
      @miguelosvaldofloresdomingu8911 Před 6 měsíci

      import java.awt.FlowLayout;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import javax.swing.JComboBox;
      import javax.swing.JFrame;
      public class Main {
      public static void main (String [] args) {
      new MyFrame();
      }
      }
      class MyFrame extends JFrame implements ActionListener {
      JComboBox comboBox;
      MyFrame() {
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.setLayout(new FlowLayout());
      String [] animals = {"dog","cat","bird"};
      comboBox = new JComboBox(animals);
      comboBox.addActionListener(this);
      comboBox.setEditable(true);
      this.add(comboBox);
      this.pack();
      this.setVisible(true);
      }
      @Override
      public void actionPerformed(ActionEvent e) {
      if (e.getSource() == comboBox) {
      System.out.println(comboBox.getSelectedItem());
      System.out.println(comboBox.getSelectedIndex());
      }
      }
      }

  • @amr_badran2948
    @amr_badran2948 Před rokem

    hello

  • @wombozombo
    @wombozombo Před rokem

    Comment

  • @angelcastineira2561
    @angelcastineira2561 Před rokem

    🐽🐕🐈

  • @zstar8397
    @zstar8397 Před rokem

    Yo just wanna say that GOD loved the world so much he sent his only begotten
    son Jesus to die a brutal death for us so that we can have eternal life
    and we can all accept this amazing gift this by simply believing in him (Jesus) asking for the forgiveness of your sins
    and forming a relationship with heavenly father.

  • @joyceasante8292
    @joyceasante8292 Před rokem

    Practicing...
    public class Main{
    public static void main(String[]args){
    new MyFrame();
    }
    }
    *****************************
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class MyFrame extends JFrame implements ActionListener{
    JComboBox comboBox;

    MyFrame(){
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(new FlowLayout());
    String[]destinations = {"Zanzibar","Solomon Islands", "Saint Lucia"};
    //Integer[]destinations = {1,2,3};
    comboBox = new JComboBox();
    comboBox.addActionListener(this);
    //comboBox.setEditable(true);
    //System.out.println(comboBox.getItemCount());
    //comboBox.add("Senegal");
    //comboBox.insertItemAt("Turks and Caicos Islands",3);
    //comboBox.setSelectedIndex(3);
    //comboBox.removeItem("Saint Lucia");
    //comboBox.removeItemAt(0);
    comboBox.removeAllItems();

    this.add(comboBox);
    this.pack();
    this.setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent e){
    if(e.getSource()==comboBox){
    //System.out.println(comboBox.getSelectedItem());
    System.out.println(comboBox.getSelectedIndex());
    }
    }
    }

  • @izcilsboy5753
    @izcilsboy5753 Před 8 měsíci

    Thanks