I have read this article on the EDT (Event Dispatch Thread) javaworld.com which shows how to correctly setup a Swing GUI on the EDT and put long running tasks which modify the GUI inside Runnables.
It all makes sense however the example program (I pasted below) where the only modification I made is a Thread.sleep(6000) to simulate a long lag makes the interface irresponsive for some seconds.
Am I missing something?
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ViewPage
{
public static void main(String[] args)
{
Runnable r;
r = new Runnable()
{
#Override
public void run()
{
final JFrame frame = new JFrame("View Page");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.add(new JLabel("Enter URL"));
final JTextField txtURL = new JTextField(40);
panel.add(txtURL);
frame.getContentPane().add(panel, BorderLayout.NORTH);
final JTextArea txtHTML = new JTextArea(10, 40);
frame.getContentPane().add(new JScrollPane(txtHTML),
BorderLayout.CENTER);
ActionListener al;
al = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
txtURL.setEnabled(false);
Runnable worker = new Runnable()
{
public void run()
{
InputStream is = null;
try
{
URL url = new URL(txtURL.getText());
is = url.openStream();
final StringBuilder sb;
sb = new StringBuilder();
int b;
while ((b = is.read()) != -1)
{
sb.append((char) b);
}
Runnable r = new Runnable()
{
public void run()
{
try
{
Thread.sleep(6000);
}
catch (InterruptedException ex)
{
Logger.getLogger(ViewPage.class.getName()).log(Level.SEVERE, null, ex);
}
txtHTML.setText(sb.toString());
txtURL.setEnabled(true);
}
};
try
{
EventQueue.invokeAndWait(r);
}
catch (InterruptedException ie)
{
}
catch (InvocationTargetException ite)
{
}
}
catch (final IOException ioe)
{
Runnable r = new Runnable()
{
public void run()
{
txtHTML.setText(ioe.getMessage());
txtURL.setEnabled(true);
}
};
try
{
EventQueue.invokeAndWait(r);
}
catch (InterruptedException ie)
{
}
catch (InvocationTargetException ite)
{
}
}
finally
{
Runnable r = new Runnable()
{
public void run()
{
txtHTML.setCaretPosition(0);
txtURL.setEnabled(true);
}
};
try
{
EventQueue.invokeAndWait(r);
}
catch (InterruptedException ie)
{
}
catch (InvocationTargetException ite)
{
}
if (is != null)
{
try
{
is.close();
}
catch (IOException ioe)
{
}
}
}
}
};
new Thread(worker).start();
}
};
txtURL.addActionListener(al);
frame.pack();
frame.setVisible(true);
}
};
EventQueue.invokeLater(r);
}
}
Am I missing something?
Yes.
Runnable is just an interface not another thread.
Here with this line you are wrapping the call to execute later in the Event Dispatch Thread.
EventQueue.invokeLater(r); // you can use SwingUtilities.invokeLater(r) too
Then you call
Thread.sleep(6000);
This is executed in EDT and make irresponsive the gui until finish.
For long task you should use another threads or SwingWorker and for short task with some repetitions SwingTimer.
Read about Concurrency in Swing
Related
new Scanner(new URL("SOME_URL.txt").openStream(), "UTF-8").useDelimiter("\\A").next();
Using this ^ I get the data from a .txt-File which I save in a String.
For my progress bar, I wondered if it would be possible for jobs like that (or in general for methods etc.) to count (or sth. like that) the time it needed to be finished. So that I can show in real time process time in my bar.
Is this somehow possible?
EDIT:
package app.gui;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.net.URL;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.apache.commons.io.IOUtils;
public class Updater {
private JFrame frame;
private static String rawG;
private static String versI;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
rawG = new Scanner(new URL("SOME_URL.txt").openStream(), "UTF-8").useDelimiter("\\A").next();
versI = IOUtils.toString(getClass().getClassLoader().getResourceAsStream("version.txt"));
} catch (Exception e) {
System.out.println("error class Updater try/catch raw github");
}
if (Integer.parseInt(rawG.split("\\.")[0]) < Integer.parseInt(versI.split("\\.")[0])) {
System.out.println("Version check failure, update needed");
try {
Updater window = new Updater();
window.frame.setVisible(true);
} catch (Exception e) {
System.out.println("error class Updater try/catch initialize frame");
}
} else {
System.out.println("Version check correct, no update needed");
}
}
});
}
public Updater() {
initialize();
}
private void initialize() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
| UnsupportedLookAndFeelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.SOUTH);
panel.setLayout(new BorderLayout(0, 0));
JProgressBar progressBar = new JProgressBar();
progressBar.setStringPainted(true);
panel.add(progressBar, BorderLayout.NORTH);
}
}
Is it possible? Yes. Is it possible when using Scanner.next() to read the contents of the URL? No.
You will need to read the bytes yourself, and count them:
URL url = new URL("SOME_URL.txt");
URLConnection conn = url.openConnection();
ByteBuffer buffer = ByteBuffer.allocate(conn.getContentLength());
EventQueue.invokeLater(() -> progressBar.setMaximum(buffer.limit()));
EventQueue.invokeLater(() -> progressBar.setValue(0));
try (ReadableByteChannel channel = Channels.newChannel(conn.getInputStream())) {
while (channel.read(buffer) >= 0) {
EventQueue.invokeLater(() -> progressBar.setValue(buffer.position()));
}
}
buffer.flip();
String rawG = StandardCharsets.UTF_8.decode(buffer).toString();
We are using foxtrot package for stop freeze the swing application.
But in this below code it make a deadlock.
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import foxtrot.Task;
import foxtrot.Worker;
public class FoxtrotExample extends JFrame {
public static void main(String[] args) {
FoxtrotExample example = new FoxtrotExample();
example.setVisible(true);
}
boolean st = true;
public FoxtrotExample() {
super("Foxtrot Example");
final JButton button = new JButton("Take a nap !");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Start..");
button.setText("Sleeping...");
String text = null;
try {
text = (String) Worker.post(new Task() {
public Object run() throws Exception {
System.out.println("Inside Worker 1");
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
try {
System.out.println("Inside invokeLater");
Worker.post(new Task() {
#Override
public Object run()
throws Exception {
System.out.println("Inside Worker 2");
st = false;
return null;
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
});
while (st) {
System.out.println("Inside the loop..");
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
return "Slept !";
}
});
} catch (Exception x) {
}
button.setText(text);
System.out.println("Finished.....");
}
});
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new GridBagLayout());
c.add(button);
setSize(300, 200);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
Dimension size = getSize();
int x = (screen.width - size.width) >> 1;
int y = (screen.height - size.height) >> 1;
setLocation(x, y);
}
}
If use ConcurrentWorker this will work fine.Can any one explane this.
I am bit confuse how EDT behave here ?
This is the result of my program.
Start 1st worker
In the loop
Start invoke later
In the loop
In the loop
In the loop
In the loop
......
It start the 1st worker.Then part of the code is in invokeLater.So request is enqued in the event queue and start the loop.Later execute the invokeLater but not execute the 2nd worker because first worker still doing some work.Since worker are ruining one after another and it runs on a single worker queue 2nd worker cannot execute and deadlock comes.
Thanks to MadProgrammer i understood this.Hope this is correct.
This is a part of my java code, in this code there are labels which are counting numbers from 0 up to so on, I want to stop labels to count when I click the button 1st time, and I want to restart the labels to count again when I click the button 2nd time, the problem is that the labels are not restarting there counting when I am clicking the button 2nd time, so please tell how should I notify all the labels to restart there counting???
import java.awt.Color;
import java.awt.FlowLayout;
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.SwingWorker;
public class Main implements ActionListener {
JButton button = new JButton("Click");
JFrame frame = new JFrame();
boolean wait=false;
public static void main(String arg[]) {
new Main();
}
public Main() {
frame.setLayout(new FlowLayout());
frame.getContentPane().setBackground(Color.BLACK);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
button.addActionListener(this);
frame.add(button);
frame.setVisible(true);
new Producer().execute();
}
public class Producer extends SwingWorker<Void, Void> {
public Void doInBackground() {
for(int infinite=0; infinite!=-1; infinite++) {
new Counter().execute();
try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}
}
return null;
}
}
public class Counter extends SwingWorker<Void, Void> {
JLabel label = new JLabel();
public Counter() {
label.setForeground(Color.WHITE);
frame.add(label);
}
public Void doInBackground() {
synchronized (this) {
for(int i=0; i!=-1; i++) {
if(wait==true)
try {this.wait();} catch(Exception exp) {exp.printStackTrace();}
label.setText(""+i);
try {Thread.sleep(200);} catch (InterruptedException e) {e.printStackTrace();}
}
}
return null;
}
}
public void actionPerformed(ActionEvent clicked) {
if(wait==false)
wait=true;
else if(wait==true) {
synchronized (this) {
this.notifyAll();
}
}
}
}
I don't see a place where you ever reset wait to false. Try this and see if it gets you unstuck:
public void actionPerformed(ActionEvent clicked) {
if(wait==false) {
wait=true;
} else {
wait=false;
synchronized (this) {
this.notifyAll();
}
}
}
I have a desktop application in which I am showing one Frame as notification but I want to stop that thread when mouse hover to the notification frame. Then how could I can do that?
Here current code:
final Notification not = new Notification();
Notification.notification_name.setText(msg[1]);
final ScheduledExecutorService s = Executors.newSingleThreadScheduledExecutor();
s.schedule(new Runnable() {
public void run() {
not.setVisible(false); //should be invoked on the EDT
not.dispose();
}
}, 6, TimeUnit.SECONDS);
It's showing and Exit in 6 sec.
The code that I am trying.
final Notification not = new Notification();
not.addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent e) {
try {
super.mouseEntered(e);
System.out.println("Mouse Entered");
//s.wait();
new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(10000);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
} catch (Exception x) {
x.printStackTrace();
}
}
#Override
public void mouseExited(MouseEvent e) {
try {
super.mouseExited(e);
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
final ScheduledExecutorService s = Executors.newSingleThreadScheduledExecutor();
s.schedule(new Runnable() {
public void run() {
not.setVisible(false); //should be invoked on the EDT
not.dispose();
}
}, 6, TimeUnit.SECONDS);
In this code Notification frame is showing till 6 sec but if user hover that frame then it should be Show till user mouse exit from that Frame.
Whenever you deal with anything that might affect the UI in some way, you need to be careful and ensure that everything is done within the context of the EDT.
javax.swing.Timer allows you to setup a callback at some time in the future that when triggered, will be called within the context of the EDT
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;
public class TimeExample {
public static void main(String[] args) {
new TimeExample();
}
public TimeExample() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
frame.add(new TestPane());
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private Timer timer;
public TestPane() {
setBorder(new LineBorder(Color.BLACK));
timer = new Timer(6000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
setBackground(Color.RED);
}
});
timer.setRepeats(false);
MouseAdapter ma = new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent e) {
if (timer.isRunning()) {
timer.stop();
setBackground(Color.BLUE);
}
}
#Override
public void mouseExited(MouseEvent e) {
if (!timer.isRunning()) {
timer.restart();
setBackground(UIManager.getColor("Panel.background"));
}
}
/**
* Testing purposes only!!
*
* #param e
*/
#Override
public void mouseClicked(MouseEvent e) {
setBackground(UIManager.getColor("Panel.background"));
timer.restart();
}
};
addMouseListener(ma);
timer.start();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(100, 100);
}
}
}
Have a look at How to Use Swing Timers for more details
I have a tabbed Jframe named Version3 which implements Runnable. Into it i have 3 JPanels in different tabbs.Next to those tabs i have a textarea.
I want my GUI to listen for messages and display them in the textarea. I tried to make my GUI Version3 a server which listens all the time in case it receives any message from client.
java.awt.EventQueue.invokeLater(new Runnable(){
public void run(){
Version3 v=new Version3();
v.setVisible(true);
v.listenTo();
}
});
I made my GUI Version3 a server but when i run the program the components of the GUI doesn't show until it's connected to client.I just have a blank GUI window with no components. Any ideas how to display all my components on my GUI without a client connected?
I made my GUI Version3 a server but when i run the program the
components of the GUI doesn't show until it's connected to client.I
just have a blank GUI window with no components. Any ideas how to
display all my components on my GUI without a client connected?
I think it's more than likely that you're blocking the Event Dispatching Thread (a.k.a. EDT) while your class is trying to connect to the client. That's the reason why it works when you have connection but it doesn't when you haven't. The EDT is a single and special thread where Swing component creation and updates take place. If you have a heavy task running in the EDT then your GUI will freeze and Swing components won't be able to work (or even display).
Take a look to Concurrency in Swing trail to learn about concurrency in Swing.
Off-topic: please consider add your code in future questions. As #alex2410 suggested it's better if you include a SSCCE demonstrating your problem.
Server:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Server extends JPanel {
Socket socket;
final static int PORT = 2325;
PrintWriter pr;
public ServerSocket serverSocket;
JButton btn_sendHello;
int counter;
Thread thread;
public Server() {
counter = 0;
btn_sendHello = new JButton("Send hello");
btn_sendHello.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (pr != null) {
pr.println("Hello from server " + ++counter);
}
}
});
this.add(btn_sendHello);
try {
serverSocket = new ServerSocket(PORT);
thread = new Thread(waitingClient);
thread.start();
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
Runnable waitingClient = new Runnable() {
#Override
public void run() {
try {
socket = serverSocket.accept();
openStreams();
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
private void openStreams() {
if (socket != null) {
try {
pr = new PrintWriter(socket.getOutputStream(), true);
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.setTitle("Server");
frame.add(new Server());
frame.pack();
frame.setSize(250, 100);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
Client:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class Client extends JPanel {
final static int PORT = 2325;
private Socket socket;
private BufferedReader fromServer;
private JTextField jtfield;
Thread threadReceive;
public Client() {
jtfield = new JTextField(12);
this.add(jtfield);
try {
socket = new Socket("localhost", PORT);
openStreams();
Thread thread = new Thread(receives);
thread.start();
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
}
Runnable receives = new Runnable() {
#Override
public void run() {
while (true) {
synchronized (this) {
if (socket != null) {
processServerInput();
}
}
}
}
};
private void openStreams() {
try {
if (socket != null) {
fromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void processServerInput() {
try {
String line = fromServer.readLine();
while (!(line.equals("Bye"))) {
jtfield.setText(line);
line = fromServer.readLine();
}
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
}
void closeStreams() {
try {
fromServer.close();
socket.close();
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.setTitle("Client");
frame.add(new Client());
frame.pack();
frame.setSize(250, 100);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}