File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes using threads or there's another way? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "using threads or there Watch "using threads or there New topic

using threads or there's another way?

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
in a swing standalone app i want a message to appear for a few seconds confirming user did deleted something
i'm using this code:

is there a better/smoother way of doing this?


java amateur
Henry Wong

Joined: Sep 28, 2004
Posts: 20535

Actually, once your swing application has been displayed, you are not allowed to access any of the components from anything that is not the event dispatching thread. With the exception of a few methods, like repaint(), it is not thread safe to do so.

Having a separate thread to handle the timing for changing the visibility is fine, but it is not allowed to directly manipulate the component. Take a look at the invokeLater() method of the SwingUtilities to dispatch that task back to the event dispatching thread.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
i'm not sure if i got it right:
my main:

my refactored code:
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
create a JFrame as private member data, create an inner class to display
the message


private class A{

public A()

public void paintComponent(Graphics g){}

and in your program create new instance of the private frame
and do:
.getContentPane().add(new A());

now you have a JFrame you can call

so say you want that frame to popup when they click a certain button
you would say.


you can also set the location of the frame by

Point p = MainFrame.getLocation(); // your main clients window.
myFrame.setLocation(new Point(p.getX()+MainFrame.getLength()/2,p.getY()+MainFrame.getWidth()/2));

that would set the top left corner of your popup display message to approx.
the middle of the "MainFrame".


You down with OOP? Yeah you know me!
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
well you would call the timer, then in the timer, once the
message box is visible, you would just count everytime the
timer is called, then you would .dispose() the message box
when you are out of the counter loop, and stop the current timer.

I agree. Here's the link:
subject: using threads or there's another way?
It's not a secret anymore!