Java progress bar 📊

Поділитися
Вставка
  • Опубліковано 22 січ 2025

КОМЕНТАРІ • 93

  • @BroCodez
    @BroCodez  4 роки тому +36

    Guys I accidentally cut out the part about displaying current/total HP.
    You would probably want to add a line such as:
    bar.setString(currentHP+"/"+maximumHP+" HP");
    //******************************************************************
    public class Main{


    public static void main(String[] args)
    {

    ProgressBarDemo demo = new ProgressBarDemo();

    }

    }
    //******************************************************************
    import java.awt.*;
    import javax.swing.*;
    public class ProgressBarDemo {
    JFrame frame = new JFrame();
    JProgressBar bar = new JProgressBar(0,100);

    ProgressBarDemo(){

    bar.setValue(0);
    bar.setBounds(0,0,420,50);
    bar.setStringPainted(true);
    bar.setFont(new Font("MV Boli",Font.BOLD,25));
    bar.setForeground(Color.red);
    bar.setBackground(Color.black);

    frame.add(bar);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(420, 420);
    frame.setLayout(null);
    frame.setVisible(true);

    fill();
    }

    public void fill() {
    int counter =0;

    while(counter

    • @goodgamershow6505
      @goodgamershow6505 4 роки тому

      currentHP and maxHP variables doesn't exit error

    • @sanskarsongara2592
      @sanskarsongara2592 4 роки тому

      @@goodgamershow6505 if they doesn't exist then just create em

    • @mozd1729
      @mozd1729 3 роки тому

      @@goodgamershow6505 Are you like new to programming? Create and declare the variables fool!

    • @luke17.10
      @luke17.10 2 роки тому +1

      bar.setString(counter+"/" +"500"+" HP");

    • @mahimasingh4791
      @mahimasingh4791 2 роки тому +1

      @@luke17.10 Awesome thanks :)

  • @EmperorVenn
    @EmperorVenn 4 роки тому +16

    Maaaaaan... you actually need more attention, amazing, thanks for the hard work dude!
    Have you never subscribed to me never would i have found your tutorials.
    Just wanting to become a software developer and learning python for now and next would be java.
    I have plans to become a software developer in a year so your tutorials are perfect. Once again, thanks!

    • @BroCodez
      @BroCodez  4 роки тому +6

      thanks for watching! I wish you luck on your pursuits!

    • @EmperorVenn
      @EmperorVenn 4 роки тому +3

      @@BroCodez My pleasure! And thank you!

  • @cannac6995
    @cannac6995 3 роки тому +8

    I always hate loading bars in video games, but i like loading bars to my game :)
    Thank you for tutorial!

  • @puppylili
    @puppylili 3 роки тому +4

    You really should get a patreon, there'll be so many people willing to even donate a single dollar just so you can make your already great content 100x better

  • @pavelkvasnicka6856
    @pavelkvasnicka6856 Рік тому +1

    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

  • @paraparasolian6484
    @paraparasolian6484 3 роки тому

    One of the rare java tutorials that is actually enjoyable because I understand everything!

  • @amikoss5635
    @amikoss5635 Рік тому

    Bro, your tutorials are simply perfect!

  • @noah77
    @noah77 4 роки тому +3

    Please make a tutorial for a cool animation in java.

  • @speklex779
    @speklex779 Рік тому +1

    Nice video

  • @NovusTM
    @NovusTM 4 роки тому +3

    One day Bro code will learn unity and make unity tutorials! Keep doing what ur doing, your amazing man!

    • @BroCodez
      @BroCodez  4 роки тому +2

      no promises on Unity lol

  • @shaynazoedeguzman7418
    @shaynazoedeguzman7418 2 роки тому

    Superb. Hats off to you sir! Stay awesome!

  • @kemann3815
    @kemann3815 3 роки тому

    Well done

  • @salaheddine6556
    @salaheddine6556 4 роки тому +2

    If foregound doesn't change the font color in Progress Bar , so how do we change its color ?

  • @aleksandarkalanj1012
    @aleksandarkalanj1012 3 роки тому

    Thanks for all this videos u create, u helped me a lot! Nice job bro...

  • @stevancosovic4706
    @stevancosovic4706 3 роки тому +7

    If anyone has problem with JProgressBar showing up until it reaches 100%, here is how I fixed it. You need to create a class which extends Thread class. In this class you should have JProgressBar which can be updated in redefined run method. Also a start button in GUI, here's the example:
    //Creating a child class of Thread class
    public class ThreadBar extends Thread
    {
    JProgressBar bar;

    public ThreadBar(JProgressBar newBar)
    {
    this.bar = newBar;
    }

    @Override
    public void run()
    {
    int percentage = 0;
    while (percentage < 100)
    {
    try
    {
    sleep(1000);
    }
    catch (InterruptedException e)
    {
    return;
    }
    percentage += 10;
    bar.setValue(percentage);
    }
    }
    //Starting the Thread "tb" with "Start" button on GUI
    public JButton getStartButton()
    {
    if (startButton == null)
    {
    startButton = new JButton("START");
    startButton.setBounds(90, 127, 225, 38);
    StartButtonEvent sbe = new ButtonEvent(); //Event object created in sub-class below
    startButton.addActionListener(sbe); //Adding the object to the ActionListener
    }
    return startButton;
    }

    private class StartButtonEvent implements ActionListener
    {
    @Override
    public void actionPerformed(ActionEvent e)
    {
    ThreadBar tb = new ThreadBar(getBar()); //Our thread
    tb.start(); //Calls the redefined "run" method above
    }
    }
    P.S. I hope it was helpful

    • @autismoprime7293
      @autismoprime7293 2 роки тому

      Thank you so much dude

    • @rectaalborween8471
      @rectaalborween8471 11 місяців тому

      I also fixed this by running setVisible(true) inside the method
      public void fillBar() {
      int count = 0;
      setVisible(true);
      though i really dont understand why it works XD

  • @Soyosan22
    @Soyosan22 2 роки тому +3

    In Mac it doesn't change the foreground and background colours even though I use the methods in the video. :(

  • @noah77
    @noah77 4 роки тому +1

    Cool, thanks for the tutorial

  • @kaushalprasadyadav9242
    @kaushalprasadyadav9242 2 роки тому

    Great work bro. Please make video for mobile app development.....
    It will very helpful for us...

  • @kemann3815
    @kemann3815 3 роки тому

    Awesome

  • @aryanmehta1904
    @aryanmehta1904 2 роки тому +2

    Nice Tutorial
    Can you tell me how to change the font color in the progerssbar???

  • @gauravtak3191
    @gauravtak3191 4 роки тому +1

    Nice Explain sir

    • @BroCodez
      @BroCodez  4 роки тому +2

      thanks for watching gauruv

  • @attalaw36
    @attalaw36 3 роки тому

    you are best teacher

  • @MmdRsh
    @MmdRsh Рік тому

    good luck

  • @ankitparmar4331
    @ankitparmar4331 2 роки тому

    You're awesome bro

  • @nawfalnjm5699
    @nawfalnjm5699 3 роки тому

    awesome

  • @hussenbarzani
    @hussenbarzani 2 місяці тому

    best course

  • @girl6994
    @girl6994 4 роки тому +5

    Thank you so much. You always cost your time to read every comment or questions of us. And thanks to other people who watches this channel too, because they gave me some advice too. And I tried make videos on Chinese platform about how to learning Japanese and how to code or what game I enjoy. But I felt very unhappy because I don’t know why lots of people laughs me, make fun of me, insults me and I didn’t offend them. But I love coding, I will not give up it because it is really fun and I love learning other languages too. I just want to give up making videos on China’s platform, I want to delete all videos I made because it gives me bad memories but I also don’t want because I spend much time on it. But still I loves making videos. I will upload my videos on UA-cam someday.

    • @BroCodez
      @BroCodez  4 роки тому +5

      I would like to watch your videos if you upload them. People will insult you no matter what if you make yourself public, even if you do everything perfect. I get insulted all the time and I only teach coding. Some people just love to hurt others. Don't take it personal.

  • @henriquefelinimena3693
    @henriquefelinimena3693 3 роки тому

    Thank you for the content

  • @TheFanfan7
    @TheFanfan7 2 роки тому

    AWESOME !!

  • @lukaivanic9798
    @lukaivanic9798 4 роки тому +3

    Could you explain how to make the fill function on a button click, I am asking because I have a for loop with repaint(), when I run the function normally (on the start of the program like you) it works perfectly, but when I activate it on a button click it "freezes" and repaints only when the for loop is done, any clue how to fix this?

    • @BroCodez
      @BroCodez  4 роки тому

      You could write something like this. Have a method that is called with a button click
      public void actionPerformed(ActionEvent e) {
      if(e.getSource == button){
      bar.fill();
      }
      }
      public void fill(){
      bar.setValue(current+x);
      }

    • @lukaivanic9798
      @lukaivanic9798 4 роки тому

      @@BroCodez I got that, the problem is when I put a for loop which has repaint() in the function, it's a common problem but none of the solutions worked for me

    • @BroCodez
      @BroCodez  4 роки тому

      hmmm I'm having a difficult time visualizing this. Could you post the code that you have?

    • @lukaivanic9798
      @lukaivanic9798 4 роки тому

      @@BroCodez I posted a question so I don't post here chaotically:stackoverflow.com/questions/62679294/jpanel-only-updates-after-the-for-loop-is-finished
      Also this is the whole code so you can easily run it. Thanks for the help

    • @lukaivanic9798
      @lukaivanic9798 4 роки тому +1

      @@BroCodez I solved it by putting a swing worker around the function, I edited the stack overflow question to the working code, thanks for the help tho!

  • @pavelmartchenkov7740
    @pavelmartchenkov7740 4 роки тому +2

    frame.setSize(420, 420) //i like your style bro

  • @albithesecond3921
    @albithesecond3921 2 роки тому

    My program freezes when the progress bar starts and unfreezes when the progress bar is done

  • @ibrahimylmaz8378
    @ibrahimylmaz8378 2 роки тому

    thanks bro

  • @majid1885
    @majid1885 3 роки тому

    Thank you very much

  • @aoiumi1864
    @aoiumi1864 2 роки тому

    Absolute Best! Hey Its a really nice way of teaching ! do you have any idea how do i do something like this , lets say , only when i click on a button then the ProgressBar will start running , because in this tutorial it start immediately when we run the program! you are doing the god works! :D Bro Code!

  • @d.v.v562
    @d.v.v562 2 роки тому

    Hey bro Im busy learning Java from your videos and firstly just want to say thanx bro😂 but my actual question is how do u know what to import and what not🤷🏼‍♂️

  • @kump3l27
    @kump3l27 Рік тому

    why can't I see a frame until the progress bar is 100%?

  • @filip6172
    @filip6172 3 роки тому

    how can I set my bar to not show me percentages, i want to see only number 20/100 for example

  • @ratanrathod7
    @ratanrathod7 Рік тому

    Thank you brother ♥

  • @neiimoycrack1330
    @neiimoycrack1330 4 роки тому +1

    Why my jprogressbar not showing the progress. When run the frame not show up but after a few second it show 100% progressbar?

  • @joycepepito7545
    @joycepepito7545 4 роки тому

    can progress bar stay at the center of the jframe? if its possible, i think it will look better.

    • @BroCodez
      @BroCodez  4 роки тому +1

      You can but it depends on the layout manager you're using. I would recommend creating a JPanel and adding the progress bar to the JPanel. If you're using no layout manager, this should do the trick:
      final int FRAME_HEIGHT = 600;
      final int FRAME_WIDTH = 600;
      final int BAR_WIDTH = 200;
      final int BAR_HEIGHT = 50;
      bar.setBounds(
      (FRAME_WIDTH/2)-(BAR_WIDTH/2),
      (FRAME_HEIGHT/2)-(BAR_HEIGHT/2),
      BAR_WIDTH,
      BAR_HEIGHT);
      //...
      panel.setSize(FRAME_WIDTH , FRAME_HEIGHT );

  • @akshaykumarsharma
    @akshaykumarsharma 4 роки тому +1

    how to set text color in JProgressBar

    • @BroCodez
      @BroCodez  4 роки тому

      bar.setForeground(Color.red);

    • @paraparasolian6484
      @paraparasolian6484 3 роки тому

      @@BroCodez Yes, but bar.setForeground sets the bar color, not the text color, right? I see from the video that the text color is (looks) blue against the white background but (looks) white against the red background when the bar becomes bigger. Can you set (fix) the font color independent of the bar color?

  • @Yuipser
    @Yuipser 2 роки тому +1

    I cant change the foreground color and background color even though I copy the same code. I am coding on mac Eclipse, anyone knows why?

  • @UnrealSL
    @UnrealSL 3 роки тому

    Please Help me.. I'm New to Java. How to put this Progress bar to bottom??

  • @EdgeLordOmen
    @EdgeLordOmen 8 місяців тому

    Thank you bro code

  • @MrLoser-ks2xn
    @MrLoser-ks2xn 2 роки тому

    Thanks

  • @kazlee6909
    @kazlee6909 3 роки тому +1

    Mine only updates visually when the loop completes. Anyone know how to fix this?

  • @leviplays7466
    @leviplays7466 10 місяців тому

    You're the Bro

  • @angelcastineira2561
    @angelcastineira2561 4 роки тому

    thanks!

  • @augischadiegils.5109
    @augischadiegils.5109 4 роки тому

    Thanks bro:)

  • @robertraafat413
    @robertraafat413 3 роки тому

    Bro! How can I remove the percentage sign

  • @kukuuu9855
    @kukuuu9855 3 роки тому +1

    hi bro ,I follow all of your code ,but my progressbar's color can't change ,how to solve this question ,thx!!!!

    • @majdhajhmedy5590
      @majdhajhmedy5590 3 роки тому +1

      Brother, if you have any questions go ask it on stackoverflow.com, youtubers have so much to do, and they don't have time, and also stack overflow is a large community so programmers would respond in minutes, wish you the best!

  • @prinzmarkiii
    @prinzmarkiii 4 роки тому

    Hello
    So i just wanna Write a easy Programm which works like this
    You press on a Button and the progressbar Starts filling. The filling process should be 10 sec.
    My Problem is that the Animation isnt working. So its just from 0 to 100% with a 10 sec delay. Can you help me out?

    • @kazlee6909
      @kazlee6909 3 роки тому

      I have the same problem

  • @eugenezuev7349
    @eugenezuev7349 8 місяців тому

    that's cool

  • @nehalayaaz9406
    @nehalayaaz9406 4 роки тому

    Thanks 🌸🤍🌸

  • @coderxgamer
    @coderxgamer 3 роки тому

    I guess were making progress

  • @user-tt1tv4qz2o
    @user-tt1tv4qz2o 3 роки тому

    bro i do exactly in video but the progress bar is only in 100 percent

    • @riba5245
      @riba5245 2 роки тому

      @@eternalexcalibur run method problem...guys you need to learn alone..

  • @tomaszkarolak6179
    @tomaszkarolak6179 4 роки тому

    Comment down below ...

  • @antoinealam6531
    @antoinealam6531 4 місяці тому

    bro

  • @zari_723
    @zari_723 2 роки тому

    hi

  • @greeneggsandmushrooms9855
    @greeneggsandmushrooms9855 3 роки тому

    i made progress

  • @honoredegg
    @honoredegg 2 роки тому

    64th. Thank you, ma Bro Sensei!