Calling One Jframe from Another Using Timer Without Any Buttons

Method called from first JFrame does nothing in second

As stated in the comments above by @matt

Every time you click on manual button, you're creating a new MainGUI().

You need to create a single instance, either in your constructor or in the ActionListener and ask if you already have an instance of it (i.e. a Singleton) and use it.

If you decide to use the first one, declare gui as a global variable:

MainGUI gui = new MainGUI();

And on your ActionListener have it changed as:

@Override
public void actionPerformed(ActionEvent e) {
System.out.println(currentThread());
gui.setRfidEnabled();
//frame.dispose();
}

Then you have a single instance of it.

Also as stated by @Sergiy you don't really need all those threads

Here are some examples on how to use ActionListeners:

  • I'm trying to make a button to count characters in a text field
  • AppletViewer bugged and trying to involve a timer
  • Calculator returns 0.0 to all questions asked
  • Java - My action event doesn't work
  • Drawing shapes on a JForm java
  • Animated Sprites with Java Swing This one includes a Timer (Another thread that handles the animation but doesn't block the EDT)

As you can see in all the above examples, none of them required another Thread to handle the actions, the one that uses a thread is only for performing the animation and not to react to user clicks.

Recommended tutorial: How to use Actions

Timer or other idea required to allow code to continue execution after calling method and JOptionPane

But it hangs on the JOptionPane. I need a way to make it so that the
program either keeps going underneath the JOptionPane or to close the
pane after about 10 seconds. I am not sure how to work either into my
code, currently

there are two ways

  • (better easier, comfortable) create JDialog(JFrame parent, boolean true), with default close operation HIDE_ON_CLOSE, only one JDialog as local variable, reuse this Object by setVisible(false/true)

  • looping inside arrays of all JOptionPanes (all exists there untill current JVM is alive) by @kleopatra

Java Swing: Restrict ActionListener Calls within a Certain Window of Time

If someone could tell me how to use the timer, that would be great.

See: Update a Label with a Swing Timer for a basic example.

I want the timer to start once the first click is made.

  1. In the constructor of your class you create the Timer
  2. In the ActionListener of your button you use the isRunning() method of the Timer. If it is not running, then you start the Timer
  3. When the Timer fires you a) stop the timer and b) make but button invisible

How to call a Swing Timer from Inside a JButton

Get rid of Thread.sleep(10000);, it's cause the Event Dispatching Thread to sleep for 10 seconds, which is stopping it from process the the Event Queue, which where the Timer will post it's "action" events to run

Take a look at The Event Dispatch Thread for more details

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Test {

public static void main(String[] args) throws IOException {
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

public TestPane() {
JButton btn = new JButton("Start");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
Timer timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("...");
}
});
timer.start();
}
});
add(btn);
}

}

}

but how would I stop it after a certain time now?

That's a more difficult answer. First, you would need to make it an instance field so you can reference it.

Second, you could either use a second Timer, set to the specified timeout (and set not to repeat) and when it fires, stop the first Timer or, use something like Instant to calculate the difference between when the timer was started and now, for example and example (at the lest the second half)

Show a JWindow for a while

Here is a short example using a Swing timer.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class TestGUI {
public TestGUI() {
final JFrame frame = new JFrame("Test");
JButton press = new JButton("Press Me");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(press);
press.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
final JDialog dialog = new JDialog();
dialog.add(new JLabel("Here for 2 seconds"));
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
Timer timer = new Timer(2000, new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
dialog.dispose();
}
});
timer.setRepeats(false);
timer.start();
}
});
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
new TestGUI();
}
});
}
}


Related Topics



Leave a reply



Submit