I have a problem with this code:
public class Gui_01 extends JFrame {
private JPanel display;
private ActionListener visualizza() {
ActionListener evento = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
display.removeAll();
Thread t = new Thread(new Runnable() {
public void run() {
JPanel visualizza = new JPanel();
visualizza.add(new JLabel("Test", SwingConstants.CENTER));
display.add(visualizza, BorderLayout.SOUTH);
updateProgress(visualizza);
}
}
);
t.start();
}
};
return evento;
}
private void updateProgress(final JPanel visualizza) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
display.add(visualizza, BorderLayout.SOUTH);
}
});
}}
I don't understand why the code don't work properly, the thread t and the thread in updateProgress work fine, but any modification to display won't affect the GUI even with invokeLater.
display.add(visualizza, BorderLayout.SOUTH)
This code don't modify the gui, i know it's normal (due to Swing), but why invokeLater don't work.
Sorry for my bad english, thanks in advance for replys.
First get rid of the display.add(visualizza, BorderLayout.SOUTH); statement in your thread. as you should never update or modify the UI from outside the context of the EDT...
Thread t = new Thread(new Runnable() {
public void run() {
JPanel visualizza = new JPanel();
visualizza.add(new JLabel("Test", SwingConstants.CENTER));
//display.add(visualizza, BorderLayout.SOUTH);
updateProgress(visualizza);
}
}
In fact, I'd discourage your from creating UI elements outside of the EDT, as you can't guarantee when they might start interacting with the UI.
Second, call revalidate and repaint after you've updated the UI...
private void updateProgress(final JPanel visualizza) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
display.add(visualizza, BorderLayout.SOUTH);
display.revalidate();
display.repaint();
}
});
}}
Swing is lazy when it comes to updating the UI, this is a good thing, as it allows you to make sweeping changes and only update the UI when you're ready to do so.
I'd also encourage you to use a SwingWorker instead, as it has functionality which you can use to synchronise the updates with the EDT. See Worker Threads and SwingWorker for more details
Related
This is a very simplified version of my code to get a better understanding of what I'm doing wrong here. The GUI freezes if the button is pressed. I need to be able to run a while loop if the button is pressed without freezing.
class obj1 extends Thread{
public void run(){
while(true) {
System.out.println("this thread should run when the button is pressed and I should be able to press another button");
}
}
}
class GUI extends Thread{
JFrame frame = new JFrame();
JButton button = new JButton("test1");
JButton button2 = new JButton("test2");
JPanel panel = new JPanel();
String command;
public void run() {
frame.setVisible(true);
panel.add(button);
panel.add(button2);
frame.add(panel);
frame.pack();
buttonOnAction();
}
public void buttonOnAction(){
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
obj1 one = new obj1();
one.start();
one.run();
}
});
button2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
obj1 one2 = new obj1();
one2.start();
one2.run();
}
});
}
}
public class Main{
public static void main(String args[]){
GUI gui = new GUI();
gui.start();
gui.run();
}
}
Why does the GUI freeze?
Don't call run() directly on your Thread object. This immediately executes the run() method and doesn't spawn a new thread. Instead, just call start() as you have and let the system create the thread and call run() when it decides to.
It is also worth pointing out that the proper way to schedule graphical work in Swing is to make sure it ends up on the event dispatch thread. To do this properly, use SwingUtilities#invokeLater(Runnable), which will not wait for the work to complete, or SwingUtilities#invokeAndWait(Runnable), which will.
I have two jframe windows and I want to hide the NewJFrameSplash window after a while but I am having trouble succeeding with setVisible(true)
What can I be doing wrong?
as the code shows I start the NewJFrameSplash window and at the end of a while start the NewJFrame and hide the NewJFrameSplash.
static NewJFrameSplash frame;
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrameSplash().setVisible(true);
}
});
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
new NewJFrame().setVisible(true);
frame = new NewJFrameSplash();
frame.setVisible(false);
}
Don't use Thread.sleep(5000);, you run the risk of blocking the Event Dispatching Thread, or, if you're using a second thread, violating the single threaded nature of the API.
Instead, use a Swing Timer instead.
new NewJFrame().setVisible(true); is just going to make a new window and make it visible, twice. You need to use setVisible(false) on the instance of the window you've created earlier.
As a basic example...
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
NewJFrameSplash frame = new NewJFrameSplash();
frame.setVisible(true);
javax.swing.Timer timer = new javax.swing.Timer(5000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
// Create next window
}
});
timer.setRepeats(false);
timer.start();
}
});
Take a look at How to use Swing Timers for more details
Swing also provides "splash screen" support. You should take a closer look at How to create a splash screen for more details
Hello guys I am doing a thread to update a ball over JFrame so I repaint the screen... and then paint the ball update its position .. and then draw the screen again ... draw the ball and the same cycle ... here is the code
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
Thread t = new Thread()
{
public void run()
{
while(true)
{
repaint();
b2.update(ob,2);
b2.paint(ob.getGraphics());
b2.setT(b2.getT() + 1);
try {
Thread.sleep(50);
} catch (InterruptedException ex) {
System.out.println("Error in Sleeping");
}
}
}
};
t.start();
}
but the problem is that I don't see the ball... the paint of the screen always overrides the ball and the ball is like down under the Jframe ..
If you want to have animations in Swing, the recommended class to use is the javax.swing.Timer . This class allows you to perform operations on the Event Dispatch Thread at regular intervals.
The Swing Timer tutorial
An animation example posted here on SO (which is linked in the Swing wiki here on SO btw)
Some General Rules
Swing is not thread safe, you should only ever update UI components from within the context of the Event Dispatching Thread.
You do not control the paint process, the repaint manager does. You can request updates to occur by calling repaint, but you should never call update and paint directly when trying to update the display.
The Graphics context used by the paint sub system is a shared resource and is not guaranteed to be the same between paint cycles, you should never maintain a reference to it. You should also not rely on the results from JComponent#getGraphics this method is capable of returning null.
An Example Solution
You have a number of options, depending on what you want to ultimately achieve.
You could use a SwingWorker, but given the fact that all your going to is enter an infinite loop and it would easier to use SwingUtilities#invokeLater then actually use the publish method, this approach would actually be more work.
You could also use a Thread, but you'd end up with the same problems as using a SwingWorker
The simpliset solution, for what you're presented, is actually a javax.swing.Timer
public class Blinky {
public static void main(String[] args) {
new Blinky();
}
public Blinky() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new BlinkyPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
protected class BlinkyPane extends JPanel {
private JLabel blinkyLabel;
private boolean blink = false;
public BlinkyPane() {
setLayout(new GridBagLayout());
blinkyLabel = new JLabel("I'm blinking here");
blinkyLabel.setBackground(Color.RED);
add(blinkyLabel);
Timer timer = new Timer(250, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
blink = !blink;
if (blink) {
blinkyLabel.setForeground(Color.YELLOW);
} else {
blinkyLabel.setForeground(Color.BLACK);
}
blinkyLabel.setOpaque(blink);
repaint();
}
});
timer.setRepeats(true);
timer.setCoalesce(true);
timer.start();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 100);
}
}
}
You can take a look at Swing Timer and Concurrency in Swing for more info
If you access GUI components outside the EDT (Event Dispatch Thread) then you might encounter strange problems, Contrary if you perform long running tasks in the EDT then also you will get problems.
Check this post for more info on GUI Threading in Java
I am using swing timer to to load different pdf files in swing application
but I am facing a problem when ever I execute a program the screen remains blank for few seconds like 4 to 5 seconds and then the pdf file is rendered so during this time I want to show a message like please wait. Here is my sample code
if (type[i].equalsIgnoreCase("PDF")) {
int k = i;
pdfTimer = new Timer(0, (ActionEvent e) -> {
renderPDF(k);
});
pdfTimer.setDelay(1000*2);
pdfTimer.start();
Run rendering on SwingWorker's background thread (doInBackground) method. That way, your GUI will remain responsive. From done method you can notify user that rendering is done. Keep in mind not to update any Swing GUI from doInBackground method since it runs outside of EDT.
P.S. Swing Timer is for repetitive tasks.
You can display a progress bar and compute the renderding on a second thread.
JLabel lblWait = new JLabel("Please wait...");
lblWait .setBounds(116, 26, 113, 14);
contentPanel.add(lblWait );
final JProgressBar progressBar = new JProgressBar();
progressBar.setStringPainted(true);
progressBar.setBounds(72, 66, 187, 16);
contentPanel.add(progressBar);
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
final JButton btFin = new JButton("Cancel");
btFin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
dispose();
}
});
buttonPane.add(btFin);
}
}
Thread t = new Thread() {
public void run() {
//You launch the thread with your progress bar as an argument
maStructure.setaAfficher(new MgtSimulation(aAfficher, nombreSimulations).jouerSimulations(progressBar));
maStructure.actualiserAffichage();
dispose();
}
};
t.start();
}
And you change your progress bar value in your method
public BeanAffichage jouerSimulations(JProgressBar progressBar){
//Variables
for (int i = 0; i < nombreSimulations; i++) {
//Computing
progressBar.setValue(Whatever you want);
}
return aAfficher;
}
Just an alternative for SwingWorker
By default show some message like Loading... and create a Thread to run in the background which loads the PDF and updates the window
class BackgroundThread implements Runnable {
#Override
public void run() {
// the Swing call below must be queued onto the Swing event thread
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run() {
// OK To make Swing method calls here
loadFile(args..);
repaint();//Or something similar that fits your purpose
}
});
}
}
I'm working in Java, and I have a JPanel in a JFrame. In that JPanel, among other things, I have a JLabel that I want to make appear and disappear at will. I've tried setting visibility to true/false, adding and removing it from the JFrame and JPanel, and, having looked online, I tried validate()ing and revalidate()ing ad infinitum. What can be done here to solve this problem?
In general, calling the setVisible method is sufficient to make a Swing component to be shown or hidden.
Just to be sure that it works, I tried the following:
public class Visibility {
private void makeGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JLabel l = new JLabel("Hello");
final JButton b = new JButton("Hide Label");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
l.setVisible(false);
}
});
f.getContentPane().setLayout(new BorderLayout());
f.getContentPane().add(l, BorderLayout.CENTER);
f.getContentPane().add(b, BorderLayout.SOUTH);
f.setSize(200, 200);
f.setLocation(200, 200);
f.validate();
f.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Visibility().makeGUI();
}
});
}
}
The above program is able to affect the visibility by clicking on a JButton.
Could it be a Threading Issue?
My next suspicion was that perhaps a Thread that is not on the event dispatch thread (EDT) may not be affecting the display immediately, so I added the following after initializing the JLabel and JButton.
Thread t = new Thread(new Runnable() {
public void run() {
while (true) {
b.setVisible(!b.isVisible());
try {
Thread.sleep(100);
} catch (InterruptedException e) { /* Handle exception /* }
}
}
});
t.start();
With the new Thread running, it changed the toggled the visibility of the JLabel every 100 ms, and this also worked without a problem.
Calling a Swing component off the event dispatch thread (EDT) is a bad thing, as Swing is not thread-safe. I was a little surprised it worked, and the fact that it works may just be a fluke.
Repaint the JPanel?
If the JLabel's visibility is only being affected on resizing, it probably means that the JLabel is being drawn only when the JPanel is being repainted.
One thing to try is to call the JPanel's repaint method to see if the visibility of the JLabel will change.
But this method seems to be just a band-aid to a situation, if the main cause is due to a thread off the EDT is attempting to make changes to the GUI. (Just as a note, the repaint method is thread-safe, so it can be called by off-EDT threads, but relying on repaint is a workaround than a solution.)
Try using SwingUtilities.invokeLater
Finally, probably the thing I would try is the SwingUtilities.invokeLater method, which can be called (and should only be called) from a thread running separate from the EDT, if it wants to affect the GUI.
So, the earlier Thread example should be written as:
Thread t = new Thread(new Runnable() {
public void run() {
while (true) {
try {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
b.setVisible(!b.isVisible());
}
});
} catch (Exception e1) { /* Handle exception */ }
try {
Thread.sleep(100);
} catch (InterruptedException e) { /* Handle exception */ }
}
}
});
t.start();
If the change to the GUI is indeed occurring on a separate thread, then I would recommend reading Lesson: Concurrency in Swing from The Java Tutorials in order to find out more information on how to write well-behaving multi-threaded code using Swing.
setVisible() or removing it should work fine, make sure you are doing it from the event dispatch thread though. There are utility methods in EventQueue for running blocks in that thread.
http://helpdesk.objects.com.au/java/how-do-i-update-my-gui-from-any-thread
You would need to call revalidate() on the parent JPanel if you need its components to be re-laid out.
If you can post an example that demonstrates the problem I can have a look at it for you.