Slight SwingWorker Update problem
Hi,
I've recieved help from a number of people regarding the development
of this app, and for this I am grateful. I've learned loads about
Java GUI from this project.
I'm at a stage now, where I'm using SwingWorker to control GUI
updates, and it mostly works. A progress bar is updates according to
the int value sent from SwingWorker, as is the frame title, but, even
though when I debug this int value is sent to the UpdateCAGrid method,
it is not changing the colour of the panel (the int is used as an
index for an array). Initially, UpdateCAGrid is called (before swing
worker calls it), and if I hard-code different values into this
argument, the panel initially appears in the relevant colour, but
doesn't when SwingWorker calls it.
I can't really see what would inhibit this, and am hoping someone will
point out a silly mistake on my part.
Many thanks,
JNY0
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Main{
//ManuBar
static JMenuBar menuBar = new JMenuBar();
static JMenu file = new JMenu("File");
static JMenuItem getData = new JMenuItem("Get Data");
//Other GUI Elements
static JLabel lblDataAcquired = new JLabel();
static JPanel caPanel = new JPanel();
static JPanel outerPanel = new JPanel();
static JPanel buttonPanel = new JPanel();
static JPanel controlPanel = new JPanel();
static JProgressBar progressBar = new JProgressBar(0, 10);
static JFrame frame = new JFrame();
public static void main(String[] args) {
caPanel.setName("CA Panal");
//menu bar
menuBar.add(file);
file.add(getData);
frame.setJMenuBar(menuBar);
lblDataAcquired.setText("No Data");
//GetData from menu bar under File
getData.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
System.out.println("You have clicked on the new action");
frame.setTitle("Hello");
//GetData();
}
});
//Updating the progress bar - SwingWorker
final CounterTask task = new CounterTask();
task.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
if ("progress".equals(evt.getPropertyName())) {
progressBar.setValue((Integer) evt.getNewValue());
frame.setTitle((String.valueOf((Integer) evt.getNewValue
())));
updateCAGrid((Integer) evt.getNewValue());
}
}
});
//Start button press - SwingWorker
JButton startButton = new JButton("Start");
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
task.execute();
}
});
//Stop button press - SwingWorker
JButton cancelButton = new JButton("Stop");
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
task.cancel(true);
}
});
GridLayout outerLayout = new GridLayout(2,1);
GroupLayout caPanalLayout = new GroupLayout(caPanel);
GridLayout controlLayout = new GridLayout(3,1);
outerPanel.setLayout(outerLayout);
caPanel.setLayout(null);
controlPanel.setLayout(controlLayout);
buttonPanel.setBackground( Color.GREEN );
progressBar.setBackground( Color.BLUE );
controlPanel.add(progressBar);
controlPanel.add(buttonPanel);
controlPanel.add(lblDataAcquired);
outerPanel.add(controlPanel);
buttonPanel.add(startButton);
buttonPanel.add(cancelButton);
controlPanel.setSize(50, 50);
updateCAGrid(1);
}
public static void updateCAGrid(int iNumber)
{
Color colors[] = { Color.WHITE, Color.RED, Color.BLUE, Color.GREEN,
Color.YELLOW , Color.CYAN, Color.MAGENTA, Color.DARK_GRAY, Color.PINK,
Color.LIGHT_GRAY, Color.WHITE};
JPanel tempPanel = new JPanel();
tempPanel.setBackground(colors[iNumber]);
tempPanel.setSize(10, 10);
tempPanel.setVisible(true);
caPanel.add(tempPanel);
caPanel.setVisible(true);
tempPanel.repaint();
caPanel.repaint();
caPanel.setSize(10, 10);
outerPanel.add(caPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(outerPanel);
frame.setSize(1000, 1000);
frame.setVisible(true);
}
}
class CounterTask extends SwingWorker<Integer, Integer> {
int DELAY = 1000;
@Override
protected Integer doInBackground() throws Exception {
int i = 0;
int count = 10;
while (!isCancelled() && i < count) {
i++;
publish(new Integer[] { i });
setProgress(count * i / count);
Thread.sleep(DELAY);
}
return count;
}
protected void process(List<Integer> chunks) {
System.out.println(chunks);
}
@Override
protected void done() {
if (isCancelled())
System.out.println("Cancelled !");
else
System.out.println("Done !");
}
}