SwingWorker: Syntax error on token "execute" -? - java

I'm trying to use a SwingWorker but for some reason at worker.execute(); I receive the following error:
"Syntax error on token "execute", Identifier expected after this
token"
That doesn't help much, it's a generic error, and no matter how stupid it might be, it's been torturing me for hours.. I've seen that moving SwingWorker somewhere else outside the class may fix it, but I don't understand how and why and why it should not work like this! Ugh!
public class App {
private App() { // CONSTRUCTOR
final int WINHSIZE = 2000;
final int WINVSIZE = 2000;
class Enjoy extends JPanel {
#Override
public void paintComponent(Graphics g) {
g.drawLine(0, 0, 2000, 2000);
}
class MyExecutor extends SwingWorker<Void,Void> {
#Override
protected Void doInBackground() {
return null;
}
#Override
protected void done(){ // runs on EDT
}
}
MyExecutor worker = new MyExecutor();
worker.execute(); // What the hell is going wrong here?
}
Runnable runner = new Runnable() {
#Override
public void run() {
JFrame f = new JFrame("Title");
JPanel panel = new Enjoy();
JScrollPane myScrollPane = new JScrollPane(panel);
f.add("Center", myScrollPane);
f.pack();
f.setVisible(true);
}
};
SwingUtilities.invokeLater(runner);
}
public static void main(String[] args) {
new App();
}
}
Thanks.

worker.execute(); must be inside some method, it can't be directly within the scope of your Enjoy class.

Related

How to change Background color of a JButton from a derived class in Java Swing

I have a base class mainframe and i have keeping the JButton as final static Which its BGcolor going to be changed by a extended class of mainframe namely dataframe. Initially i need to set the BGColor of the JButton to red. Then I need to change it to some other colors from the dataframe. I can able to set the BGColor from the mainframe but not from the dataframe(extended class). I've used mainframe.Button_name.setBackground(color.yellow); but still its not changing
`enter code here`
public class mainframe {
final static JButton Button_name = new JButton("Hi");
public static void main(String[] args)
{
public void run()
{
Button_name.setBackground(color.Red); //This is working
}
}
}
class dataframe extends mainframe implements Runnable
{
public void run()
{
//doing other things
while(some condition)
{
if (another_condition)
{
//from here i need to change that Buttons color
// i've tried this
mainframe.Button_name.setBackground(color.yellow); //Not working
}
}
}
}
Kindly anyone help with this issue
So you want to change the state of a UI component from a different thread in a different class. There are multiple ways you might be able to do this, but first, I would start by defining away for those classes to be able to only effect the change you want them to.
Exposing the entire frame, component or even button is not a good idea, people have a habit of changing things you don't want them to, so instead, we define a simple contract which states what they are allowed to do, for example...
public interface Colorable {
public void setColor(Color color);
}
This immediately decouples your code, meaning that any code that wants to change the state of your UI (or change the color of something else) can do so, without relying on the physical implementation.
Thread
First, we're going to have a look at using a Thread to change the UI...
public class ColorChanger {
private Colorable colorable;
public ColorChanger(Colorable colorable) {
this.colorable = colorable;
}
public void start() {
Thread t = new Thread(new Runnable() {
#Override
public void run() {
for (int index = 0; index < 1000; index++) {
if (index % 100 == 0) {
if ((index / 100) % 2 == 0) {
colorable.setColor(Color.GREEN);
} else {
colorable.setColor(Color.RED);
}
}
try {
// This is so you can see the colors changing
Thread.sleep(5);
} catch (InterruptedException ex) {
}
}
System.out.println("Done");
}
});
t.start();
}
}
This is a pretty basic class, it requires an instance of Colorable and will change the state of the color for every 100 counts, based on if it's an even or odd hundred
We use a simple JPanel as our base test class, when you click the button, the ColorChanger is created and started.
public class TestPane extends JPanel implements Colorable {
private JButton btn;
private ColorChanger changer;
public TestPane() {
setLayout(new GridBagLayout());
setBorder(new EmptyBorder(20, 20, 20, 20));
btn = new JButton("I am your button");
add(btn);
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (changer == null) {
changer = new ColorChanger(TestPane.this);
changer.start();
}
}
});
}
#Override
public void setColor(Color color) {
if (EventQueue.isDispatchThread()) {
btn.setBackground(color);
} else {
System.out.println("Not in the EDT");
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
setColor(color);
}
});
}
}
}
You will note that the setColor method has a bunch of code in it, this is to ensure that the updates to the UI are executed only from within the context of the Event Dispatching Thread.
SwingWorker
An alternative is to use a SwingWorker, which operates very similarly to a Thread, expect it has the ability to publish content to the EDT
public class ColorChangerWorker extends SwingWorker<Void, Color> {
private Colorable colorable;
public ColorChangerWorker(Colorable colorable) {
this.colorable = colorable;
}
#Override
protected void process(List<Color> chunks) {
colorable.setColor(chunks.get(chunks.size() - 1));
}
#Override
protected Void doInBackground() throws Exception {
for (int index = 0; index < 1000; index++) {
if (index % 100 == 0) {
if ((index / 100) % 2 == 0) {
publish(Color.GREEN);
} else {
publish(Color.RED);
}
}
try {
// This is so you can see the colors changing
Thread.sleep(5);
} catch (InterruptedException ex) {
}
}
System.out.println("Done");
return null;
}
}
You will note here, that when we want to change the color we call publish. The process method is called to let us know that there is more data to be processed, but here, we're only interested in the last change.
And out TestPane...
public class TestPane extends JPanel implements Colorable {
private JButton btn;
private ColorChangerWorker changer;
public TestPane() {
setLayout(new GridBagLayout());
setBorder(new EmptyBorder(20, 20, 20, 20));
btn = new JButton("I am your button");
add(btn);
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (changer == null) {
changer = new ColorChangerWorker(TestPane.this);
changer.execute();
}
}
});
}
#Override
public void setColor(Color color) {
if (EventQueue.isDispatchThread()) {
btn.setBackground(color);
} else {
System.out.println("Not in the EDT");
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
setColor(color);
}
});
}
}
}
You will note that the setColor method remains unchanged, this is deliberate, when you test this class, you will note that "Not in the EDT" is never printed, basically meaning we could do away with all that code and just call btn.setBackground(color);, but I want you to see the difference.
The Button...
Now, when I run this code, I get the following output...
Wait a minute, that buttons background is filled?! Actually it is, but many button implementations have a secondary "content area" filling
You can turn this off using something like...
btn.setContentAreaFilled(false);
btn.setOpaque(true);
Which will result in something like...

JProgressBar.setMaximum(int) sometimes doesn't seems to work

I have a JProgressBar that I need to set a new minimum, maximum and progress value each time a task of a queue is done, using the code below:
this.progressBar.setMinimum(minimum);
this.progressBar.setMaximum(maximum);
this.progressBar.setValue(progress);
But, I noticed that sometimes, the setMaximum(int) method doesn't seems to work, because the progress bar still working with the old maximum value.
Then I wrote some test code and ran it:
progressBar.setMaximum(10);
System.out.println(progressBar.getMaximum());
Sometimes it prints 10 as expected, and sometimes prints the old value: anything different from 10.
I spent hours on Google and javadocs, tried to call revalidate() and repaint() on JProgressBar's parent, and nothing. I also tried to call Thread.sleep(10) to wait AWT threads to run another tasks and didn't work.
Any ideas?
EDIT: Provided some more code:
/* Applet that construct the view and init a Thread. */
public class FastApplet extends Applet {
private JProgressBar progressBar;
private JPanel panel;
private Runnable runnable;
#Override
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
#Override
public void run() {
createAndShowGUI();
initThreads();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void createAndShowGUI() {
panel = new JPanel();
progressBar = new JProgressBar();
progressBar.setStringPainted(true);
panel.add(progressBar);
}
private void initThreads() {
runnable = new MyRunnable(progressBar);
Thread thread = new Thread(runnable);
thread.start();
}
}
/* Runnable that update progress and call setMaximum(int) on JProgressBar */
public class MyRunnable {
private int progress;
private final JProgressBar progressBar;
MyRunnable(JProgressBar progressBar) {
this.progressBar = progressBar;
}
#Override
public void run() {
// do the tasks and update the progressBar using progressBar.setValue(progress)
// when done, reset the progress bar with the a new maximum:
defineNewProgressBar(0, 0, newMaximum);
}
public void defineNewProgressBar(int progress, int minimum, int maximum) {
this.progress = progress;
Component component = progressBar.getParent();
JPanel panel = (JPanel) component;
this.progressBar.setMinimum(minimum);
this.progressBar.setMaximum(maximum);
this.progressBar.setValue(progress);
panel.revalidate();
panel.repaint();
}
}
You wrote in your comments that you call setMaximum() not from the EDT. You should call it from the EDT like this:
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
progressBar.setMaximum(10);
}
});
If you use Java8, you can do this with using a lambda expression:
SwingUtilities.invokeLater(()->progressBar.setMaximum(10));
You can use SafeProgressBarUpdaterThread :
progressBarUpdater = new SafeProgressBarUpdaterThread(progressBar);
//...
progressBarUpdater.setMaximum(maximum);
progressBarUpdater.setValue(value);
java.awt.EventQueue.invokeLater(progressBarUpdater);

how to suspend Applet in browser when tab is unselected

I'm making my first Applet. I have a JPanel which creates a Swing GUI and performs CPU intensive tasks (repainting a Component 60Hz). My Applet displays this JPanel on event dispatching thread. here is an abstraction of the problem. Normally I would launch the applet from an html document instead of having a main method. This program puts about a 40% load on my CPU.
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
public class TestApplet extends JApplet {
TestPanel tp;
public void init() {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
createGUI();
}
});
} catch (Exception e) {
System.err.println("createGUI didn't complete successfully");
}
}
private void createGUI() {
//Create and set up the content pane.
tp = new TestPanel();
tp.setOpaque(true);
setContentPane(tp);
}
public static void main(String[] args) {
JFrame f = new JFrame("Fish Tank");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JApplet ap = new TestApplet();
ap.init();
f.add("Center", ap);
f.pack();
f.setVisible(true);
}
}
class TestPanel extends JPanel{
public TestTank tt = new TestTank();
public TestPanel() {add(tt);}
public void stop() {tt.stop();}
public void start() {tt.start();}
}
class TestTank extends Component implements ActionListener{
private javax.swing.Timer timer;
TestTank(){
timer = new javax.swing.Timer(17, this);
timer.setCoalesce(true);
timer.start();
}
public Dimension getPreferredSize(){
return new Dimension(900, 700);
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Dimension size = getSize();
g2.setPaint(new GradientPaint(0,0,Color.RED,900, 0,Color.WHITE));
g2.fill(new Rectangle2D.Float(0,0,size.width,size.height));
}
public void actionPerformed(ActionEvent e) {
repaint();
}
public void stop(){timer.stop();}
public void start(){timer.start();}
}
My question: How do I suspend and resume execution of the JPanel (FishTankPanel) when the user switches tabs or minimizes the browser? I want the Applet to stop using the CPU when the user can't see what it is doing. I need to capture browser events in order to execute tp.stop() in the applet. I have tried to execute them with window event listeners in the JPanel, and by overriding the start() and stop() methods in the Applet. I have been unsuccessful. Any suggestions or solutions would be appreciated.
I would do as Dave said and use the JApplet override start and stop methods to call your GUI methods. For instance, see changes in code:
public class TestApplet extends JApplet {
TestPanel tp;
public void init() {
// ... no change
}
private void createGUI() {
// ... no change
}
#Override
public void stop() {
if (tp != null) {
tp.stop();
}
}
#Override
public void start() {
if (tp != null) {
tp.start();
}
}
}
class TestTank extends Component implements ActionListener {
private javax.swing.Timer timer;
// ... no change
public void stop() {
timer.stop();
System.out.println("stop");
}
public void start() {
timer.start();
System.out.println("start");
}
}
It seems you might need to leverage some JS for this. E.G. use the JS shown in this answer to explicitly call the applet start() & stop() methods on focus & blur detection respectively.
The solution for my problem was to use javascript to implement the Page Visibility API. I then called the appropriate Java methods from within the javascript script.

Java Event Queue : how to update component in JFrame

I have read about that when programming Java Swing, we should put these components into Java Event Queue, because Java Swing thread is not-thread safe.
But, when I use Event Queue, I don't know how to update component properties (for example : set text for label or change something..). Here is my code :
public class SwingExample {
private JLabel lblLabel;
SwingExample(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
lblLabel = new JLabel("Hello, world!", JLabel.CENTER);
frame.getContentPane().add(lblLabel); // adds to CENTER
frame.setSize(200, 150);
frame.setVisible(true);
}
public void setLabel(){
lblLabel.setText("Bye Bye !!!");
}
public static void main(String[] args) throws Exception
{
SwingExample example = null;
EventQueue.invokeLater(new Runnable()
{
public void run()
{
example = new SwingExample(); // ERROR : Cannot refer to non-final variable inside an inner class defined in different method
}
});
// sometime in the futures, i want to update label, so i will call this method...
example.setLabel();
}
}
I know, if I write SwingExample example = new SwingExample(); the error won't appear again, but if i use that, I cannot process example.setLabel later.
Please tell me about this error and how to fix this.
Thanks :)
By having your SwingExample instance as a field, you can reference it inside the inner classes without it being final.
public class SwingExample {
private JLabel lblLabel;
private static SwingExample instance;
SwingExample() {
// code omitted
}
public void setLabel() {
lblLabel.setText("Bye Bye !!!");
}
public static void main(String[] args) throws Exception {
EventQueue.invokeLater(new Runnable() {
public void run() {
instance = new SwingExample();
}
});
// ...
EventQueue.invokeLater(new Runnable() {
public void run() {
instance.setLabel();
}
});
}
}

java program flow on example of DynamicReports and progressbar

i use DynamicReports library for make reports for my app. Createing report takes some time and i decided create custom progress bar while reports has not been created. Question after code examples.
progress bar class:
public class ProgressDialog implements DialogWrapper{
private JFrame iFrame;
private JDialog iDialog;
private JPanel pane;
private final JProgressBar aJProgressBar = new JProgressBar(0, 100);
public ProgressDialog(){
onCreate();
}
#Override
public void onCreate() {
iFrame = new JFrame("Создание отчета");
iDialog = new JDialog(iFrame, true);
pane = new JPanel();
aJProgressBar.setIndeterminate(true);
pane.add(aJProgressBar, BorderLayout.NORTH);
iDialog.add(pane, BorderLayout.CENTER);
iDialog.setTitle("Создание отчета");
iDialog.setSize(300, 150);
iDialog.setResizable(false);
iDialog.setVisible(true);
return;
}
#Override
public void fillData() {}
#Override
public void onSubmit() {}
protected void onCancel(){
iDialog.setVisible(false);
iDialog.dispose();
}
public void cancel(){
onCancel();
}
}
Report abstract class
public abstract class AbstractReportMain<T extends ReportDesign<U>, U extends ReportData> {
private ProgressDialog pd;
public AbstractReportMain() {
pd = new ProgressDialog();
build();
}
protected void build() {
try {
JasperReportBuilder reportBuilder = DynamicReports.report();
U data = getReportData();
if (data != null) {
reportBuilder.setDataSource(data.createDataSource());
}
getReportDesign().configureReport(reportBuilder, data);
pd.cancel();
reportBuilder.show(false);
} catch (DRException e) {
e.printStackTrace();
}
}
protected U getReportData() {
return null;
}
protected abstract T getReportDesign();
}
Question: when i create ProgressDialog, program flow stops while i do not close dialog. Why it's happen, how this behaviour called and where i can read about it? How it use and маке it work for me. Thanks.
I believe your problem is that you are not creating a new thread for your progress dialog. Here is an example that will help you. Modal Progress Dialog example

Categories

Resources