Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Frame won't display components & becomes irresponsive when called from another Frame's event handler  RSS feed

 
Arlene Batada
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm building up a desktop application which encrypts & decrypts files. The JFrames have been made directly using netbeans.

The flow is as such:

Main frame -> file chooser -> display selected files frame -> password taker frame -> progress frame -> end frame
................................................................................................................................................| /\
...............................................................................................................................................\/ |
..................................................................................................................................file enc. & dec. Java class

Each frame's job is written in its constructor which in turn calls necessary functions and creates necessary objects. New subsequent frame is created after an event occurs in the previous frame.

"progress frame" takes each file, sends it to "file enc. & dec. Java class" along with reference to its JProgress Bar & JText Area. "file enc. & dec. Java class" encrypts/decrypts the given file, sets the value of progress bar accordingly & writes the prescribed text in the given text area.

The problem is that when "progress frame" is created/invoked from inside "password taker frame's" event handler, it doesn't display its components, displays white area instead and doesn't even respond to close operation. Meanwhile, "file enc. & dec. Java class" invoked from "progress frame's" constructor does its job perfectly. After it finishes its job, the control returns back to "progress frame's" constructor and finally it reaches back to "password taker frame's" event handler which also finishes executing. It is now that "progress frame" displays everything with progress bar reached to 100% along with all the necessary text written by "file enc. & dec. Java class" displayed perfectly fine.

I also tried replacing "progress frame" with some other frame and also tried calling it from previous frame's constructor instead of event handler, but got the same result. I even tried changing the previous frame, but still the same result.

But when "progress frame" is called independently from static main() function, it works fine.


What could be the reason for this unusual behavior? Any solution to handle this situation?
 
Les Morgan
Rancher
Posts: 752
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In about 90+ percent of these situations it has been the programmer is blocking the event dispatch thread by burning resources by looping, and thereby, not allowing proper processing. This is indicated by your works on a stand alone, but not back in the app, type of comment you gave earlier.

In any case we're going to be guessing without example code that illustrates your problem.
 
Rob Camick
Ranch Hand
Posts: 2754
12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with the suggest by Les.

You need to have your encryption/decryption logic execute in separate Thread so you don't block the EDT.

Read the section from the Swing tutorial on Concurrency for more information. Maybe you should be using a SwingWorker.

Also, you application should only have a single main JFrame. Other child windows should probably be a JDialog with the frame as the parent.
 
Arlene Batada
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much guys. I'm a C programmer and all this was pretty new to me. This is something new I've learnt.

Thank you so much Rob Camick for recommending SwingWorker. The application works fine now.
 
Campbell Ritchie
Sheriff
Posts: 53771
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
Why are you using two frames?
 
Arlene Batada
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2 frames? It's worse. There are 5 at present!!

I should've used 5 panels instead.
 
Campbell Ritchie
Sheriff
Posts: 53771
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use any number of dialogues instead of multiple frames. That might be nearer to what you want.
 
Arlene Batada
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure Campbell Ritchie, I'll definitely keep that in mind from the next time.

But for now, the application has been made.

Here's the source code & the executable in case if anyone would like to see it:

https://github.com/arlenebatada/File-Encryption-and-Decryption-Application-in-JAVA

It might not be so grand. It is something small made out of my hobby.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!