JProgressBar (Another Example)

If you can control whether the contents of a text field may be modified by the user by calling setEditable( ). In setEditable( ), if value is true, the text may be changed. If it is false, the text cannot be altered.

Program

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JProgressBar;
import javax.swing.JOptionPane;
import javax.swing.BoxLayout;
import javax.swing.BorderFactory;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.FlowLayout;

class JProgressBarTest extends JFrame implements ActionListener{
   
    private JProgressBar jp;
    private JButton[] dataArray = new JButton[10];
    private JPanel p1;
    private JTextField jt;
    private int arrayCount = 0 ;
    private int jprogressBarUPDATE = 0 ;
   
    public JProgressBarTest()
    {
        setLayout(new FlowLayout());
        setTitle("JProgressBar");
        setJTextField();
        setJProgressBar();
        setJButtons();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(700,350);
        setResizable(false);
        setVisible(true);
    }
   
    void setJTextField()
    {
        JLabel g = new JLabel("Write the data into text filed");
        add(g);
        jt = new JTextField(10);
        jt.addActionListener(this);
        add(jt);
    }
   
    void setJProgressBar()
    {
        jp = new JProgressBar();
        jp.setMinimum(0);
        jp.setMaximum(100);
        jp.setStringPainted(true);
        jp.setBackground(Color.white);
        add(jp);
    }
   
    void setJButtons()
    {
        p1 = new JPanel();
        p1.setLayout(new BoxLayout(p1,BoxLayout.Y_AXIS));
        p1.setBorder(BorderFactory.createTitledBorder("Data Array"));
        for(int i=0;i<10;i++)
        {
            dataArray[i]= new JButton();
            dataArray[i].setText("["+i+"] = 0");
            p1.add(dataArray[i]);
        }
        add(p1);
    }
   
    public void actionPerformed(ActionEvent eve)
    {
        if(arrayCount<10)
        {
            dataArray[arrayCount].setText("["+arrayCount+"] = "+jt.getText());
            arrayCount++;
            jprogressBarUPDATE+=10;
            jp.setValue(jprogressBarUPDATE);
        }else{
            jp.setString("Done");
            JOptionPane.showMessageDialog(rootPane,"Data array is full");
            jt.setEditable(false);
        }
    }
}

public class Javaapp {
   
    public static void main(String[] args) {
       
        JProgressBarTest jpr = new JProgressBarTest();
    }
}


Program Output

Leave a Comment