Solved

Progress bar with a TextField (JDK 1.0.2)

Posted on 1998-12-14
1
252 Views
Last Modified: 2012-06-21
Hi, right now I'm trying to build a simple Progress bar using a TextField (with JDK 1.0.2), it can fill well, but I don't know how to drain the StringBuffer content for refill it. This is what I have:

import java.awt.*;
import java.lang.*;
import java.util.*;
import java.applet.*;
import java.io.*;

public class miBarraProg2 extends java.applet.Applet {
  TextField progreso;
  Button inicio;
  Label porcen1;
  Label porcen2;
 
  StringBuffer llena;
  String llena2;
  String agrega;
 
  public void init() {
    llena = new StringBuffer(40);
    llena2 = "";
    agrega = "|";
   
    progreso = new TextField(8);
    progreso.setEditable(false);
    inicio = new Button("Start the progress!");
    porcen1 = new Label("0% ");
    porcen2 = new Label("100%");
    add(porcen1);
    add(progreso);
    add(porcen2);
    add(inicio);
  }
 
  public boolean action (Event evt, Object what) {
    Object target=evt.target;
        if(target == inicio) {
        progreso.setText("");
       
        llena2 = "";
        //How do I drain the content of the StringBuffer?
       
         for (int x=1; x<30; x++) {
          llena2 = llena.append(agrega).toString();
          progreso.setText(llena2);
          for (int y=1; y<200; y++) {}  //a delay
         }
       return true;
       }
   return false;
  }  

}

Could somebody help me?. Thanks.

  Deladier.
0
Comment
Question by:Deladier
1 Comment
 
LVL 1

Accepted Solution

by:
Hans_Klose earned 100 total points
ID: 1229506
public class miBarraProg2 extends java.applet.Applet
{
  TextField progreso;
  Button inicio;
  Label porcen1;
  Label porcen2;
   
  StringBuffer llena;
  String llena2;
  String agrega;
   
  public void init()
  {
    llena = new StringBuffer(40); // ? Why did you make it 40  
    llena2 = "";
    agrega = "|";
     
    progreso = new TextField(8);
    progreso.setEditable(false);
    inicio = new Button("Start the progress!");
    porcen1 = new Label("0% ");
    porcen2 = new Label("100%");
    add(porcen1);
    add(progreso);
    add(porcen2);
    add(inicio);
  }
   
  public boolean action (Event evt, Object what)
  {
    Object target=evt.target;
        if(target == inicio)
        {
           
                progreso.setText("");
         
                llena2 = "";
                //How do I drain the content of the StringBuffer?
         
                for (int x=1; x<30; x++)
                {
                    llena2 = llena.append(agrega).toString();
                    progreso.setText(llena2);
         
                    for (int y=1; y<900; y++) {}  //a delay
                }
           
           
            //  Here is a quick hack of what I think you could do.
            //  1. Start with a full buffer at the last position
            //  2. while you are above or equal to 0 change the char at x to space
            // Just be carefull with your indexes since you start at 1.
           
         
                System.out.println("Length of a buffer "+llena.length());
                for(int x= 28 ; x >= 0; x--)
                {
                    System.out.println("Here is x" + new Integer(x).toString() );
                   
                    llena.setCharAt(x,' ');
                    llena2 = llena.toString();
                    progreso.setText(llena2);
         
                    for (int y=1; y<900; y++) {}  //a delay    
                }
   
                return true;
               
    }
   return false;
 
  }
}

// I hope this works with 1.0.2 I have 1.2 but I attempted to make only the 1.0.2 api calls
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now