Olá …
Sou iniciante em Java … (bem …pela pergunta da pra notar né rss)
Queria fazer com q minha ProgressBar começasse a correr assim que minha janela fosse aberta …
Achei varios exemplos, mas tudo com um botão para inicia-la…
Alguem pode me dar uma luz ?
Grato
pois então, o botão só faz algo iniciar…
copie o código que fica dentro do actionPerformed no final de seu método construtor…
Qualquer duvida posta novamente com um código para modificar-mos
Essa aqui é a classe com a progress
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JWindow;
import javax.swing.UIManager;
public class Progress extends JWindow {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JLabel jLabel = null;
private JProgressBar jProgressBar = null;
/**
* This is the default constructor
*/
public Progress() {
super();
initialize();
}
/**
Método Construtor
*/
private void initialize() {
this.setSize(349, 282);
UIManager.put("ProgressBar.selectionBackground", Color.black);
UIManager.put("ProgressBar.selectionForeground", Color.white);
UIManager.put("ProgressBar.foreground", new Color(8,32,128));
UIManager.put("ProgressBar.cellLength", new Integer(5));
UIManager.put("ProgressBar.cellSpacing", new Integer(1));
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension size = getSize();
if (size.width > screenSize.width)
size.width = screenSize.width;
if (size.height > screenSize.height)
size.height = screenSize.height;
this.setLocation((screenSize.width - size.width) / 2, (screenSize.height - size.height) / 2);
this.setContentPane(getJContentPane());
}
/**
Inicializa a Label
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel = new JLabel();
jLabel.setSize(new java.awt.Dimension(349,262));
jLabel.setLocation(new java.awt.Point(0,0));
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel, null);
jContentPane.add(getJProgressBar(), null);
}
return jContentPane;
}
/**
Inicializa a ProgressBar
*/
private JProgressBar getJProgressBar() {
if (jProgressBar == null) {
jProgressBar = new JProgressBar();
jProgressBar.setBounds(new java.awt.Rectangle(0,262,349,20));
jProgressBar.setMinimum(0);
jProgressBar.setMaximum(100);
jProgressBar.setStringPainted(true);
}
return jProgressBar;
}
}
Olha um exemplo do q vc quer…
/**
* @version 1.00 1999-07-17
* @author Cay Horstmann
*/
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
public class ProgressBarTest {
public static void main(String[] args)
{ JFrame frame = new ProgressBarFrame();
frame.show();
}
}
class ProgressBarFrame extends JFrame
{ public ProgressBarFrame()
{ setTitle("ProgressBarTest");
setSize(300, 200);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
} );
Container contentPane = getContentPane();
// this text area holds the activity output
textArea = new JTextArea();
// set up panel with button and progress bar
JPanel panel = new JPanel();
startButton = new JButton("Start");
progressBar = new JProgressBar();
progressBar.setStringPainted(true);
panel.add(startButton);
panel.add(progressBar);
contentPane.add(new JScrollPane(textArea), "Center");
contentPane.add(panel, "South");
// set up the button action
startButton.addActionListener(
new ActionListener()
{ public void actionPerformed(ActionEvent event)
{ progressBar.setMaximum(1000);
activity = new SimulatedActivity(1000);
activity.start();
activityMonitor.start();
startButton.setEnabled(false);
}
});
// set up the timer action
activityMonitor = new javax.swing.Timer(500,
new ActionListener()
{ public void actionPerformed(ActionEvent event)
{ int current = activity.getCurrent();
// show progress
textArea.append(current + "\n");
progressBar.setValue(current);
// check if task is completed
if (current == activity.getTarget())
{ activityMonitor.stop();
startButton.setEnabled(true);
}
}
});
}
private javax.swing.Timer activityMonitor;
private JButton startButton;
private JProgressBar progressBar;
private JTextArea textArea;
private SimulatedActivity activity;
}
class SimulatedActivity extends Thread
{ public SimulatedActivity(int t)
{ current = 0;
target = t;
}
public int getTarget()
{ return target;
}
public int getCurrent()
{ return current;
}
public void run()
{ while (current < target && !interrupted())
{ try
{ sleep(100);
}
catch(InterruptedException e)
{ return;
}
current++;
}
}
private int current;
private int target;
}
Com esse exemplo dá pra adaptar do jeito que quiser , gostei, to criando um software aqui e adaptei nele. Fico massa.