|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.ccg.swing.UpdateSafely
public class UpdateSafely
Class to make the updating of Swing components safe.
Swing components can not be freely modified by any thread running within a JVM. For example, this means that if one tries to update the text of a component from a thread which is external to the main Swing event dispatching thread, unpredictable things may occur.
The SwingUtilities class provides a set of
methods to make the updating of swing components safe for any
thread. This class attempts to encapsulate these features into
something which is a little easier (more convienent) to work
with. You use this class in the following manner:
JComponent objects to it.
smartUpdate()" method to safely
update the component(s) with your new properties.
SwingUtilities,
Grouping| Constructor Summary | |
|---|---|
UpdateSafely()
Default constructor. |
|
UpdateSafely(Object o)
Constructor for a single component. |
|
| Method Summary | |
|---|---|
Color |
getBackground()
Get the background color to apply to the
components. |
Boolean |
getEnabled()
Get whether the component should be enabled or disabled. |
Color |
getForeground()
Get the foreground color to apply to the
components. |
Boolean |
getSelected()
Get whether the components should be selected or not. |
String |
getText()
Get the new value for the label field. |
boolean |
isAutoPropertyClearEnabled()
Should properties be automatically cleared after updating? |
void |
run()
Method which updates the component(s) in the
background. |
void |
setAutoPropertyClearEnabled(boolean val)
Set should we clear propertries after updating. |
void |
setBackground(Color val)
Set the background color to apply to the
components. |
void |
setEnabled(Boolean val)
Set whether the component should be enabled or disabled. |
void |
setForeground(Color val)
Set the foreground color to apply to the
components. |
void |
setSelected(Boolean val)
Set whether the components should be selected or not. |
void |
setText(String val)
Set the new value for the label field. |
void |
smartAdd(Object o)
Add more component(s) to the set we can update. |
void |
smartUpdate()
The "non-static" version of "smart update". |
static void |
smartUpdateJLabel(JLabel label,
String val)
The "thread safe" way to change the value of a GUI label. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public UpdateSafely()
You'll want to use the "add"/"set" methods after creating a object in this fashion.
public UpdateSafely(Object o)
Constructs the object and does a "smart add" of the object(s) which you specify.
o - Typically a JComponent, but it can be any object
supported by the Grouping.smartAdd(object) method.| Method Detail |
|---|
public void smartAdd(Object o)
component(s) to the set we can update.
This method allows one to add more component(s) to the group of components which we allow you to
update on the fly.
o - Typically a JComponent, but it can be any object
supported by the Grouping.smartAdd(object) method.
public static void smartUpdateJLabel(JLabel label,
String val)
This static method will "safely" update the text of a JLabel component regardless of whether you invoke it from
the Swing event dispatching thread or not.
This method determines if the invoking thread is the Swing
event dispatching
thread or some external thread. If it is within the main Swing
event thread, we will simply update the label immediately. If
not, we will create a new UpdateSafely
object and pass it to the invokeLater(Runnable) method so that the update can occur at a
"safe" time in the Swing universe.
label - The label component to update. You can pass
null, but we won't do anything.val - The value to assign to the label. You can pass null and we
will clear the contents of the label (same as passing "").smartUpdate()public void smartUpdate()
This is similar to the static version of the "smartUpdate()"
method. This method updates the contents of the associated label
in a "safe" manner regardless of what JVM thread you invoke it
from (it makes use of the invokeLater(Runnable) method if necessary).
smartUpdateJLabel(JLabel,String)public void run()
component(s) in the
background.
This method is intended to be run at a later time by the Swing event dispatcher thread (when it is safe to change the value(s) of the component(s)).
If there are any component(s) set, the
following will be done:
specified.
specified.
specified.
specified.
specified.
After the updates have been applied to the component(s), the
current settings will be cleared from this object UNLESS you
explicitly tell the object to keep the properties applied (via
the setAutoPropertyClearEnabled(boolean) method).
NOTE: This method should never be invoked directly (unless you
know you are within the Swing event dispatching thread). Instead,
either use the smartUpdate() method which
will safely invoke this method, or pass the entire object to
SwingUtilities.invokeLater(java.lang.Runnable) method.
run in interface RunnablesmartUpdate()public void setText(String val)
value for the label field.
val - New String value to assign.getText()public String getText()
value for the label field.
setText(java.lang.String)public void setAutoPropertyClearEnabled(boolean val)
By default, the properites which you set (such as text, colors, enabled, etc) are cleared once we apply them to the components. This is typically the desired behavior. If you would like this object to retain its property settings AFTER they have been applied, then you need to set the value to false.
val - New boolean value to assign.isAutoPropertyClearEnabled()public boolean isAutoPropertyClearEnabled()
setAutoPropertyClearEnabled(boolean)public void setForeground(Color val)
color to apply to the
components.
val - New Color value to assign or null if you don't want to change
the color.getForeground()public Color getForeground()
color to apply to the
components.
setForeground(java.awt.Color)public void setBackground(Color val)
color to apply to the
components.
val - New Color value to assign or null if you don't want to change
the color.getBackground()public Color getBackground()
color to apply to the
components.
setBackground(java.awt.Color)public void setEnabled(Boolean val)
val - Boolean object which indicates whether to enable or disable
the components (or null if you don't want to change the
enable/disable state).getEnabled()public Boolean getEnabled()
setEnabled(java.lang.Boolean)public void setSelected(Boolean val)
val - Boolean object to use to set the selected state of the
components, OR null if you don't want to change the selected
state.getSelected()public Boolean getSelected()
setSelected(java.lang.Boolean)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||