What I have to do is make a little GUI that when a button is clicked it starts to progress bars with threads; however, we have to do it with an inner class that implements the Runnable interface, and within the constructor for that class, it takes a String and a JProgressBar object. Then within the main constructor, we are supposed to instantiate 2 JProgressBar objects and then create 2 of the Inner class objects using those JProgressBars.
Only my second JProgressBar is updating with the thread. I know that my second JProgressBar is basically overriding my first one, but I don't know how I would go to fix that, because if I try to change the inner class's constructor to setting 2 JProgressBar attributes equal to the parameter of the constructor, then the first JProgessBar just completely disappears from the GUI. Here is my code, thank you to anyone that can help.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Lab4Part2 {
//Attributes
private JFrame frame;
private String progress;
private JProgressBar jpb;
//Constructor
public Lab4Part2() {
//Create frame with specified grid layout
frame = new JFrame();
GridLayout grid = new GridLayout(0,1);
frame.setLayout(grid);
JButton jbClick = new JButton("Let's start this show");
frame.add(jbClick);
//Add in JProgressBars and create Inner Class objects with them
JProgressBar jpb1 = new JProgressBar();
JProgressBar jpb2 = new JProgressBar();
InnerProgress bar1 = new InnerProgress("Progress 1: ", jpb1);
InnerProgress bar2 = new InnerProgress("Progress 2: ", jpb2);
//Anonymous class for the button's action listener
jbClick.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
Thread t1 = new Thread(bar1);
Thread t2 = new Thread(bar2);
t1.start();
t2.start();
}
});
//Adds inner object to frame
frame.add(bar1);
frame.add(bar2);
//Packing and stuff
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
//Inner class
class InnerProgress extends JPanel implements Runnable {
//Constructor
public InnerProgress(String pbar, JProgressBar jpBar) {
jpBar.setMinimum(0);
jpBar.setMaximum(80);
jpBar.setStringPainted(true);
progress = pbar;
jpb = jpBar;
JLabel label = new JLabel(pbar);
add(label);
add(jpBar);
}
//Thread action
public void run() {
System.out.println("We are running " + Thread.currentThread().getName());
for(int i = 1; i<= 80; i++) {
try {
Thread.currentThread().sleep((long)Math.random()*100);
jpb.setValue(i);
}
catch(InterruptedException ie) {
System.out.println(ie);
}
}
System.out.println("Thread name: " + Thread.currentThread().getName() + " " + System.currentTimeMillis());
}
}
//Main
public static void main(String[] args) {
new Lab4Part2();
}
}
Your inner class should maintain it's own reference to the JProgressBar that it is passed.
Simply take...
private JProgressBar jpb;
and move it inside your inner class
class InnerProgress extends JPanel implements Runnable {
private JProgressBar jpb;
//...
So, two other issues
One, (long)Math.random()*100 is causing the result of Math.random() to be case to an int, which will result in it becoming 0.
You need to cast the result of the operation, for example...
Thread.currentThread().sleep((int)(Math.random() * 500));
(nb: I made it 500 for my testing)
Two, Swing is not thread safe. This means you should not be updating the UI from outside of the context of the Event Dispatching Thread.
So, instead of...
jpb.setValue(i);
I wrote a new method which will update the UI accordingly
protected void updateProgress(int value) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
InnerProgress.this.jpb.setValue(value);
}
});
}
(Long and short, this over comes issues with referencing variables from inside an anonymous context)
Then simply called it using updateProgress(i);
Runnable example...
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
public class Lab4Part2 {
//Attributes
private JFrame frame;
private String progress;
//Constructor
public Lab4Part2() {
//Create frame with specified grid layout
frame = new JFrame();
GridLayout grid = new GridLayout(0, 1);
frame.setLayout(grid);
JButton jbClick = new JButton("Let's start this show");
frame.add(jbClick);
//Add in JProgressBars and create Inner Class objects with them
JProgressBar jpb1 = new JProgressBar();
JProgressBar jpb2 = new JProgressBar();
InnerProgress bar1 = new InnerProgress("Progress 1: ", jpb1);
InnerProgress bar2 = new InnerProgress("Progress 2: ", jpb2);
//Anonymous class for the button's action listener
jbClick.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
Thread t1 = new Thread(bar1);
Thread t2 = new Thread(bar2);
t1.start();
t2.start();
}
});
//Adds inner object to frame
frame.add(bar1);
frame.add(bar2);
//Packing and stuff
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
//Inner class
class InnerProgress extends JPanel implements Runnable {
private JProgressBar jpb;
//Constructor
public InnerProgress(String pbar, JProgressBar jpBar) {
jpBar.setMinimum(0);
jpBar.setMaximum(80);
jpBar.setStringPainted(true);
progress = pbar;
jpb = jpBar;
JLabel label = new JLabel(pbar);
add(label);
add(jpBar);
}
//Thread action
public void run() {
System.out.println("We are running " + Thread.currentThread().getName());
for (int i = 1; i <= 80; i++) {
try {
Thread.currentThread().sleep((int)(Math.random() * 500));
updateProgress(i);
} catch (InterruptedException ie) {
System.out.println(ie);
}
}
System.out.println("Thread name: " + Thread.currentThread().getName() + " " + System.currentTimeMillis());
}
protected void updateProgress(int value) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
InnerProgress.this.jpb.setValue(value);
}
});
}
}
//Main
public static void main(String[] args) {
new Lab4Part2();
}
}
Related
When I start my application it opens a JFrame (the main window) and a JFilechooser to select an input directory, which is then scanned.
The scan method itself creates a new JFrame which contains a JButton and a JProgressBar and starts a new Thread which scans the selected Directory. Up until this point everything works fine.
Now I change the Directory Path in my Main Window, which calls the scan method again. This time it creates another JFrame which should contain the JProgressBar and the JButton but it shows up empty (The JFrame Title is still set).
update:
minimal example
public class MainWindow
{
private JFrame _frame;
private JTextArea _textArea;
private ProgressBar _progress;
public MainWindow() throws InterruptedException, ExecutionException
{
_frame = new JFrame("Main Window");
_textArea = new JTextArea();
_frame.add(_textArea);
_frame.setSize(200, 200);
_frame.setVisible(true);
_textArea.setText(doStuffinBackground());
_progress.dispose();
}
private String doStuffinBackground() throws InterruptedException,
ExecutionException
{
setUpProgressBar();
ScanWorker scanWorker = new ScanWorker();
scanWorker.execute();
return scanWorker.get();
}
private void setUpProgressBar()
{
// Display progress bar
_progress = new ProgressBar();
}
class ProgressBar extends JFrame
{
public ProgressBar()
{
super();
JProgressBar progressBar = new JProgressBar();
progressBar.setIndeterminate(true);
progressBar.setStringPainted(false);
add(progressBar);
setTitle("Progress Window");
setSize(200, 200);
toFront();
setVisible(true);
}
}
class ScanWorker extends SwingWorker<String, Void>
{
#Override
public String doInBackground() throws InterruptedException
{
int j = 0;
for (int i = 0; i < 10; i++)
{
Thread.sleep(1000);
j += 1;
}
return String.valueOf(j);
}
}
public static void main(String[] args) throws InvocationTargetException,
InterruptedException
{
SwingUtilities.invokeAndWait(new Runnable()
{
public void run()
{
// Start the main controller
try
{
new MainWindow();
}
catch (InterruptedException | ExecutionException e) {}
}
});
}
}
From the basic looks of your scan method, you are blocking the Event Dispatching Thread, when you scan the directory, which is preventing it from updating the UI.
Specifically, you don't seem to truly understand what Callable and FutureTask are actually used for or how to use them properly...
Calling FutureTask#run will call the Callable's call method...from within the current thread context.
Take a look at Concurrency in Swing for more details...
Instead of trying to use FutureTask and Callable in this manner, consider using a SwingWorker, which is designed to do this kind of work (and uses Callable and FutureTask internally)
Have a look at Worker Threads and SwingWorker for more details
Now, before you jump down my throat and tell me that "it works the first time I ran it", that's because you're not starting your UI properly. All Swing UI's should be create and manipulated from within the context of the Event Dispatching Thread. You main method is executed in, what is commonly called, the "main thread", which is not the same as the EDT. This is basically setting up fluke situation in where the first time you call scan, you are not running within the context of the EDT, allowing it to work ... and breaking the single thread rules of Swing in the process...
Take a look at Initial Threads for more details...
I would also consider using a JDialog instead of another frame, even if it's not modal, it makes for a better paradigm for your application, as it really should only have a single main frame.
Updated based on new code
So, basically, return scanWorker.get(); is a blocking call. It will wait until the doInBackground method completes, which means it's block the EDT, still...'
Instead, you should be making use of the publish, process and/or done methods of the SwingWorker
import java.util.ArrayList;
import java.util.List;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
public class MainWindow {
private JFrame _frame;
private JTextArea _textArea;
private ProgressBar _progress;
public MainWindow() {
_frame = new JFrame("Main Window");
_textArea = new JTextArea();
_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
_frame.add(new JScrollPane(_textArea));
_frame.setSize(200, 200);;
_frame.setVisible(true);
doStuffinBackground();
}
private void doStuffinBackground() {
// _progress = new ProgressBar();
// ScanWorker scanWorker = new ScanWorker();
// scanWorker.execute();
// return scanWorker.get();
_progress = new ProgressBar();
ScanWorker worker = new ScanWorker(_textArea, _progress);
worker.execute();
_progress.setVisible(true);
}
class ProgressBar extends JDialog {
public ProgressBar() {
super(_frame, "Scanning", true);
JProgressBar progressBar = new JProgressBar();
progressBar.setIndeterminate(true);
progressBar.setStringPainted(false);
add(progressBar);
setTitle("Progress Window");
pack();
setLocationRelativeTo(_frame);
}
}
class ScanWorker extends SwingWorker<List<String>, String> {
private JTextArea textArea;
private ProgressBar progressBar;
protected ScanWorker(JTextArea _textArea, ProgressBar _progress) {
this.textArea = _textArea;
this.progressBar = _progress;
}
#Override
protected void process(List<String> chunks) {
for (String value : chunks) {
textArea.append(value + "\n");
}
}
#Override
public List<String> doInBackground() throws Exception {
System.out.println("...");
int j = 0;
List<String> results = new ArrayList<>(25);
for (int i = 0; i < 10; i++) {
Thread.sleep(1000);
j += 1;
System.out.println(j);
results.add(Integer.toString(j));
publish(Integer.toString(j));
}
return results;
}
#Override
protected void done() {
progressBar.dispose();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MainWindow();
}
});
}
}
I'm trying to create a pop up dialog progress bar preferably an Indeterminate but that's not too important. I have been looking through "Oracle's ProgressBar Tutorials" and Google searching but not such luck in getting it to work. I'm pasted my code below of my Action Listener and the dialog will not pop up. Any suggestions? Thanks in advance!
Sorry this is my first post on this site. But how it works is that When I press the create button, it goes out and grab some information from different servers and directories and creates a file for me. That is what the new Project is. Features is a Enumeration I made that are set with the text in the JTextBox when the Create Button. The problem is that this process takes time to process, so I want the a progress bar to show that its processing
private class CreateButton implements ActionListener
{
#Override
public void actionPerformed(ActionEvent arg0) {
class Task extends SwingWorker<Void, Void>
{
#Override
protected Void doInBackground()
{
//Set Variables
for(Feature f : Feature.values())
{
if(f.getComp() != null)
{
f.getVariable().setVariable(((JTextField) f.getComp()).getText());
}
}
new Project(jobs.getSelectedValue().split("-")[0].trim(),
jobs.getSelectedValue().split("-")[1].trim(),
features);
return null;
}
}
ProgressMonitor pm = new ProgressMonitor(display, "Testing...", "", 0, 100);
pm.setProgress(0);
Task task = new Task();
task.execute();
}
}
I was not sure about your SSCCE so I am just posting how JProgressBar usually works.
Read about SwingWorker and JProgressBar
During background process show progress bar. A simple example of how it works is shown.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
public class MyProgressBarTest {
private static final long serialVersionUID = 1L;
private static JProgressBar progressBar;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
MyProgressBarTest obj = new MyProgressBarTest();
obj.createGUI();
}
});
}
public void createGUI() {
final JFrame frame = new JFrame();
JPanel panel = new JPanel();
final JButton button = new JButton("Progress");
progressBar = new JProgressBar();
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
MyCustomProgressBarDialog progressBarObj = new MyCustomProgressBarDialog(progressBar, frame);
progressBarObj.createProgressUI();
MyActionPerformer actionObj = new MyActionPerformer(progressBar, progressBarObj, button);
actionObj.execute();
}
});
panel.add(button);
frame.add(panel);
frame.setTitle("JProgressBar Example");
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setLocationRelativeTo(null);
frame.setSize(200, 300);
frame.setVisible(true);
}
}
class MyActionPerformer extends SwingWorker<String, Object> {
JProgressBar fProgressBar;
MyCustomProgressBarDialog progressDialog;
JButton button;
public MyActionPerformer(JProgressBar progressBar, MyCustomProgressBarDialog progressDialog, JButton button) {
this.fProgressBar = progressBar;
this.fProgressBar.setVisible(true);
this.fProgressBar.setIndeterminate(true);
this.button = button;
this.progressDialog = progressDialog;
this.button.setEnabled(false);
}
protected String doInBackground() throws Exception {
calculateResult();
return "Finished";
}
protected void done() {
fProgressBar.setVisible(false);
this.progressDialog.setVisible(false);
this.button.setEnabled(true);
}
public void calculateResult() {
for (int i = 0; i < 500000; i++) {
System.out.println("Progress Bar: " + i);
}
}
}
class MyCustomProgressBarDialog extends JDialog {
private static final long serialVersionUID = 1L;
private static JProgressBar progressBar;
private JFrame motherFrame;
private JLabel label = new JLabel("loading.. ");
private JButton button;
public MyCustomProgressBarDialog(JProgressBar progressBar, JFrame frame) {
this.progressBar = progressBar;
this.motherFrame = frame;
this.button = button;
}
public void createProgressUI() {
add(label, BorderLayout.NORTH);
add(progressBar, BorderLayout.CENTER);
setSize(50, 40);
setAlwaysOnTop(true);
setLocationRelativeTo(motherFrame);
setUndecorated(true);
setVisible(true);
}
}
This comes from the Oracle javadoc for ProgressMonitor:
Initially, there is no ProgressDialog. After the first
millisToDecideToPopup milliseconds (default 500) the progress monitor
will predict how long the operation will take. If it is longer than
millisToPopup (default 2000, 2 seconds) a ProgressDialog will be
popped up.
Note that it doesn't pop up until at least 1/2 second after you create it. Even then, it only pops up if the process is expected to take over 2 seconds.
This is all based on your repeated calls to setProgress(int) and the time between the progression of values across the range you gave it.
I suspect the conditions that cause the dialog to pop up are not being met. Or, perhaps, your program exits before that amount of time goes by.
You need to define attribute
ProgressMonitor pm;
then should create total progress size
int totalProgress = Feature.values().size();
then in the loop just increment count
int counter = 0;
for(Feature f : Feature.values())
{
if (pm.isCanceled()) {
pm.close();
return null;
}
pm.setProgress(counter);
pm.setNote("Task is " + counter*100/totalProgress + "% completed");
counter++;
call the progress monitor
pm = new ProgressMonitor(display, "Testing...", "", 0, totalProgress);
assumed that the most part of the job is done in the loop, if the other part such as project creation takes time then you could add additional percent counts to totalProgress or reset monitor after features completed.
I am using a gui with JTextFields to collect some information and then a JButton that takes that infomration and writes it to a file, sets the gui visibility to false, and then uses Runnable to create an instance of another JFrame from a different class to display a slideshow.
I would like to access some of the information for the JTextFields from the new JFrame slideshow. I have tried creating an object of the previous class with accessor methods, but the values keep coming back null (I know that I have done this correctly).
I'm worried that when the accessor methods go to check what the variables equal the JTextFields appear null to the new JFrame.
Below is the sscce that shows this problem.
package accessmain;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class AccessMain extends JFrame implements ActionListener
{
private static final int FRAMEWIDTH = 800;
private static final int FRAMEHEIGHT = 300;
private JPanel mainPanel;
private PrintWriter outputStream = null;
private JTextField subjectNumberText;
private String subjectNumberString;
public static void main(String[] args)
{
AccessMain gui = new AccessMain();
gui.setVisible(true);
}
public AccessMain()
{
super("Self Paced Slideshow");
setSize(FRAMEWIDTH, FRAMEHEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
//Begin Main Content Panel
mainPanel = new JPanel();
mainPanel.setBorder(new EmptyBorder(0,10,0,10));
mainPanel.setLayout(new GridLayout(7, 2));
mainPanel.setBackground(Color.WHITE);
add(mainPanel, BorderLayout.CENTER);
mainPanel.add(new JLabel("Subject Number: "));
subjectNumberText = new JTextField(30);
mainPanel.add(subjectNumberText);
mainPanel.add(new JLabel(""));
JButton launch = new JButton("Begin Slideshow");
launch.addActionListener(this);
mainPanel.add(launch);
//End Main Content Panel
}
#Override
public void actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand();
if(actionCommand.equals("Begin Slideshow"))
{
subjectNumberString = subjectNumberText.getText();
if(!(subjectNumberString.equals("")))
{
System.out.println(getSubjectNumber());
this.setVisible(false);
writeFile();
outputStream.println("Subject Number:\t" + subjectNumberString);
outputStream.close();
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
AccessClass testClass = new AccessClass();
testClass.setVisible(true);
}
});
}
else
{
//Add warning dialogue here later
}
}
}
private void writeFile()
{
try
{
outputStream = new PrintWriter(new FileOutputStream(subjectNumberString + ".txt", false));
}
catch(FileNotFoundException e)
{
System.out.println("Cannot find file " + subjectNumberString + ".txt or it could not be opened.");
System.exit(0);
}
}
public String getSubjectNumber()
{
return subjectNumberString;
}
}
And then creating a barebones class to show the loss of data:
package accessmain;
import javax.swing.*;
import java.awt.*;
public class AccessClass extends JFrame
{
AccessMain experiment = new AccessMain();
String subjectNumber = experiment.getSubjectNumber();
public AccessClass()
{
System.out.println(subjectNumber);
}
}
Hardcoding the accessor method with "test" like this:
public String getSubjectNumber()
{
return "test";
}
Running this method as below in the new JFrame:
SelfPaceMain experiment = new SelfPaceMain();
private String subjectNumber = experiment.getSubjectNumber();
System.out.println(subjectNumber);
Does cause the system to print "test". So the accessor methods seem to be working. However, trying to access the values from the JTextFields doesn't seem to work.
I would read the information from the file I create, but without being able to pass the subjectNumber (which is used as the name of the file), I can't tell the new class what file to open.
Is there a good way to pass data from JTextFields to other classes?
pass the argument 'AccessMain' or 'JTextField' to the second class:
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
AccessClass testClass = new AccessClass(AccessMain.this); //fixed this
testClass.setVisible(true);
}
});
Then reading the value of 'subjectNumber'(JTextField value) from the 'AccessMain' or 'JTextField' in the second class:
public class AccessClass extends JFrame
{
final AccessMain experiment;
public AccessClass(AccessMain experiment)
{
this.experiment = experiment;
}
public String getSubjectNumber(){
return experiment.getSubjectNumber();
}
}
Also, you should try Observer pattern.
A simple demo of Observalbe and Observer
Observable and Observer Objects
I have a function graphics() that creates my JFrame and two JRadioButtons and adds ActionListeners to them. This graphics is called from main() and graphics itself calls game().
public void game() throws Exception
{
jTextArea1.setLineWrap(true);
jTextArea1.setWrapStyleWord(true);
jTextArea1.setText("This is private information.");
jRadioButton1.setVisible(true);
jRadioButton2.setVisible(true);
try {
t.sleep(40000);
repaint();
} catch (InterruptedException e) {
// We've been interrupted: no more messages.
return;
}
After displaying "This is private information." in the text Area, I want the program execution to pause for 40 seconds, or until the user presses the JRadioButton, whichever is earlier. So I added an ActionListener and called t.interrupt() inside it.
private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
t.interrupt();
jRadioButton1.setVisible(false);
jRadioButton2.setVisible(false);
//System.out.println(t.interrupted());
jTextArea1.setText("Please wait...");
}
However, even after choosing the JRadioButton which should trigger the interrupt, that does not happen and t.interrupted returns false.
Any help would be appreciated.
Never, ever call Thread.sleep(...) on the Swing event thread as you will freeze the thread and effectively freeze your program. The solution is to consider use of a Swing Timer for the time-dependent portion of your requirement and using a SelectionListener for the JCheckBox or JRadioButton requirement.
For example:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
public class PausingExecution extends JPanel {
private static final String SELECTED_TEXT = "Snafus are Better!!!";
private static final String UNSELECTED_TEXT = "Fubars Rule!!";
private static final String TIMES_UP = "Time's Up!!!!";
private static final int TIMER_DELAY = 10 * 1000;
private JTextField messageField = new JTextField(UNSELECTED_TEXT, 10);
private JCheckBox checkBox = new JCheckBox("Click Me");
public PausingExecution() {
add(messageField);
add(checkBox);
checkBox.addItemListener(new ItemListener() {
#Override
public void itemStateChanged(ItemEvent iEvt) {
if (iEvt.getStateChange() == ItemEvent.SELECTED) {
messageField.setText(SELECTED_TEXT);
} else {
messageField.setText(UNSELECTED_TEXT);
}
}
});
Timer mySwingTimer = new Timer(TIMER_DELAY, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
messageField.setText(TIMES_UP);
checkBox.setEnabled(false);
}
});
mySwingTimer.setRepeats(false);
mySwingTimer.start();
}
private static void createAndShowGui() {
PausingExecution mainPanel = new PausingExecution();
JFrame frame = new JFrame("PausingExecution");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
I've got a "status" JLabel in one class (named Welcome) and the timer in another one (named Timer). Right now, the first one displays the word "status" and the second one should be doing the countdown. The way I would like it to be, but don't know how to - display 10, 9, 8, 7 ... 0 (and go to the next window then). My attempts so far:
// class Welcome
setLayout(new BorderLayout());
JPanel area = new JPanel();
JLabel status = new JLabel("status");
area.setBackground(Color.darkGray);
Font font2 = new Font("SansSerif", Font.BOLD, 25);
status.setFont(font2);
status.setForeground(Color.green);
area.add(status, BorderLayout.EAST); // can I put it in the bottom-right corner?
this.add(area);
and the timer:
public class Timer implements Runnable {
// public void runThread() {
// new Thread(this).start();
// }
public void setText(final String text) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
setText(text); // link to status here I guess
}
});
}
public void run() {
for (int i = 10; i > 0; i--) {
// set the label
final String text = "(" + i + ") seconds left";
setText(text);
// // sleep for 1 second
// try {
// Thread.currentThread();
// Thread.sleep(1000);
// } catch (Exception ex) {
// }
}
// go to the next window
UsedBefore window2 = new UsedBefore();
window2.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
// runThread();
}
} // end class
I agree that you should consider using a "Java" Timer as per Anh Pham, but in actuality, there are several Timer classes available, and for your purposes a Swing Timer not a java.util.Timer as suggested by Anh would suit your purposes best.
As for your problem, it's really nothing more than a simple problem of references. Give the class with the label a public method, say setCountDownLabelText(String text), and then call that method from the class that holds the timer. You'll need to have a reference of the GUI class with the timer JLabel in the other class.
For example:
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Welcome extends JPanel {
private static final String INTRO = "intro";
private static final String USED_BEFORE = "used before";
private CardLayout cardLayout = new CardLayout();
private JLabel countDownLabel = new JLabel("", SwingConstants.CENTER);
public Welcome() {
JPanel introSouthPanel = new JPanel();
introSouthPanel.add(new JLabel("Status:"));
introSouthPanel.add(countDownLabel);
JPanel introPanel = new JPanel();
introPanel.setPreferredSize(new Dimension(400, 300));
introPanel.setLayout(new BorderLayout());
introPanel.add(new JLabel("WELCOME", SwingConstants.CENTER), BorderLayout.CENTER);
introPanel.add(introSouthPanel, BorderLayout.SOUTH);
JPanel usedBeforePanel = new JPanel(new BorderLayout());
usedBeforePanel.setBackground(Color.pink);
usedBeforePanel.add(new JLabel("Used Before", SwingConstants.CENTER));
setLayout(cardLayout);
add(introPanel, INTRO);
add(usedBeforePanel, USED_BEFORE);
new HurdlerTimer(this).start();
}
private static void createAndShowUI() {
JFrame frame = new JFrame("Welcome");
frame.getContentPane().add(new Welcome());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
public void setCountDownLabelText(String text) {
countDownLabel.setText(text);
}
public void showNextPanel() {
cardLayout.next(this);
}
}
class HurdlerTimer {
private static final int TIMER_PERIOD = 1000;
protected static final int MAX_COUNT = 10;
private Welcome welcome; // holds a reference to the Welcome class
private int count;
public HurdlerTimer(Welcome welcome) {
this.welcome = welcome; // initializes the reference to the Welcome class.
String text = "(" + (MAX_COUNT - count) + ") seconds left";
welcome.setCountDownLabelText(text);
}
public void start() {
new Timer(TIMER_PERIOD, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (count < MAX_COUNT) {
count++;
String text = "(" + (MAX_COUNT - count) + ") seconds left";
welcome.setCountDownLabelText(text); // uses the reference to Welcome
} else {
((Timer) e.getSource()).stop();
welcome.showNextPanel();
}
}
}).start();
}
}
Since you're using Swing you should use the javax.swing.Timer, not the java.util.Timer. You can set the timer to fire at 1 second (1000 ms) intervals and have your listener do the updating. Since Swing updates must take place in the event dispatch thread your listener is the perfect place for status.setText.
there's already a Timer class in java: http://www.exampledepot.com/egs/java.util/ScheduleRepeat.html
Why not put the setText method in the welcome class and just do 'status.setText(text)'?
And you might try BorderLayout.SOUTH or .PAGE END or .LINE END to get the timer in the lower right corner