• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using threads or there's another way?

 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

TiA
 
Henry Wong
author
Marshal
Pie
Posts: 21024
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Henry
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm not sure if i got it right:
my main:

my refactored code:
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
create a JFrame as private member data, create an inner class to display
the message

ex:

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());
.pack();
.setVisible(true);

now you have a JFrame you can call
.setVisible(true/false);
or
.dispose();

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

myFrame.setVisible(true);
myFrame.setAlwaysOnTop(true);

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".


Justin
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


Justin
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic