When I run my autoclicker I can't stop it - java

If I click on start button I can't click back or stop. I need to stop the app. One way to stop the app is that you must power off computer :D. When the app is running, start button is locked and I can't click on it. Click on the exit button doesn't work. This app is simple AutoClicker. Exist in java something better to do autoclicker than robot class? How can I fix it? Is there error anywhere? Or is there something I can do to stop the app?
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Robot;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.Color;
public class AutoClicker extends JFrame {
private JPanel contentPane;
private final JLabel m = new JLabel("AutoClicker");
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AutoClicker frame = new AutoClicker();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public AutoClicker() {
setAlwaysOnTop(false);
setTitle("AutoClicker");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 147, 162);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JCheckBox chckbxOnTop = new JCheckBox("On Top");
boolean onTop = false;
chckbxOnTop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(chckbxOnTop.isSelected()){
setAlwaysOnTop(true);
}
else{
setAlwaysOnTop(false);
}
}
});
chckbxOnTop.setBounds(6, 7, 97, 23);
contentPane.add(chckbxOnTop);
JCheckBox chckbxAutoclicker = new JCheckBox("AutoClicker");
chckbxAutoclicker.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
while(chckbxAutoclicker.isSelected()){
try {
Robot r = new Robot();
r.mousePress(MouseEvent.BUTTON1_MASK);
r.setAutoDelay(1080);
r.mouseRelease(MouseEvent.BUTTON1_MASK);
} catch (AWTException e1) {
e1.printStackTrace();
}
}
}
});
chckbxAutoclicker.setBounds(6, 80, 97, 23);
contentPane.add(chckbxAutoclicker);
m.setForeground(new Color(153, 102, 0));
m.setBounds(16, 92, 120, 31);
contentPane.add(m);
}
}

Swing is single threaded - calling any long running task on that thread will lock that thread up (the EDT) and prevent any painting, events, etc... from occurring. One of the ActionListener implementations creates an infinite loop:
while(chckbxAutoclicker.isSelected()){
The above will never evaluate to false, because it is evaluating on the EDT, and events (such as disabling the JCheckBox to allow this method to return false) cannot occur until the EDT is free. If you wish to continually run a task while allowing the EDT to performs its necessary tasks, you have three options:
Create a new Thread. Note any calls to Swing from this thread should be dispatched to the EDT using SwingUtilities.invoke*
Use a SwingWorker
If you wish to do something at a later time on the EDT, or run something on a schedule on the EDT, use a javax.swing.Timer

Related

Toggle the visibility of Jlabel and JPanel

I am trying to toggle the visibility of JLable and JPanel. Both are in initial case in visible state. After pressing a Button, they should be invisible for two seconds. After this time they are going to be visible again. In adddition, there are two checkBoxes on the JPanel, which are grouped by ButtonGroup. The selection should be cleared after pressing the Button.
For solving I wrote the following code:
package Aufgaben;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.EmptyBorder;
public class Aufgabe1 extends JFrame {
private JPanel contentPane;
private JPanel checkPanel;
private JPanel pointPanel;
private JRadioButton rdbtnJa;
private JRadioButton rdbtnNein;
private ButtonGroup btnGroup;
private JButton btnStart;
private JLabel lblX;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Aufgabe1 frame = new Aufgabe1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Aufgabe1() {setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 700, 300);
contentPane = new JPanel();
setContentPane(contentPane);
contentPane.setLayout(null);
btnStart = new JButton("Start");
btnStart.setBounds(0, 0, 684, 23);
contentPane.add(btnStart);
checkPanel = new JPanel();
checkPanel.setBounds(0, 229, 684, 33);
contentPane.add(checkPanel);
btnGroup = new ButtonGroup();
rdbtnJa = new JRadioButton("Ja");
checkPanel.add(rdbtnJa);
btnGroup.add(rdbtnJa);
rdbtnNein = new JRadioButton("Nein");
checkPanel.add(rdbtnNein);
btnGroup.add(rdbtnNein);
pointPanel = new JPanel(new BorderLayout());
pointPanel.setBackground(Color.BLACK);
pointPanel.setBounds(0, 23, 684, 209);
contentPane.add(pointPanel);
lblX = new JLabel("X");
lblX.setForeground(Color.WHITE);
lblX.setVerticalAlignment(JLabel.CENTER);
lblX.setHorizontalAlignment(JLabel.CENTER);
pointPanel.add(lblX, BorderLayout.CENTER);
run();
}
public void run() {
System.out.println("----------");
System.out.println("Method run()");
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("btn actionPerformed()");
createLightPoint();
}
});
}
private void createLightPoint() {
System.out.println("----------");
System.out.println("Method createLightPoint()");
btnGroup.clearSelection();
lblX.setVisible(false);
// lblX.repaint();
// pointPanel.repaint();
checkPanel.setVisible(false);
// checkPanel.repaint();
System.out.println("Bevor Sleep");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("After Sleep");
lblX.setVisible(true);
// lblX.repaint();
checkPanel.setVisible(true);
// checkPanel.repaint();
}
}
As you can see I tried to solve the problems (what I described above) over the function .setVisibility() as well .repaint(). I tried the functions .show() and .hide althought they are deprecatet as well.But in any case, it doesn't work.
I already read the following posts:
Show/Hide JLabel with button?
https://softwareengineering.stackexchange.com/questions/233068/is-better-show-hide-or-setvisiblebool-visible
But none of them could help the solve my problem.
I hope you can Help me.
Don't use sleep() on Event Dispatch Thread - it will prevent the thread from processing painting and other UI related events and the UI will become frozen. See The Event Dispatch Thread tutorial for more details.
You can use Swing timer instead, see How to Use Swing Timers tutorial for examples.

(multiple image ) animation Java

I have got a set of nodes in my program, each have a specific x,y location.
and each have a set of image icons.
I want to draw image animation for each nodes at its specific location.
Here is my code: (this only shows the last image which i know why!.)
public void showPicture() {
//nodes :
for(int i=0;i<thisGraph.getNode().size();i++){
if(thisGraph.getNode().get(i).getImageIcon()!=(null)){
for(int j=0;j<thisGraph.getNode().get(i).getImageIcon().size();j++){
if(j>0)
lables.get(lables.size()-1).setVisible(false);
JLabel jLabel1 = new JLabel();
lables.add(jLabel1);
jLabel1.setLayout(new GridBagLayout());
jLabel1.setIcon(thisGraph.getNode().get(i).getImageIcon().get(j));
jLabel1.setVisible(true);
jLabel1.setBounds((int)thisGraph.getNode().get(i).getX(),(int)thisGraph.getNode().get(i).getY(),195,163);
jPanel1.add(jLabel1);
}
}
}
}
This method showPicture() is called in a buttonActionListener.
And I also have another button which I want it to stop the image animations for all labels.
What I have tried:
Thread.sleep() -> it freezes the button and it only shows the last image
I figured I had to use timer, but through all the topics I went they only used it on one label, not multiple labels.
Edit
->
i read those examples given in the comments . and here is what i have resolved but it still is freezes the button and doesn't works :
int j = 0;
public void showPicture(){
//nodes :
for(int i=0;i<thisGraph.getNode().size();i++){
if(thisGraph.getNode().get(i).getImageIcon()!=(null)){
j=0;
while( j<thisGraph.getNode().get(i).getImageIcon().size()){
if(j>0)
lables.get(lables.size()-1).setVisible(false);
JLabel jLabel1 = new JLabel();
lables.add(jLabel1);
jLabel1.setLayout(new GridBagLayout());
jLabel1.setIcon(thisGraph.getNode().get(i).getImageIcon().get(j));
jLabel1.setVisible(true);
jLabel1.setBounds((int)thisGraph.getNode().get(i).getX(),(int)thisGraph.getNode().get(i).getY(),195,163);
jPanel1.add(jLabel1);
//
ActionListener act;
act = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
jLabel1.setVisible(true);
j++;
}
};
Timer timer = new Timer(1000, act );
timer.start();
timer.stop();
//
}
}
}}
Swing is single threaded and not thread safe. This means that you shouldn't block the Event Dispatching Thread with long running or blocking operations, like Thread.sleep. You should also, only ever update the UI (or anything it relies on) from within the context of the Event Dispatching Thread.
See Concurrency in Swing for more details.
Probably the simplest solution to your problem is to use a Swing Timer.
The idea is a you use a single Timer to act as the "main animation loop", changing the properties of ALL the objects you need updated within it.
The following is pretty basic example, it animates 100 JLabels, simply changing their background color with a randomly picked color
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
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 List<JLabel> nodes = new ArrayList<>(100);
private Random random = new Random();
private Color[] colors = new Color[] { Color.RED, Color.GREEN, Color.BLUE, Color.BLACK, Color.MAGENTA};
public TestPane() {
setLayout(new GridLayout(0, 10));
for (int index = 0; index < 100; index++) {
JLabel label = new JLabel();
label.setBorder(new EmptyBorder(5, 5, 5, 5));
label.setOpaque(true);
label.setBackground(pickColor());
nodes.add(label);
add(label);
}
Timer timer = new Timer(500, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
for (JLabel label : nodes) {
label.setBackground(pickColor());
}
}
});
timer.start();
}
protected Color pickColor() {
return colors[random.nextInt(colors.length)];
}
}
}
See How to Use Swing Timers for more details

GUI freeze when JCombobox selected, How to use Timer in Swing Application with Condition Statement in actionListener?

I am working on a login frame where i am retrieving and populating JComboBox with user created databases and then I am setting Text of JLabel on the basis of Item selected from JComboBox.
I want to delay the execution and display the 'Connecting...' text in JLabel when user selects an item from JComboBox but when I select an item the GUI freezes and after 5 seconds it shows 'Connected' It skips the 'Connecting...' JLabel.
A Lot of Thanks in advance.
package com.softoak.dba;
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import javax.swing.JComboBox;
public class Login{
private JFrame frame;
static ArrayList<String> dbcom = new ArrayList<String>();
static String[] usecom;
JLabel status = new JLabel("•");
JLabel lblNotConnected;
/**
* Launch the application.
*/
public static void main(String[] args) throws Exception{
Connection m_Connection = DriverManager.getConnection("jdbc:sqlserver://localhost;integratedSecurity=true");
String dbs = "SELECT * FROM sys.databases WHERE owner_sid != 1";
Statement st = m_Connection.createStatement();
ResultSet m_ResultSet = st.executeQuery(dbs);
dbcom.add("-None-");
while(m_ResultSet.next()){
dbcom.add(m_ResultSet.getString(1));
}
usecom = new String[dbcom.size()];
usecom = dbcom.toArray(usecom);
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Login window = new Login();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Login() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
status.setBounds(385, 235, 10, 10);
status.setForeground(Color.red);
frame.getContentPane().add(status);
JLabel lblLoginApplication = new JLabel("Login Application");
lblLoginApplication.setFont(new Font("Lucida Calligraphy", Font.BOLD, 20));
lblLoginApplication.setBounds(120, 25, 220, 30);
frame.getContentPane().add(lblLoginApplication);
JComboBox comboBox = new JComboBox(usecom);
comboBox.setBounds(230, 80, 110, 30);
comboBox.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
if (comboBox.getSelectedIndex() == 1 || comboBox.getSelectedIndex() == 2) {
lblNotConnected.setText("Connecting...");
try{
Thread.sleep(5000);
}catch(InterruptedException ex){
JOptionPane.showMessageDialog(null,ex.getMessage());
}
status.setForeground(Color.green);
lblNotConnected.setText("Connected");
JOptionPane.showMessageDialog(null, "Connected");
}
else{
status.setForeground(Color.red);
lblNotConnected.setText("Not Connected");
}
}
});
frame.getContentPane().add(comboBox);
JLabel lblSelectDatabase = new JLabel("Select Database");
lblSelectDatabase.setFont(new Font("Microsoft Sans Serif", Font.BOLD, 14));
lblSelectDatabase.setBounds(91, 79, 129, 30);
frame.getContentPane().add(lblSelectDatabase);
lblNotConnected = new JLabel("Not Connected");
lblNotConnected.setFont(new Font("Elephant", Font.PLAIN, 12));
lblNotConnected.setBounds(280, 230, 110, 20);
frame.getContentPane().add(lblNotConnected);
}
}
Code executed in a listener executes on the Event Dispatch Thread(EDT). When you use Thread.sleep() this causes the EDT to sleep which means the GUI can't repaint itself. Read the section from the Swing tutorial on Concurrency for more information.
Any potentially long running task should NOT execute on the EDT for the above reason. Instead you should use a separate Thread. Check out the SwingWorker from the above tutorial. You can use Thread.sleep() there and publish values to display as you wish.
However, why would you want the user to wait 5 seconds? I know I would get frustrated.
Maybe instead you should be using a ProgressMonitor to let the user know that you might be executing a long running task. Read the section from the Swing tutorial on How to Use Progress Bars for more information and working examples.

How do I call a method from an action listener without the JFrame freezing and waiting for the action listener to complete?

How do I set the text of a JTextArea while its JFrame is running, and refresh the JFrame to show the change, from another class?
I have a JFrame with a JTextArea which acts as a log, and the string it prints i update periodically with new activity from another class. My JFrame class (EnablePage) looks like this:
package bot;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class EnablePage extends JFrame {
public static String enablePane;
private static JPanel contentPane;
public static JTextArea txtrHello = new JTextArea();
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
EnablePage frame = new EnablePage();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public EnablePage() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 594, 474);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setToolTipText("");
scrollPane.setBounds(6, 89, 582, 357);
contentPane.add(scrollPane);
txtrHello.setEditable(false);
txtrHello.setText(enablePane);
txtrHello.setWrapStyleWord(true);
txtrHello.setLineWrap(true);
scrollPane.setViewportView(txtrHello);
JButton btnNewButton = new JButton("Enable");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
navigator.navigator();
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
btnNewButton.setBounds(59, 29, 117, 29);
contentPane.add(btnNewButton);
}
public static void update(String x) {
txtrHello.setText(enablePane+"\n"+x);
}
}
And from my navigator class I've been trying to use this line of code to update the JtextArea, while it manipulates a website. This code I didn't include, but replaced here with "Thread.sleep(100000);" to illustrate the problem:
package bot;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
public class navigator {
public static DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy(HH:mm:ss)");
public static void navigator() throws Exception {
Date date1 = new Date();
Thread.sleep(100000);
EnablePage.update("Bot enabled: "+dateFormat.format(date1));
}
}
However this is not updating the JFrame with the new text, because the EnablePage class is stuck waiting for the navigator() method to complete. What ends up happening is the Enable button stays blue because the actionlistener method is never broken from, because the nagivator() method never finished. What can I do to still call navigator() from the enable button but not have the EnablePage class freeze on this line?
Here's a simple example. A clock JTextField is updated from a Thread.
As you can see, there are no update, validate, or invalidate method calls.
Edited to add: The calls to the SwingUtilities invokeLater method are important, to ensure that the Swing components are created and updated on the Event Dispatch thread (EDT).
I also modified the Clock example to stop the Timer thread cleanly before disposing of the JFrame.
package com.ggl.testing;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class Clock implements Runnable {
private JFrame frame;
private JTextField clockDisplay;
private Timer timer;
#Override
public void run() {
frame = new JFrame("Clock");
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent event) {
exitProcedure();
}
});
JPanel panel = new JPanel();
clockDisplay = new JTextField(12);
clockDisplay.setEditable(false);
clockDisplay.setHorizontalAlignment(JTextField.CENTER);
panel.add(clockDisplay);
frame.add(panel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
timer = new Timer(this);
new Thread(timer).start();
}
public void exitProcedure() {
timer.setRunning(false);
frame.dispose();
System.exit(0);
}
public void setText(String text) {
clockDisplay.setText(text);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Clock());
}
public class Timer implements Runnable {
private volatile boolean running;
private Clock clock;
private SimpleDateFormat timeFormat;
public Timer(Clock clock) {
this.clock = clock;
this.running = true;
this.timeFormat = new SimpleDateFormat("h:mm:ss a");
}
#Override
public void run() {
while (running) {
displayTime();
sleep();
}
}
public void displayTime() {
Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
final String s = timeFormat.format(date);
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
clock.setText(s);
}
});
}
public void sleep() {
try {
Thread.sleep(200L);
} catch (InterruptedException e) {
}
}
public synchronized void setRunning(boolean running) {
this.running = running;
}
}
}
JTextArea#append will allow you to append text to the JTextArea, both setText and append are bound methods, this means that they will trigger an update when they are called so you shouldn't need to do anything more. If it's not updating then it sounds like you have a reference issue.
You should consider providing a fully runnable example which demonstrates your problem. This will result in less confusion and better responses
You should avoid the use of static, especially when associated with UI components, as this really begins to give you trouble with what you are referencing and what's on the screen. static is NOT a cross communication mechanism for objects and shouldn't be used as such.
If you can, you should define some kind of interface which describes the actions which be executed on your log frame (ie addLog(String)), have your log frame implement this interface and then pass a reference of it to those classes that need it.
Alternatively, you could use a singleton pattern to allow your log window to be accessed from any where in your application, personally, I'd be tempted to devise a queue of some kind, where other classes pushed log events onto this (singleton) queue and you had your frame either poll it or use some kind of blocking queue mechanism to monitor for changes to the queue. This would require you to have a separate Thread (or SwingWorker) which monitored the queue in the background so you don't block the Event Dispatching Thread.
Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify
Updated
Your runnable example works for me, more or less. Your reliance on static is worrying and Thread.sleep(100000); will block the Event Dispatching Thread, making your program look like it's hung (cause it has). The following is reworked version of your example, without null layouts, without static and using a Swing Timer instead of Thread.sleep. The great thing about this, is once you press the "Enable" button, the timer will update the text area every second...
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.Timer;
public class EnablePage extends JFrame {
private JTextArea txtrHello = new JTextArea(10, 20);
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
EnablePage frame = new EnablePage();
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public EnablePage() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JScrollPane scrollPane = new JScrollPane(txtrHello);
scrollPane.setToolTipText("");
add(scrollPane);
txtrHello.setEditable(false);
txtrHello.setWrapStyleWord(true);
txtrHello.setLineWrap(true);
JButton btnNewButton = new JButton("Enable");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Navigator.navigator(EnablePage.this);
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
add(btnNewButton, BorderLayout.NORTH);
}
public void update(String x) {
System.out.println("Update " + x + "\n");
txtrHello.append(x);
}
public static class Navigator {
public static DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy(HH:mm:ss)");
public static void navigator(EnablePage page) throws Exception {
Timer timer = new Timer(1000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
Date date1 = new Date();
page.update("Bot enabled: " + dateFormat.format(date1));
}
});
timer.start();
}
}
}

Refreshing JPanel after loading next Image into it

I have created ImagePanel which is able to display images from the specified directory -> it sleeps 1 second and loads next image from the java project's directory.
It actually loads next image but it is not displayed(it does not refresh the panel), when it is done with all the files from the directory it shows only the last image from the directory. I would like to make it refresh after loading every image.
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;
public class Okno extends JFrame {
JPanel jp;
ImagePanel ImagePanel;
JButton buttonExit;
JButton buttonWyjscie;
public Okno() {
}
public void createGUI() {
setSize(400, 400);
setLayout(new GridLayout());
buttonExit = new JButton("Exit");
buttonWyjscie = new JButton("Wyjscie");
// Sluchacz sluchacz = new Sluchacz();
// buttonExit.addActionListener(sluchacz);
buttonExit.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
jp = new JPanel();
jp.setBorder(new LineBorder(new Color(40, 120, 80), 4));
ImagePanel = new ImagePanel();
ImagePanel.setBorder(new LineBorder(Color.blue, 4));
jp.add(buttonExit);
add(jp);
add(ImagePanel);
setVisible(true);
slajd();
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void slajd() {
try {
File f = new File(".");
File[] tablicaPlikow = f.listFiles();
for (File el : tablicaPlikow) {
String rozszerzenie = el.getName().substring(
el.getName().length() - 3);
if (rozszerzenie.equals("jpg") || rozszerzenie.equals("peg")) {
System.out.println(rozszerzenie);
ImagePanel.setImage(el);
}
repaint();
}
setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Okno().createGUI();
}
});
}
}
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
public class ImagePanel extends JPanel {
private BufferedImage image;
public ImagePanel() {
}
public ImagePanel(String sciezka) {
setImage(new File(sciezka));
}
public void setImage(File plik) {
try {
image = ImageIO.read(plik);
System.out.println("tutaj");
repaint();
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
public void paint(Graphics g) {
if (image != null) {
Image b = image.getScaledInstance(getWidth(), getHeight(),
Image.SCALE_FAST);
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
}
}
}
Sleeping in the EDT prevents swing from doing the painting, so you see only the last image. Instead of sleeping in the event dispatch thread, use a swing Timer to do repeated tasks:
private final ActionListener timerTask = new ActionListener() {
#Override
public void actionPerformed(final ActionEvent e) {
// Whatever you need to to that
showNextImage();
}
};
Timer timer = new Timer(1000, timerTask);
timer.start();
If loading the images is taking long time, consider using a background task for preloading the next one in memory, without blocking the EDT.
The short answer could be: in method slajd, after call to repaint();, add
Thread.sleep(1000);
However, this is completely contrary to the event-based nature of Swing, and in this particular case doesn't even work because, for efficiency reasons, Swing does not execute repaint() calls immediately. It "collects" and executes them only once after all event processing has concluded. If you include an sleep period (or any other long-running operation) in an event handler (directly or indirectly), repainting will be delayed and the application be extremely unresponsive to the point, as in this case, of not really be working.
What you need to do is in createGUI instantiate a Swing Timer (javax.swing.Timer; do not confuse it with java.util.Timer, or Timer classes in a few other packages) that fires every 1 second instead of calling slajd(). First firing should be immediate, or you could include code to display the first file. The associated listener, which would replace slajd() should keep track of the next file to display. You will most probably want to make this listener a full-fledged class with fields to support this tracking, a pointer to the ImagePanel where to display files, etc.
For more information, read Java's Tutorial on How to Use Swing Timers

Categories

Resources