Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (775)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (856)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  JDialog  (Read 5049 times)
0 Members and 1 Guest are viewing this topic.
Offline Marksman Ken

Senior Newbie





« Posted 2010-05-05 12:58:34 »

Hi,

I think my problem is caused by my lack of knowledge of extending JDialog, heres my situation:

I have a JDialog that is crated and setVisible(true) when you click a button. The dialog only has a JTextArea as its only component. I have created a public method called log(String text) which appends whatever text I enter to the text area within the dialog.

My problem is that when I use this method nothing happens, no text added to the area, no exceptions, I checked the strings and there fine. Is this due to the event dispatching thread or have I forgot something?

Thanks,

Ken
Offline Riven
Administrator

« JGO Overlord »


Medals: 1356
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2010-05-05 14:08:16 »

modal dialogs spawn their own event queue.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline Marksman Ken

Senior Newbie





« Reply #2 - Posted 2010-05-05 14:16:06 »

Oh really, well yes its modal so that mean it has its own event thread?
So technically I've got some multithreading going on here then. The main thread is using the public method log so does that mean I need to implement some form of protection and could this have something to do with my problem?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
Administrator

« JGO Overlord »


Medals: 1356
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2010-05-05 14:29:21 »

It's actually slightly different: the two queues are on the same thread, like a stack of queues.


Using SwingUtilities.invokeLater(Runnable) should put your 'event' in the right queue.

Just make sure you don't call code *after* dialog.setVisible(true) that the dialog depends on. setVisible is blocking (as that's where the new event queue is spawned, on that very same thread... if the current thread actually is the event thread. if not, it will still block, but hand over the work to the event thread, still making a new event queue)

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline Marksman Ken

Senior Newbie





« Reply #4 - Posted 2010-05-05 14:43:43 »

Thanks, I do find it hard to get my head around these threading concepts etc. but I roughly understand.

Well nothing it depends on happens after setVisible() eg create the text area, setup the gui etc. The only thing that happens is calls to the one method I have mentioned before.

From what you are saying does it mean that I should use the log method and append text to text area after I create the dialog but before I setVisible?
Offline Riven
Administrator

« JGO Overlord »


Medals: 1356
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2010-05-05 14:45:36 »

Share some code.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline Marksman Ken

Senior Newbie





« Reply #6 - Posted 2010-05-05 14:53:59 »

No problem!

Ill not give you the full classes, just the relavent bits.

The dialog:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public class CalcsDialog extends javax.swing.JDialog {

    /** Creates new form CalcsDialog */
    public CalcsDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public void log(String text){
        calcsArea.append(text);
    }
}


The class accessing the dialog (my main frame on the main thread):
When the button is pressed.
1  
2  
3  
4  
5  
6  
if(currentBeam != null && bendingDiagram != null){
            CalcsDialog calcs = new CalcsDialog(this, true);
            calcs.setVisible(true);
            calcs.log(currentBeam.getCalcs());
            calcs.log(bendingDiagram.getCalcs());
}


Hopefully that gives you a clue how it works, it must be something fundimentally wrong as theres not much to it...
Offline Riven
Administrator

« JGO Overlord »


Medals: 1356
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2010-05-05 15:04:54 »

Just make sure you don't call code *after* dialog.setVisible(true)


1  
2  
3  
4  
5  
6  
7  
if(currentBeam != null && bendingDiagram != null){
            CalcsDialog calcs = new CalcsDialog(this, true);
            calcs.setVisible(true);
 ---------------- this won't get executed until the Dialog closes
            calcs.log(currentBeam.getCalcs());
            calcs.log(bendingDiagram.getCalcs());
}


Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline Marksman Ken

Senior Newbie





« Reply #8 - Posted 2010-05-05 15:08:03 »

Stupid question (as I cant test it right now) but what would happen if I did the following then?

1  
2  
3  
4  
5  
6  
if(currentBeam != null && bendingDiagram != null){
            CalcsDialog calcs = new CalcsDialog(this, true);
            calcs.log(currentBeam.getCalcs());
            calcs.log(bendingDiagram.getCalcs());
            calcs.setVisible(true);
}
Offline jezek2
« Reply #9 - Posted 2010-05-06 05:05:59 »

It's actually slightly different: the two queues are on the same thread, like a stack of queues.

This is little incorrect. It actually creates a new thread for new queue but the old thread is paused in the meanwhile. So it behaves like there would be just one and there is no synchronization problems.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
Administrator

« JGO Overlord »


Medals: 1356
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2010-05-06 08:07:45 »

This is little incorrect. It actually creates a new thread for new queue but the old thread is paused in the meanwhile. So it behaves like there would be just one and there is no synchronization problems.

I vagely recall stacktraces with 2 (or 3) event 'pumps'

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline Marksman Ken

Senior Newbie





« Reply #11 - Posted 2010-05-06 10:15:00 »

What you say jezek2 makes perfect sense to me, it creates a new thread and pauses the main thread hence why my methods do nothing and why you cant do anything with any other GUI outside the dialog.

I changed my code to what I suggest and it worked anyway so Im happy  Smiley

Learn something new everyday...
Offline Riven
Administrator

« JGO Overlord »


Medals: 1356
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #12 - Posted 2010-05-06 11:11:27 »

What you say jezek2 makes perfect sense to me, it creates a new thread and pauses the main thread hence why my methods do nothing and why you cant do anything with any other GUI outside the dialog.

I changed my code to what I suggest and it worked anyway so Im happy  Smiley

Learn something new everyday...

I don't really care what makes sense to you. What I say is a fact.

Run the code below, and you'll see how there is only 1 thread involved in handling GUI.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
 final JFrame[] frame = new JFrame[1];
         final JButton button1 = new JButton("click to open a dialog");
         final JButton button2 = new JButton("click to terminate");
         
         button1.addActionListener(new ActionListener()
         {
            @Override
            public void actionPerformed(ActionEvent e)
            {
               System.out.println("frame.thread.hashcode=" + System.identityHashCode(Thread.currentThread()));

               //Thread.dumpStack();

               JDialog dialog = new JDialog(frame[0], true);
               dialog.getContentPane().add(button2);
               dialog.pack();
               dialog.setLocationRelativeTo(null);
               dialog.setVisible(true);
            }
         });
         button2.addActionListener(new ActionListener()
         {
            @Override
            public void actionPerformed(ActionEvent e)
            {
               System.out.println("dialog.thread.hashcode=" + System.identityHashCode(Thread.currentThread()));

              // Thread.dumpStack();

               System.exit(0);
            }
         });
         frame[0] = new JFrame();
         frame[0].setTitle("test");
         frame[0].getContentPane().setLayout(new BorderLayout());
         frame[0].getContentPane().add(button1, BorderLayout.CENTER);
         frame[0].setResizable(false);
         frame[0].pack();
         frame[0].setLocationRelativeTo(null);
         frame[0].setVisible(true);
         frame[0].setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


Output:
1  
2  
frame.thread.hashcode=17984263
dialog.thread.hashcode=17984263


You can uncomment the 'Thread.dumpStack()' lines to see the stacked event queues.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline Riven
Administrator

« JGO Overlord »


Medals: 1356
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #13 - Posted 2010-05-06 11:23:19 »

Thread stack dump after clicking 2nd button


java.lang.Exception: Stack trace
   at java.lang.Thread.dumpStack(Unknown Source)
   at automailer.AutoMailerMain$2.actionPerformed(AutoMailerMain.java:67) // my code
   ...
   at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) // event pump of dialog
   ...
   at java.security.AccessController.doPrivileged(Native Method) // creates new event pump
   ...
   at java.awt.Dialog.setVisible(Unknown Source) // call to setVisible
   at automailer.AutoMailerMain$1.actionPerformed(AutoMailerMain.java:57) // my code
   ...
   at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) // event pump of frame
   ...
   at java.awt.EventDispatchThread.run(Unknown Source) // creates the EDT

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline Riven
Administrator

« JGO Overlord »


Medals: 1356
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #14 - Posted 2010-05-06 11:29:54 »

The *only* time the EDT thread changes is when an uncaught Exception occurs on the EDT.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline Marksman Ken

Senior Newbie





« Reply #15 - Posted 2010-05-06 11:53:08 »

Calm down  Undecided
I didnt say what he said was correct, I dont know or I wouldnt be here!
All I said was what he says makes sense...

I will take alook at your code but I dont think it will mean much to me, I dont even understand what you mean by event pump.
EDIT: Ah yes I roughly see what you've done, ok I believe you its the same thread.
Offline jezek2
« Reply #16 - Posted 2010-05-06 12:02:16 »

The *only* time the EDT thread changes is when an uncaught Exception occurs on the EDT.

Sorry my mistake, I always thought it's the case, thanks for the example code. It's funny how you always learn something new about stuff you think you know well Smiley
Offline Riven
Administrator

« JGO Overlord »


Medals: 1356
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #17 - Posted 2010-05-06 12:24:47 »

I will take alook at your code but I dont think it will mean much to me, I dont even understand what you mean by event pump.
EDIT: Ah yes I roughly see what you've done, ok I believe you its the same thread.

an 'event pump' is the code that pops events off the 'event queue', so every queue has a pump. every modal dialog has it's own stacked queue+pump on top of the current queue+pump. all this is running on the 'event thread'.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline Marksman Ken

Senior Newbie





« Reply #18 - Posted 2010-05-06 12:31:53 »

Ah I see I think I get the rough theory behind it. Its hard for a new programmer to learn these things when they happen behind the scenes, its not something you implement yourself.

So you have a list of events to occur and the pump goes down the stack one by one performing them...
Offline Riven
Administrator

« JGO Overlord »


Medals: 1356
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #19 - Posted 2010-05-06 13:21:53 »

So you have a list of events to occur and the pump goes down the stack one by one performing them...

to be precise: it goes down a queue (first-in-first-out), not a stack (first-in-last-out)

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline Marksman Ken

Senior Newbie





« Reply #20 - Posted 2010-05-06 13:26:59 »

Yes thats logical, why do people always refer to stacks then? lol
Pages: [1]
  ignore  |  Print  
 
 

 
hadezbladez (40 views)
2018-11-16 13:46:03

hadezbladez (43 views)
2018-11-16 13:41:33

hadezbladez (24 views)
2018-11-16 13:35:35

hadezbladez (18 views)
2018-11-16 13:32:03

EgonOlsen (1894 views)
2018-06-10 19:43:48

EgonOlsen (1930 views)
2018-06-10 19:43:44

EgonOlsen (1285 views)
2018-06-10 19:43:20

DesertCoockie (1716 views)
2018-05-13 18:23:11

nelsongames (1403 views)
2018-04-24 18:15:36

nelsongames (2036 views)
2018-04-24 18:14:32
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!