I've been trying for at least an hour on refreshing my simple Jframe. I have tried repaint() revalidate; and just about anything else on the internet.
here is my entire class:
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public final class BRUTEFORCE {
static int Passwords = 0;
static JFrame frame;
public static void main(String[] args) {
//Create and set up the window.
frame = new JFrame("Simple GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel textLabel = new JLabel("Passwords tried: " + Passwords,SwingConstants.CENTER);
textLabel.setPreferredSize(new Dimension(300, 100));
frame.getContentPane().add(textLabel, BorderLayout.CENTER);
//Display the window.
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
Passwords++;
new Thread("Refresh") {
public void run () {
while(true){
frame.invalidate();
frame.validate();
frame.repaint();
}
}
}.start();
new Thread("Test") {
public void run () {
while(true) Passwords++;
}
}.start();
}
}
What am I doing wrong?
Two things are going wrong. You're first thread is constantly feeling the Event Queue with update requests, probably faster then the Event Queue can process them which may eventually flood it, degrading the performance of the system.
Secondly, you never actually change the text of the textLabel
For example...
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
public class BruteForce {
static transient int Passwords = 0;
static JFrame frame;
public static void main(String[] args) {
//Create and set up the window.
frame = new JFrame("Simple GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JLabel textLabel = new JLabel("Passwords tried: " + Passwords, SwingConstants.CENTER);
textLabel.setPreferredSize(new Dimension(300, 100));
frame.getContentPane().add(textLabel, BorderLayout.CENTER);
//Display the window.
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
Passwords++;
new Thread("Test") {
public void run() {
while (true) {
try {
Passwords++;
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
textLabel.setText("Passwords tried: " + Passwords);
}
});
Thread.sleep(5);
} catch (InterruptedException ex) {
Logger.getLogger(BruteForce.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}.start();
}
}
Now, instead of a Thread, you might consider using a SwingWorker instead...
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
public class BruteForce {
static transient int Passwords = 0;
static JFrame frame;
public static void main(String[] args) {
//Create and set up the window.
frame = new JFrame("Simple GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JLabel textLabel = new JLabel("Passwords tried: " + Passwords, SwingConstants.CENTER);
textLabel.setPreferredSize(new Dimension(300, 100));
frame.getContentPane().add(textLabel, BorderLayout.CENTER);
//Display the window.
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
SwingWorker worker = new SwingWorker<Integer, Integer>() {
#Override
protected void process(List<Integer> chunks) {
// Only care about the last one..
int value = chunks.get(chunks.size() - 1);
textLabel.setText("Passwords tried: " + value);
}
#Override
protected Integer doInBackground() throws Exception {
while (true) {
// Perform long running process...
// Forced delay to simulate long running process
Thread.sleep(5);
Passwords++;
publish(Passwords);
}
}
};
worker.execute();
}
}
Related
I am creating a Java program using Swing and want to open a JFrame, run a for-loop that stores items in a list of Strings, display graphics while that is happening, and only after that is done call another method. The issue is that even though I call frame.setVisible(true); before running the loop, it only displays after the loop is done. I would use a SwingWorker but I need to stop the main thread from running the next method until after the loop is finished. If someone knows a way to use SwingWorker or knows a fix to this, that would be great. Here is the code I am referring to:
//The JPanel in charge of displaying graphics while the loop is running
FrameRenderer renderer = new FrameRenderer(videoFile, this.getWidth(), this.getHeight());
this.add(renderer);
this.setVisible(true);
//Call the method with the for-loop after this.setVisible is called
List<String> frames = renderer.renderFrames();
//I need this to run after the loop is finished
DisplayFrames display = new DisplayFrames(frames, this.getWidth(), this.getHeight(), this);
this.add(display);
SwingWorker
Worker Threads and SwingWorker
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingWorker;
import javax.swing.border.EmptyBorder;
public final class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new MainPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class MainPane extends JPanel {
private JLabel label = new JLabel("...");
public MainPane() {
setLayout(new GridBagLayout());
setBorder(new EmptyBorder(32, 64, 32, 64));
add(label);
SwingWorker<Void, String> worker = new SwingWorker<>() {
#Override
protected Void doInBackground() throws Exception {
for (int index = 0; index < 1000; index++) {
publish(Integer.toString(index));
// It's important, if you want to allow the UI to
// update on a single view, you need to allow time
// for this thread to sleep, otherwise, you could
// end up in a siutatio where the only update you
// get is the last one (with a list of all the
// the values you "published"
Thread.sleep(10);
}
return null;
}
#Override
protected void process(List<String> chunks) {
label.setText(chunks.get(chunks.size() - 1));
}
};
worker.addPropertyChangeListener(new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent evt) {
if (worker.getState() == SwingWorker.StateValue.DONE) {
label.setText("All done here");
}
}
});
worker.execute();
}
}
}
Swing Timer
How to Use Swing Timers
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;
public final class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new MainPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class MainPane extends JPanel {
private JLabel label = new JLabel("...");
public MainPane() {
setLayout(new GridBagLayout());
setBorder(new EmptyBorder(32, 64, 32, 64));
add(label);
Timer timer = new Timer(10, new ActionListener() {
private int value = 0;
#Override
public void actionPerformed(ActionEvent e) {
if (value >= 1000) {
((Timer)(e.getSource())).stop();
label.setText("All done here");
}
label.setText(Integer.toString(value));
value++;
}
});
timer.start();
}
}
}
I want to update the Jlabel text in every second as long as the loop is running. how could I do this? I want to do as this fromat.
JPanel jpnl=new JPanel();
jfrm.add(jpnl);
String[] fonts=GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
jlab = new JLabel("This is Label");
jpnl.add(jlab);
for(int i=0;i<fonts.length;i++){
System.out.println(fonts[i]);
jlab.setText(fonts[i]);
jlab.setFont(new Font(fonts[i],Font.PLAIN,30));
jlab.setForeground(Color.DARK_GRAY);
}
Swing's single threaded nature precludes using a loop or Thread.sleep in the way you seem to be trying. Doing so, will simply block the UI and prevent it from been painted/updated until the loop is completed.
Because Swing is not thread safe, you can't simply use another Thread and the above approaches to update the UI, without jumping through some hoops
The conical answer to your question is to use a Swing Timer, which triggers an update at a regular bases. Because these updates are triggered within the context of the Event Dispatching Thread, it makes it safe to use when you want to update the UI.
Take a closer look at How to use Swing Timers for more details
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private String[] fonts;
private final JLabel jlab;
private int index = 0;
public TestPane() {
setLayout(new GridBagLayout());
fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
jlab = new JLabel("This is Label");
add(jlab);
Timer timer = new Timer(1000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
updateFont();
index++;
if (index >= fonts.length) {
((Timer)e.getSource()).stop();
}
}
});
timer.setInitialDelay(0);
timer.start();
}
protected void updateFont() {
System.out.println(fonts[index]);
jlab.setText(fonts[index]);
jlab.setFont(new Font(fonts[index], Font.PLAIN, 30));
jlab.setForeground(Color.DARK_GRAY);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
I have one JFrame that's not actively rendered, a la standard basic Swing applications, which I need to launch another JFrame when a button is clicked. The second JFrame is actively rendered using Swing's BufferStrategy, and runs on its own independently - however, when I call it from the other JFrame's ActionPerformed both JFrames freeze.
I know there are complications in using Swing to accomplish this kind of behavior - how can I get around them?
You might be able to adapt this Swing based Launcher that uses
ProcessBuilder to run programs in a separate JVM.
package gui;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
* #see http://stackoverflow.com/a/5696404/230513
*/
public class Launcher extends JPanel implements Runnable {
private final JLabel label = new JLabel();
private final JButton launch = new JButton();
ProcessBuilder pb = new ProcessBuilder(
"java", "-cp", "build/classes", "gui.Launcher$DialogTest");
private volatile Process proc;
public static void main(String[] args) {
EventQueue.invokeLater(new Launcher()::createGUI);
}
private void createGUI() {
final JFrame frame = new JFrame();
frame.setLayout(new GridLayout(0, 1));
frame.add(new Launcher());
frame.add(new Launcher());
frame.add(new Launcher());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public Launcher() {
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
this.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
launch.setAlignmentX(0.5f);
label.setAlignmentX(0.5f);
launch.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (proc == null) {
launch.setText("Terminate");
label.setText("Status: run");
new Thread(Launcher.this).start();
} else {
proc.destroy();
reset();
}
}
});
this.add(launch);
this.add(label);
reset();
}
#Override
public void run() {
try {
proc = pb.start();
proc.waitFor();
} catch (IOException | InterruptedException ex) {
ex.printStackTrace(System.err);
}
EventQueue.invokeLater(this::reset);
}
private void reset() {
proc = null;
launch.setText("Launch");
label.setText("Status: idle");
}
private static class DialogTest {
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
JOptionPane.showMessageDialog(null, "Running",
"Test", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
});
}
}
}
Updated to Java 8 at the author's request.
How do I define a keyboard shortcut for top level special keys like german umlaut key Ä? I found a way to map unicode letters that are used for default american layout keys, see here. But the key event for the german umlaut key Ä is:
java.awt.event.KeyEvent[KEY_PRESSED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar='ä',keyLocation=KEY_LOCATION_STANDARD,rawCode=222,primaryLevelUnicode=228,scancode=40] on frame0
The idea is to register a keyboard action:
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
public class KeyStrokeForGermanUmlaut {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.setPreferredSize(new Dimension(600, 400));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
final JLabel label = new JLabel("Text shall change with shortcut");
panel.add(label);
panel.registerKeyboardAction(new AbstractAction() {
#Override
public void actionPerformed(ActionEvent event) {
label.setText("It is working!!!");
}
}, KeyStroke.getKeyStroke("control typed Ä"), JComponent.WHEN_IN_FOCUSED_WINDOW);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
});
}
}
you can to conjuring with JLabel, nothing happends for KeyEvents
should be start point with moving the focus to JFrames ContentPane (can be used as JPanel, but has BorderLayout in compare with plain JPanel - FlowLayout)
-
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
public class KeyStrokeForGermanUmlaut {
private JFrame frame = new JFrame();
private JLabel label = new JLabel("Text shall change with shortcut");
public KeyStrokeForGermanUmlaut() {
frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.CTRL_MASK), "CTRL + A");
frame.getRootPane().getActionMap().put("CTRL + A", updateCol());
frame.setPreferredSize(new Dimension(600, 100));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(label);
frame.pack();
frame.setLocation(150, 150);
frame.setVisible(true);
}
private Action updateCol() {
return new AbstractAction("Hello World") {
private static final long serialVersionUID = 1L;
#Override
public void actionPerformed(ActionEvent e) {
label.setText(label.getText() + " presses");
}
};
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new KeyStrokeForGermanUmlaut();
}
});
}
}
.
.
EDIT see description in API getKeyStroke/ v.s. getKeyStrokeForEvent
then result is could be (little bit lost when and how to use modifiers SHIFT with uppercase form (ä and ú) for those two chars, maybe someone will help us with those pieces of KeyEvents)
from
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
public class KeyStrokeForGermanUmlaut {
private JFrame frame = new JFrame();
private JLabel label = new JLabel("Text shall change with shortcut");
public KeyStrokeForGermanUmlaut() {
frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke("typed ä"), "typed ä");
frame.getRootPane().getActionMap().put("typed ä", updateCol());
frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke("typed ú"), "typed ú");
frame.getRootPane().getActionMap().put("typed ú", updateCol1());
frame.setPreferredSize(new Dimension(600, 100));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(label);
frame.pack();
frame.setLocation(150, 150);
frame.setVisible(true);
}
private Action updateCol() {
return new AbstractAction("Hello World") {
private static final long serialVersionUID = 1L;
#Override
public void actionPerformed(ActionEvent e) {
label.setText(label.getText() + " presses - ä");
}
};
}
private Action updateCol1() {
return new AbstractAction("Hello World") {
private static final long serialVersionUID = 1L;
#Override
public void actionPerformed(ActionEvent e) {
label.setText(label.getText() + " presses - ú");
}
};
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new KeyStrokeForGermanUmlaut();
}
});
}
}
I am afraid there is something fishy in the handling of modifiers for CTRL.
That is: when inspecting the received key modifier=InputEvent.CTRL_MASK, extended modifier=InputEvent.CTRL_DOWN_MASK. And the API's javadoc is a bit suspicious.
Apart from that, Ä is not a special case, when "control" is left out.
To make it work, I had to add a dirty hack: register a key listener, that calls the action itself. I must be overseeing something.
For the rest I used an InputMap/ActionMap as intended. The input map does not seem to work, but to my understanding it does not work if added to a JTextField, or in the other answer (for Ä). The following works - in a horrible way.
final JLabel label = new JLabel("Text shall change with shortcut");
final KeyStroke key = KeyStroke.getKeyStroke((Character)'k',
InputEvent.CTRL_DOWN_MASK, false);
final Object actionKey = "auml";
final Action action = new AbstractAction() {
#Override
public void actionPerformed(ActionEvent event) {
System.out.println("aha");
label.setText("It is working!!!");
}
};
label.addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(java.awt.event.KeyEvent e) {
if (e.isControlDown() && e.getKeyChar() == 'ä') {
System.out.println("Ctrl-ä");
label.getActionMap().get(actionKey).actionPerformed(null);
// return;
}
super.keyPressed(e);
}
});
label.getInputMap().put(key, actionKey);
label.getActionMap().put(actionKey, action);
Ä, \xC4, Ä, Ä, %C4, %C3%84
There are the code for Ä. Please try with this, maybe one them will work for you.
How to edit the JLabel every seconds like (time left or score) in some games.
this is my code
static int l = 1;
static int s = 5000;
static int t = 90;
public static void main(String[] args) {
//Frame
final JFrame f = new JFrame();
f.setTitle("Picture Puzzle");
f.setSize(500,500);
f.setLocationRelativeTo(null);
f.setResizable(false);
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
f.setVisible(true);
//some extra stuffs here
JLabel blevel00 = new JLabel("Level:" + l);
JLabel bscore00 = new JLabel("Score:" + s);
JLabel btime00 = new JLabel("Time:" + t);
p2.add(blevel00);
p2.add(bscore00);
p2.add(btime00);
//some extra stuffs here
start.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
while(t != 0 ) { //the t is the static int t = 90;
f.add(p2);
f.remove(p1);
f.setVisible(true);
f.revalidate();
f.repaint();
}
t--;
}
});
}
}
I tried this and nothing happens. any help will be appreciated.
Swing is a single threaded environment, that is, all alterations and modifications to the UI are expected to occur within the context of the Event Dispatching Thread.
Anything that blocks this thread, like a never ending loop or blocking I/O will prevent this thread from processing new events, including paint events.
Swing provides a number of solutions to this problem, in your case the best solution is probably to use a javax.swing.Timer. This will allow you to schedule a regular callback that is called within the context of the EDT, allowing you to make modifications to the UI on a regular bases.
Take a look at Concurrency in Swing and How to use Swing Timers for more details
Update with simple example
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class SimpleClock {
public static void main(String[] args) {
new SimpleClock();
}
public SimpleClock() {
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 BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JLabel time;
public TestPane() {
setLayout(new GridBagLayout());
time = new JLabel();
time.setFont(time.getFont().deriveFont(Font.BOLD, 48));
add(time);
updateTime();
Timer timer = new Timer(500, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
updateTime();
}
});
timer.start();
}
protected void updateTime() {
time.setText(DateFormat.getTimeInstance().format(new Date()));
}
}
}