show multiple MessageDialog at a time - java

I am trying to make a program that displays 3 MessageDialog boxes at a time. I thought that if you put JOPtionPane.showMessageDialog in an actionListner class for a swing timer, it would show a new MessageDialog box every second.
So here is the code that I came up with:
package pracatice;
import java.awt.event.*;
import javax.swing.*;
public class practice extends JFrame
{
public static int num = 0;
public static TimerClass tc = new TimerClass();
public static Timer timer = new Timer(1000, tc);
public JPanel panel = new JPanel();
public JButton btn = new JButton("press");
public practice()
{
setSize(100,100);
setTitle("Test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPanel();
setVisible(true);
}
public void setPanel()
{
btn.addActionListener(new listener());
panel.add(btn);
add(panel);
}
public class listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
num = 0;
System.out.println("starting timer");
timer.start();
}
}
public static class TimerClass implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println("Adding 1 to num");
num++;
JOptionPane.showMessageDialog(null,"Test");
if(num == 3)
{
System.out.println("stopping the timer");
timer.stop();
}
}
}
public static void main(String[] args)
{
practice p = new practice();
System.out.println("created an instance of practice");
}
}
It works, but not the way I want it to. Instead of showing a new box every second it shows a new one 1 second after you press ok on the previous one.
So when I press "press" it waits 1 second and spawns the box. When I press "ok", it waits 1 second and spawns another one and so on. Any idea how to make the 3 boxes spawn 1 after another?

When using the showX methods of JOptionPane you are creating modal (blocking and one at a time) dialogues as stated by the documentation.
You can Use the JOptionPane directly by creating it manually instead of using the showX methods.
create a new one manually and set it to not be modal:
optionPane = new JOptionPane(...);
dialog = optionPane.createDialog(null, "the title");
dialog.setModal(false);
dialog.show();

The methods to create the dialogs (JOptionPane.show...) do not return until the user has somehow closed the dialogs. Given Swing is single threaded, no other Swing process can happen until this occurs. If you wish to have three dialogs open at once, use a non-modal dialog.

Related

is it safe to create a new thread inside actionPerformed in swing?

I created swing application that do some operations in performedAction methode, but when i create a thread that shows the progressBar, the progressBar will not visible during the performedAction methode, but at the end the progressBar will be visible with a value of 100% directly
main class:
import javax.swing.*;
import javax.swing.plaf.ButtonUI;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class main {
public static void main(String[] args) {
JFrame win = new JFrame("Test");
FlowLayout layout = new FlowLayout();
Button b1 = new Button("Click ");
win.add(b1);
b1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
window win = new window();
win.start();
try
{
Thread.sleep(2000);
}
catch(InterruptedException e5){}
}
});
win.setLayout(layout);
win.setSize(500, 300);
win.setLocationRelativeTo(null);
win.setVisible(true);
win.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
window class:
import javax.swing.*;
import java.awt.*;
public class window extends Thread{
public window(){
}
#Override
public void run() {
JFrame fen = new JFrame("New window");
FlowLayout layout = new FlowLayout();
fen.setLayout(layout);
Button b2 = new Button();
fen.setVisible(true);
fen.setSize(100, 100);
fen.setLocationRelativeTo(null);
fen.add(b2);
try
{
for(int i = 0; i <= 100; i++)
{
b2.setLabel("Button " + i);
Thread.sleep(10);
}
}
catch(InterruptedException e2){
}
}
}
Yes, it is safe to create a new Thread inside of an actionPerformed handler.
But, no thread other than the Event Dispatching Thread (EDT) must interact with the Swing components.
To create animations, or delays inside of Swing, you must use a javax.swing.Timer. Executing a Thread.sleep() on the EDT is never allowed, and won't do want you hope it will.
To return from a spawned thread to the EDT, you must use SwingUtilities.invokeLater() or SwingUtilities.invokeAndWait().
Using a SwingWorker is the preferred way to execute background tasks in Swing. It handles communication and publication of partial and final results from the background task to the EDT for displaying in the GUI components.
The following is a translation of your code to a working example, using a SwingWorker. Instead of AWT Button's, the Swing JButton is used. The program is created using invokeAndWait to ensure the main window construction occurs on the EDT. A lambda function is used, but you can replace this with new Runnable() { } inner class, if desired. The "Button 0" through "Button 100" progress reports are published from the SwingWorker background task, for processing in the EDT. Multiple results can be generated at by the background thread before the EDT has a chance to process them; here, we take only the last result to display in the button.
public class Main {
public static void main(String[] args) throws Exception {
SwingUtilities.invokeAndWait(() -> {
JFrame win = new JFrame("Test");
win.setLayout(new FlowLayout());
JButton b1 = new JButton("Click");
b1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
DoWork work = new DoWork();
work.execute();
}
});
win.add(b1);
win.setSize(500, 300);
win.setLocationRelativeTo(null);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setVisible(true);
});
}
}
class DoWork extends SwingWorker<Void, String> {
JFrame fen;
JButton b2;
DoWork() {
fen = new JFrame("New window");
fen.setLayout(new FlowLayout());
b2 = new JButton();
fen.add(b2);
fen.setSize(100, 100);
fen.setLocationRelativeTo(null);
fen.setVisible(true);
fen.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
#Override
protected Void doInBackground() throws Exception {
for (int i = 0; i <= 100; i++) {
publish("Button " + i);
Thread.sleep(10);
}
return null;
}
#Override
protected void process(List<String> data) {
String last = data.get(data.size() - 1);
b2.setText(last);
}
#Override
protected void done() {
fen.dispose();
}
}
There are still a number of improvements that can be made to this code. A JLabel instead of a JButton for displaying the results, a JDialog for the progress window, or perhaps better a ProgressMonitor. These are left as an exercise to the student.

How to close multiple JFrame and JDialog windows?

I'm working on a program which has multiple JFrame and JDialog windows.
I have a JFrame which contains a button, when I click on this button a JDialog window opens up. In this JDialog windows there is another button, which when is clicked it opens up a second JDialog window. In the second JDialog window I have a last button.
What I want to do is to close both JDialog windows and JFrame window when this last button is clicked.
This is how the opening order is:
JFrame Frame1;
JButton Button1;
JDialog Dialog1;
JButton Button2;
JDialog Dialog2;
JButton Button3;
Button1ActionPerformed(ActionEvent e){
new Dialog(Frame1Frame);
}
Button2ActionPerformed(ActionEvent e){
new Dialog2(Dialog1Frame)
}
Button3ActionPerformed(ActionEvent e){
//Here I wnat to add the code that closes JDialog2 JDialog1 and JFrame1 windows.
}
I have tried super.dispose(); but it doesn't work. Any ideas?
As shown here using Action, your actionPerformed() implementation can dispatch the WINDOW_CLOSING event to the desired Window instances.
#Override
public void actionPerformed(ActionEvent e) {
d1.dispatchEvent(new WindowEvent(d1, WindowEvent.WINDOW_CLOSING));
d2.dispatchEvent(new WindowEvent(d2, WindowEvent.WINDOW_CLOSING));
f1.dispatchEvent(new WindowEvent(f1, WindowEvent.WINDOW_CLOSING));
}
There may be better ways of doing this, but here is one general approach that might help.
In your code you create the windows but you do not store the reference to the windows you created into a variable. For example, you have:
JDialog Dialog1;
Then later, when you create the instance of Dialog1, you have this code:
Button1ActionPerformed(ActionEvent e){
new Dialog(Frame1Frame);
}
This means you have created the Dialog, but you have not retained a reference to the Dialog for later manipulation by your code. If you assign this value here, you should be able to manipulate it later.
If you change your implementation to:
Button1ActionPerformed(ActionEvent e){
Dialog1 = new Dialog(Frame1Frame);
}
Then later in your code you will have a reference to the Dialog in order to manipulate it,
Button3ActionPerformed(ActionEvent e){
Dialog1.dispose();
// you can manipulate the variables of the class from here and close other windows etc.
}
If you have the objects reference, you can do:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
public class Main
{
private static JFrame frame;
private static JButton buttonFrame;
private static JDialog dialog1;
private static JButton buttonDialog1;
private static JDialog dialog2;
private static JButton buttonDialog2;
public static void main(String[] args) {
/* frame */
frame = new JFrame("Main Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
buttonFrame = new JButton("open dialog 1");
buttonFrame.addActionListener(new ActionListener() {
#Override public void actionPerformed(ActionEvent e) {
dialog1.setVisible(true);
}
});
frame.add(buttonFrame);
/* dialog 1 */
dialog1 = new JDialog(frame, "Dialog 1");
dialog1.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog1.setSize(300, 300);
dialog1.setLocationRelativeTo(null);
buttonDialog1 = new JButton("open dialog 2");
buttonDialog1.addActionListener(new ActionListener() {
#Override public void actionPerformed(ActionEvent e) {
dialog2.setVisible(true);
}
});
dialog1.add(buttonDialog1);
/* dialog 2 */
dialog2 = new JDialog(dialog1, "Dialog 2");
dialog2.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog2.setSize(200, 200);
dialog2.setLocationRelativeTo(null);
buttonDialog2 = new JButton("close all");
buttonDialog2.addActionListener(new ActionListener() {
#Override public void actionPerformed(ActionEvent e) {
dialog2.dispose();
dialog1.dispose();
frame.dispose();
}
});
dialog2.add(buttonDialog2);
/* show frame */
frame.setVisible(true);
}
}
Otherwise you can use System.exit(0);:
buttonDialog2.addActionListener(new ActionListener() {
#Override public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
Closing multiple JFrame by another JFrame-
It is possible to close multiple windows, even non-static Java swing windows. Type below small code, step by step.
The below code is mainly for closing the First frame through the second open frame which has been opened by the same First frame.
*Make a **second class global variable** in the first-class & a **button global variable in first-class*
Class2 NiceApp1 = new Class2();
JToggleButton Nice=new JToggleButton("Nice One");
// *After the Class1 above code, you will need a close method. I use a jar of NiceApplication1 with class New Close you can use the same jar or very below close method and can call the close method. You will need to type the method in Class1, just type your **close method in the method**.*
public void method (){
NewClose NiceO = new NewClose();
NiceO.Close(this);
//or
Close();
}
*// Now make a **Button Global variable in Class2**.*
javax.swing.JToggleButton Zero = new javax.swing.JToggleButton();
*// Type in Class2 the below **method**.*
public void methodclass2(JToggleButton Nice){
Zero = Nice;
}
*//Now you will need to type the below code, type the below code in the **Class1 button action** by which you will open the Class2.*
Nice.addActionListener((ActionEvent e) -> {
method();
});
NiceApp1.methodclass2(Nice);
NiceApp1.setVisible(true);
*//Now just type a **single line code** in the second class, type anywhere from which you want to close Class1.*
Zero.doClick();
*//Now the **close method** if you don't have a close method.*
public void Close() {
WindowEvent winclosing = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winclosing);
}
**You have done now if you did the above process.
If you only want to close which opens secondly so by the only close method change the this into your frame variables.
You can also visit NiceApplication1.BlogSpot.Com.
You can use the above code. From Class2 after the button's click, you will see your previous Class1 window will close.**

Running a void method from a different class using actionlistener for my menu combobox.

I am creating a little combobox menu for my ap comp sci project. This is my first time working with GUI so I am still familiarizing myself with it. I have a menu class and a few other classes that run mostly void methods to do simple things like calculate GPA, etc. My menu is working fine (at least I think so). However, when I select my first item in the menu (I only have one actionlistener at the moment but I will add more after) the actionlistener runs the void method (run()) and it opens the window. But after that nothing happens. I have user input in that method to type in grade percentages, etc, and it works when i run the method directly from the class. But I can't type or seem to have any input when the actionlistener runs it. Thank you for your time and help!
package First_Semester_Project;
import java.awt.Color;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Menu extends GradeCalculator
{
String [] info = {"Grade Calculator", "Spanish Conjugator", "Period Table of Elements", "Sentence Compiler"};
JLabel l= new JLabel("Welcome to the Resource Library!");
JComboBox c = new JComboBox(info);
JButton b = new JButton ("Select Resource");
public Menu ()
{
frame();
}
public void frame ()
{
JFrame f = new JFrame ("Resource Library");
l.setForeground(Color.white);
l.setFont(new Font("Arial", Font.BOLD, 16));
f.setSize(600,90);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel();
p.setBackground(Color.gray);
p.add(c);
p.add(b);
p.add(l);
f.add(p);
b.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e ){
String s = c.getSelectedItem().toString();
//l.setText(s);
if (s.equals("Grade Calculator"))
{
run();
}
else if (s.equals("travel time"))
{
l.setText(s);
}
}
});
}
Im going to assume that the run method can be called from the class as its superclass GradeCalculator has the run() method.
A common thing that i like to do is create my own actionlistener class, so that i can easily follow my code and see who has access to what, this would be my solution.
b.addActionListener(new myCustomActionListener(this)); //replaces b.actionlistener
now i would add myCustomActionListener either to the end of the same file, or in a different class file alltogether - depends on your abstraction level really.
here is the class for reference:
public class myCustomActionListener implements ActionListener
{
Menu menu = null;
public myCustomActionListener(Menu menu)
{
this.menu = menu;
}
#Override
public void actionPerformed(ActionEvent arg0) {
String s = menu.c.getSelectedItem().toString();
//l.setText(s);
if (s.equals("Grade Calculator"))
{
menu.run();
}
else if (s.equals("travel time"))
{
menu.l.setText(s);
}
}
}

How do I create buttons that reset and pause a timer?

How do I make the two buttons that are displayed reset / pause the timer? The timer works but I want to change the code for the buttons so that they will change the timer instead of outputting to the console. Thank you.
CODE:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class createWindow extends JFrame implements ActionListener
{
public static void main(String[] args)
{
new createWindow();
}//end main
createWindow()
{
super("Frame");
setSize(400,70);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
show();
final JLabel time = new JLabel("Timer");
JButton reset = new JButton("Reset timer");
JButton pause = new JButton("Pause timer");
reset.setActionCommand("resetClicked");
pause.setActionCommand("pauseClicked");
reset.addActionListener(this);
pause.addActionListener(this);
add(pause);
add(time);
add(reset);
long start = System.currentTimeMillis();
while (true)
{
long timer = System.currentTimeMillis() - start;
final int seconds = (int) (timer / 1000);
String display = Integer.toString(seconds);
time.setText(display);
}//end while loop
}//end constructor
#Override
public void actionPerformed(ActionEvent e)
{
String buttonClicked = e.getActionCommand();
if(buttonClicked.equals("resetClicked"))
{
System.out.println("The reset button was clicked"); //Change to reset timer
}
else if(buttonClicked.equals("pauseClicked"))
{
System.out.println("The pause button was clicked"); //Change to pause timer
}
}//end listener
}
Don't use an infinite while loop. This blocks the EDT. Instead use a Swing Timer. This will give you control to start and stop the Timer.
Stopwatch Example
Side Notes:
Don't use JFrame.show as that method is deprecated. Use JFrame.setVisible instead. Also make this call when all components have been added to the frame.
The functionality for the JButtons is sufficiently different to warrant using separate ActionListener instances for each button.
The preferred approach is to use a JFrame instance directly rather then extending it.
Class names in Java begin with uppercase so createWindow would become CreateWindow.

JOptionPane : Display button "Ok" after a certain amount of time

I would like to display the button "OK" of this JOptionPane only after a certain amount of time (let's say for example 5 sec). (My aim is actually to let finish some thread work behind this other thread)
JOptionPane jop2 = new JOptionPane();
jop2.showMessageDialog(null, "Please wait 5s", "WAIT", JOptionPane.INFORMATION_MESSAGE);
I don't know at all how to do that, could you provide me some code working which will answer to this problem?
Thank you very much in advance!
There is no specific way to do this using JOptionPane. You will have to create a custom dialog and reveal the OK button after a fixed time. You could use one pass of a Swing timer.
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button.setVisible(true);
}
};
Timer timer = new Timer(0, taskPerformer);
timer.setInitialDelay(5000);
timer.setRepeats(false);
timer.start();
It sounds like what you're looking for is a combination of the SwingWorker and the ProgressMonitor. The SwingWorker will preform your long running task (the 5 second one), and inform the user of how it's progressing using the ProgressMonitor. An example showing you how to get the two working together can be found here:
getting the cancel event of Java ProgressMonitor
Of course, if you're convinced you want to take the approach of displaying the continue button once the work is done, here's an example that should get you started in the right direction. You'll use a SwingWorker to alert your Dialog that the long running background task has completed.
import java.awt.*;
import java.awt.Dialog.ModalityType;
import java.awt.event.*;
import javax.swing.*;
public class TempProject extends Box{
public TempProject(){
super(BoxLayout.Y_AXIS);
//Contains the content of the Alert Dialog
Box info = Box.createVerticalBox();
info.add(new Label("Please wait 5 seconds"));
final JButton continueButton = new JButton("Continue");
info.add(continueButton);
//The alert to wait 5 seconds
final JDialog d = new JDialog();
d.setTitle("WAIT");
d.setModalityType(ModalityType.APPLICATION_MODAL);
d.setContentPane(info);
d.pack();
//The action of the "Continue Button"
continueButton.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent arg0) {
d.dispose();
}
});
continueButton.setVisible(false);
//Thread That Does Work
final SwingWorker sw = new SwingWorker<Integer, Integer>()
{
protected Integer doInBackground() throws Exception {
//Do long running thread work here
int i = 0;
while (i++ < 100) {
System.out.println(i);
Thread.sleep(100);
}
return null;
}
#Override
protected void done(){
// What to do when the long runnng thread is done
continueButton.setVisible(true);
}
};
//Button to start the long running task
JButton button = new JButton("Click Me");
button.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent arg0) {
sw.execute();
d.setVisible(true);
}});
add(button);
}
public static void main(String args[])
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setContentPane(new TempProject());
frame.setPreferredSize(new Dimension(500, 400));
frame.pack();
frame.setVisible(true);
}
});
}
}
you could use something like this to stop the code for 5 seconds
try {
Thread.sleep(5000); // do nothing for 5000 miliseconds (5 seconds)
} catch (InterruptedException e) {
e.printStackTrace();
}

Categories

Resources