Sleep a iPOJO component, all disable - java

I have a simple question relating to iPOJO.
When a component iPOJO sleeps, all remaining components will also disable although there are not dependencies between them. Why? Here's an example:
Component 1:
#Component(name="frame1", immediate=true)
#Instantiate(name="iframe1")
public class Frame1 implements Runnable{
String str;
Label lb = new Label();
TextField tf = new TextField();
Frame fr;
public void run() {
fr = new Frame("Frame1");
fr.setLayout(new BorderLayout());
fr.setSize(230, 200);
fr.setLocation(900,250);
fr.add(tf, BorderLayout.NORTH);
lb.setText("Result");
fr.add(lb, BorderLayout.CENTER);
Panel pn = new Panel();
fr.add(pn, BorderLayout.SOUTH);
pn.setLayout(new GridLayout(1,4,1,1));
Button bt = new Button("Printer 1");
pn.add(bt);
bt.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
try {
System.out.println("start sleep");
Thread.sleep(5000);
System.out.println("stop sleep");
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
fr.setVisible(true);
}
#Validate
public void start() {
//this.delayService = dls;
Thread th = new Thread(this);
th.start();
}
#Invalidate
public void stop() {
System.out.println("stop");
fr.setVisible(false);
}
}
Component 2:
#Component(name="frame2", immediate=true)
#Instantiate(name="iframe2")
public class Frame2 implements Runnable{
String str;
Label lb = new Label();
TextField tf = new TextField();
Frame fr;
public void run() {
System.out.println("start component 2");
fr = new Frame("Frame2");
fr.setLayout(new BorderLayout());
fr.setSize(230, 200);
fr.setLocation(900,250);
fr.add(tf, BorderLayout.NORTH);
lb.setText("Result");
fr.add(lb, BorderLayout.CENTER);
Panel pn = new Panel();
fr.add(pn, BorderLayout.SOUTH);
pn.setLayout(new GridLayout(1,4,1,1));
Button bt = new Button("Printer 2");
pn.add(bt);
bt.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("in 2");
}
});
fr.setVisible(true);
}
#Validate
public void start() throws Exception {
//this.delayService = dls;
System.out.println("start thread 2");
Thread th = new Thread(this);
th.start();
//fr.setVisible(true);
}
#Invalidate
public void stop() throws Exception {
System.out.println("stop");
fr.setVisible(false);
}
}
Two components are deployed and running. There are two independent components. But I click the "Printer 1" button. "frame1" component is sleeping during 5s. And during these 5 seconds, i can't click "Printer 2" of "frame2" component.

This is not an ipojo issue. Swing uses one thread (and only one thread) in order to dispatch events such as clicks. When you click your first button, swing runs your actionPerformed in this thread. This method puts your thread to sleep for 5 seconds. This means that the thread responsible for event handling cannot do anything during this time. This is why your program does not respond to your second click.
Whenever you have a long computation in swing (and also osgi), it is often a good idea to run your code in a separate thread in order to avoid blocking the execution (here you have a useless Thread.sleep() but I guess you could have an http request or anything that may take a long time instead). You should probably use an executor service or anything similar.

Related

How do I set a JTextPane out of another Thread?

I am learning about multithreading right now.
I have a "main" class in which I build a JFrame with a Textfield and a Start and a Stop Button in it.
I also have another class/Thread from which I want to print the current time into my Textfield when I click the Start Button of my Frame. Everything works but the text doesn't change in my Textfield as I start the Thread even though Eclipse says my code is alright.
What am I doing wrong?
Class 1:
public class Uhr extends JFrame {
private JPanel contentPane;
public JTextPane tpZeit;
Thread t;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Uhr frame = new Uhr();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Uhr() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
tpZeit = new JTextPane();
tpZeit.setText("test");
tpZeit.setBounds(43, 50, 212, 43);
contentPane.add(tpZeit);
JButton btnstart = new JButton("GO");
btnstart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tpZeit.setText("started");
if (t == null) {
t = new Thread(new Uhrsteuerung());
}
if(!t.isAlive()) {
t = new Thread(new Uhrsteuerung());
t.start();
}
}
});
btnstart.setBounds(10, 227, 89, 23);
contentPane.add(btnstart);
JButton btnstop = new JButton("Stop");
btnstop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
t.interrupt();
}
});
btnstop.setBounds(248, 227, 89, 23);
contentPane.add(btnstop);
}
}
Class with additional Thread:
public class Uhrsteuerung extends Uhr implements Runnable {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String time;
boolean x ;
#Override
public synchronized void run() {
// TODO Auto-generated method stub
x = true;
while (x) {
try {
time = sdf.format(System.currentTimeMillis());
System.out.println(time);
tpZeit.setText(time);
repaint();
Thread.sleep(1000);
} catch (InterruptedException ex) {
x = false;
}
}
}
}
All code for Java's Swing classes must be executed on the Event Dispatch Thread (EDT). Swing provides some special classes for helping you do this.
To execute code off the EDT, use a SwingWorker class. SwingWorker will execute a task in the background (not using the EDT) and then return a result properly synchronized that executes on the EDT. https://docs.oracle.com/en/java/javase/16/docs/api/java.desktop/javax/swing/SwingWorker.html
Its application is pretty simple. Define your own class that extends SwingWorker.
class PrimeNumbersTask extends SwingWorker<List<Integer>, Integer> {
// ...
If you need to update a component, it's good to pass in the component so that you can refer to it later.
class PrimeNumbersTask extends SwingWorker<List<Integer>, Integer> {
private JTextArea textArea;
public PrimeNumbersTask( JTextArea textArea ) {
this.textArea = textArea;
}
// ...
Do your work in the method doInBackground() by overriding it. Don't touch the Swing classes in this method. Use the publish() method to send smaller chunks of data to the EDT if you want to provide progressive results instead of waiting for the whole task to complete.
#Override
public List<Integer> doInBackground() {
while (!done && ! isCancelled()) {
// do stuff
publish(number);
setProgress(100 * numbers.size() / numbersToFind);
}
}
return numbers;
}
Finally override the method process() to update your Swing classes.
#Override
protected void process(List<Integer> chunks) {
for (int number : chunks) {
textArea.append(number + "\n");
}
}
From the EDT, you can call execute() to start your background task.
JTextArea textArea = new JTextArea();
// manipulate and set up GUI...
PrimeNumbersTask task = new PrimeNumbersTask(textArea);
task.execute();
There's a lot more docs on SwingWorker if you Google for it, that's the basics of how to use it.

Java - delay between events after entering the value into JTextArea

I prepared a simple form (javax.swing.JFrame) on which JTextArea is located.
After entering the "TAB" value, I would like to see a visible JPanel on the form for a few seconds. Unfortunately, when I have the following events:
<br>
JPanel.setVisible (true); <br>
TimeUnit.SECONDS.sleep (3); <br>
JPanel.setVisible (false); <br>
in keyPressed for JTextArea.KeyListener ()
they don't execute one after another - first I get sleep, then JPanel.setVisible (true) and JPanel.setVisible (false)
public class Form1 extends JFrame {
JTextArea txt1 = new JTextArea();
JPanel pnl2 = new JPanel();
public Form1() {
txt1.setLocation(10, 10);
txt1.setSize(100, 30);
txt1.addKeyListener(new KeyListener() {
#Override
public void keyPressed(KeyEvent e
) {
if (e.getKeyCode() == KeyEvent.VK_TAB) {
e.consume();
pnl2.setVisible(true);
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException ex) {
Logger.getLogger(Form1.class.getName()).log(Level.SEVERE, null, ex);
}
pnl2.setVisible(false);
}
}
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyReleased(KeyEvent e
) {
}
});
add(txt1);
pnl2.setLayout(null);
pnl2.setLocation(10, 150);
pnl2.setSize(100, 100);
pnl2.setBorder(BorderFactory.createEtchedBorder());
pnl2.setVisible(false);
add(pnl2);
}
}
Main class:
public class Main {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
Form1 f = new Form1()
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(500, 400);
f.setLocationRelativeTo(null);
f.setResizable(false);
f.setLayout(null);
f.setVisible(true);
}
}
);
}
}
How can I get the effect of showing the panel on the form for a few seconds after entering the "TAB" button on TextArea?
Don't block the AWT Event Dispatch Thread (EDT).
Instead, set a javax.swing.Timer (get the package right - not the other one!). The Swing Timer may create another thread to do the timing, but posts the event back through the EDT (via java.awt.EventQueue.invokeLater or similar). The code executed after the sleep should be move to an action listener.
pnl2.setVisible(true);
javax.swing.Timer timer =
new javax.swing.Timer(3000, event -> {
pnl2.setVisible(false);
});
timer.setRepeats(false);
timer.start();
(I have used the fully qualified name to emphasize the right Timer. Sometimes I forget that by default it will repeat, which would be confusing.)

JProgressBar not visible during reading

I got a java progressbar which loads perfectly, but I can't see the process, only the result. (when the bar finished loading)
I want to see every percentage of the progress. When I run the code, the frame appears but the progressbar doesn't, only when it's on 100%. Where's the problem?
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
JFrame f = new JFrame("JProgressBar Sample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = f.getContentPane();
progressBar = new JProgressBar();
progressBar.setStringPainted(true);
Border border = BorderFactory.createTitledBorder("Reading...");
progressBar.setBorder(border);
content.add(progressBar, BorderLayout.CENTER);
f.setSize(300, 100);
f.setVisible(true);
progressBar.setValue(0);
inc(); //fill the bar
//It fills, but I can't se the whole loading...
}
//Here's the filling path
public static void inc(){
int i=0;
try{
while (i<=100){
progressBar.setValue(i+10);
Thread.sleep(1000);
i+=20;
}
}catch(Exception ex){
//nothing
}
}
Your GUI is not updated while you are running a long process in the GUI's thread, like filling a progress bar and sleeping some seconds.
Just emerge a thread, which will handle the long operation.
Within this thread set the progress bar to the desired value within a SwingUtilities.invokeLater(new Runnable() {...}.
Runnable r = new Runnable() {
public void run() {
int i=0;
try{
while (i<=100){
final int tmpI = i+10;
SwingUtilities.invokeLater(new Runnable(){
public void run() {
progressBar.setValue(tmpI);
}
});
Thread.sleep(1000);
i += 20;
}
} catch(Exception ex){
//nothing
}
}
};
Thread t = new Thread(r);
t.start();

Why does SwingUtilities.invokeLater() cause JButton to freeze?

Consider this basic Swing program, consisting out of two buttons:
public class main {
public static void main(String[] args) {
JFrame jf = new JFrame("hi!");
JPanel mainPanel = new JPanel(new GridLayout());
JButton longAction = new JButton("long action");
longAction.addActionListener(event -> doLongAction());
JButton testSystemOut = new JButton("test System.out");
testSystemOut.addActionListener(event -> System.out.println("this is a test"));
mainPanel.add(longAction);
mainPanel.add(testSystemOut);
jf.add(mainPanel);
jf.pack();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
public static void doLongAction() {
SwingUtilities.invokeLater(() -> {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
System.out.println("Interrupted!");
}
System.out.println("Finished long action");
});
}
}
I want my second button testSystemOut to be usable while the first one is working on its long action (here, I put a 3 second sleep in it). I can do that by manually putting doLongAction() in a Thread and call start(). But I've read I should use SwingUtilities instead, which works exactly like EventQueue here. However, if I do so, my Button freezes for the duration of its action.
Why?
By using SwingUtilities.invokeLater, you are calling the enclosed code, including the Thread.sleep(...) call, on the Swing event thread, which is something you should never do since it puts the entire event thread, the thread responsible for drawing your GUI's and responding to user input, to sleep -- i.e., it freezes your application. Solution: use a Swing Timer instead or do your sleeping in a background thread. If you are calling long-running code and using a Thread.sleep(...) to simulate it, then use a SwingWorker to do your background work for you. Please read Concurrency in Swing for the details on this. Note that there is no reason for the SwingUtilities.invokeLater where you have it since the ActionListener code will be called on the EDT (the Swing event thread) regardless. I would however use SwingUtilities.invokeLater where you create your GUI.
e.g.,
import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.concurrent.ExecutionException;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame jf = new JFrame("hi!");
JPanel mainPanel = new JPanel(new GridLayout());
JButton testSystemOut = new JButton("test System.out");
testSystemOut.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("this is a test");
}
});
mainPanel.add(new JButton(new LongAction("Long Action")));
mainPanel.add(new JButton(new TimerAction("Timer Action")));
mainPanel.add(testSystemOut);
jf.add(mainPanel);
jf.pack();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
});
}
#SuppressWarnings("serial")
public static class LongAction extends AbstractAction {
private LongWorker longWorker = null;
public LongAction(String name) {
super(name);
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}
#Override
public void actionPerformed(ActionEvent e) {
setEnabled(false);
longWorker = new LongWorker(); // create a new SwingWorker
// add listener to respond to completion of the worker's work
longWorker.addPropertyChangeListener(new LongWorkerListener(this));
// run the worker
longWorker.execute();
}
}
public static class LongWorker extends SwingWorker<Void, Void> {
private static final long SLEEP_TIME = 3 * 1000;
#Override
protected Void doInBackground() throws Exception {
Thread.sleep(SLEEP_TIME);
System.out.println("Finished with long action!");
return null;
}
}
public static class LongWorkerListener implements PropertyChangeListener {
private LongAction longAction;
public LongWorkerListener(LongAction longAction) {
this.longAction = longAction;
}
#Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getNewValue() == SwingWorker.StateValue.DONE) {
// if the worker is done, re-enable the Action and thus the JButton
longAction.setEnabled(true);
LongWorker worker = (LongWorker) evt.getSource();
try {
// call get to trap any exceptions that might have happened during worker's run
worker.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
}
#SuppressWarnings("serial")
public static class TimerAction extends AbstractAction {
private static final int TIMER_DELAY = 3 * 1000;
public TimerAction(String name) {
super(name);
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}
#Override
public void actionPerformed(ActionEvent e) {
setEnabled(false);
new Timer(TIMER_DELAY, new TimerListener(this)).start();
}
}
public static class TimerListener implements ActionListener {
private TimerAction timerAction;
public TimerListener(TimerAction timerAction) {
this.timerAction = timerAction;
}
#Override
public void actionPerformed(ActionEvent e) {
timerAction.setEnabled(true);
System.out.println("Finished Timer Action!");
((Timer) e.getSource()).stop();
}
}
}
Don't use SwingUtilities.invokeLater(...) when you want to execute some long-running code. Do that in a separate normal thread.
Swing is not multi-threaded, it's event-driven. Because of that there are methods like SwingUtilities.invokeLater(...). You have to use those methods if you want to alter Swing-Components from a different thread (since Swing is not thread-safe), for example if you want to change a Button's text.
Everything thats GUI-Related runs in that Swing-Thread, e.g. Cursor-Blinks, Messages from the OS, User Commands, etc.
Since its a single thread, every long running Code in this thread it will block your GUI.
If you just do some long-running code that isn't GUI-related, it shouldn't run in the Swing-Event-Thread, but in its own separated thread.
See
https://weblogs.java.net/blog/kgh/archive/2004/10/multithreaded_t.html
for why Swing is not Multi-Threaded.

Kill thread on close

I have a constructor of a JFrame where I have a Thread(t1) which is running thanks to a
while(true)
I would like to know how to implement my JFrame so it can kill the thread when I close it, because t1 need to be running when the JFrame is active
EDIT:
Here is the code:
public class Vue_Session extends JFrame {
private JPanel contentPane;
private int idsess;
private User u;
public Vue_Session(User us, int id) {
this.u = us;
this.idsess = id;
toServ t=new toServ(idsess);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds((int) screenSize.getWidth() / 2 - 800 + (800 / 2), 90, 800,
600);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout());
Vue_Idee vueIdee = new Vue_Idee(this.idsess, this.u);
contentPane.add(vueIdee, BorderLayout.SOUTH);
Vue_IdeeSession vueSess = new Vue_IdeeSession(this.idsess);
contentPane.add(vueSess, BorderLayout.CENTER);
Thread t1 = new Thread( new Runnable(){
public void run(){
while(true){
try{
Thread.sleep(500);
}catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
t.getIdee();
vueSess.act();
revalidate();
}
}
});
t1.start();
}
You can handle it with a boolean variable like
boolean end = false;
while (!end){...}
Also I suggest you use ExecutorService or ForkJoinPool so you can simply control your tasks, threads, etc
EDIT:
boolean end = false;
new Thread(() -> {
while (!end) {
//...
}
}).start();
and this is where you should end your tasks:
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosed(java.awt.event.WindowEvent evt) {
end = true;
System.exit(0);
// or this.dispose();
}
});
good luck :)
It's a controversial topic but in general I would replace the while (true) construct with
while(!Thread.currentThread().isInterrupted()){
try{
Thread.sleep(500);
}catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
t.getIdee();
vueSess.act();
revalidate();
}
More information on this topic can be found here:
http://www.javaspecialists.eu/archive/Issue056.html
Okay, here was the answer:
I needed to add en WindowListenne:
addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent b) {
t1.stop();
dispose();
}
});
And also:
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
First, you need to make the thread kill-able. To do this, instead of looping forever, loop as long as certain looping flag is true.
After that, you need to create a listener that gets called whenever the user closes the frame. You can use WindowAdapter to do this. When the listener is called, set the looping flag to false. Once the thread dies, you can safely terminate the program.
For example:
public class Vue_Session extends JFrame {
Thread thread = null;
boolean threadAlvie = true;
boolean threadDie = false;
public Vue_Session(User us, int id) {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
threadAlive = false;
// Wait until the thread dies
while (!threadDie) {
// Sleep for 100 milliseconds.
Thread.sleep(100);
}
System.exit(0);
}
});
thread = new Thread(new Runnable() {
public void run(){
while (threadAlive){
// do something
}
threadDie = true;
}
});
thread.start();
}
}

Categories