Swing components and the garbage collector
How much care does one need to take with swing components, with reference to running out of stack space? I ask because I'm writing a system for an automotive parts company, and the company tends to not reboot the machines very often...
Anyway... if a JFrame is closed, is it and all its components marked for GCing? Do I need to call the dispose method, or is this part of the default close behaviour? Or is there something else entirely?
Cheers
In class that calls for .setVisible(false) of one JFrame u MUST call
for dispose() method.
so
myFrame.setVisible(false)
myFrame.dispose()
myFrame = null // to help the garbage collector
If u have classes that becames from JFrame (or JDialog) u can
invoke in its constructors:
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)
This makes uncecessary to call for dispose() method explicetly.
If u do not have this kind of classes u can invoke this method
after u have create your JFrame
JFrame myFrame = new JFrame();
myFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)
The most important thing is REMOVE ALL LISETNERS that u have added, so, if u have classes that becames from JFrame u can ovverride
the dispose method to do that
eg
public void dispose()
super.dispose()
removeActionListener(.....)
and so on
If u do not have this kind of classes u must remove all listeners u have
added before u invoke the setVisible(false) method.
Regards
Similar Messages
-
How to get the garbage collector to work?
Hi,
i have i program where i load an image scale it down and save the scaled version in an array. I do this for a whole directory of images.
After every image i set the temporary variable for the loaded image = null and call the function System.gc().
My problem is, that the garbage collector does�nt give the memory of the loaded image free and the used memory of my program grows with every loaded image.
/* Reads all images from a folder an stores them in an Array of images */
public static BufferedImage[] readScaledImagesFromFolder(String folder,
int maxSize) {
File dir = new File(folder); /* Open the Folder */
String[] children = dir.list(); /* Get the children of the folder */
if (children == null) {
// Either dir does not exist or is not a directory
System.out.println("No images in the folder!");
return null;
} else {
/* Init array for images */
BufferedImage[] images = new BufferedImage[children.length];
int i = 0;
int index = 0;
BufferedImage temp;
String filename, fileending;
for (i=0; i<children.length; i++) {
// Get filename of file or directory
filename = children;
/* Get the fileending of the file */
fileending = filename.toLowerCase().substring(filename.length()-4);
if(fileending.equals(".jpg") || fileending.equals(".bmp")
|| fileending.equals(".png") || fileending.equals(".gif"))
/* Read the image */
temp = util.ImageUtils.loadBufferedImage(folder+"/"+filename);
/* Scale the image down and save it in an array */
images[index] = Util.getScaledImage(temp,maxSize);
index++;
temp = null;
System.gc();
Mosaic.sourceImageNum = index;
System.out.println((index+1)+" resized pictures loaded from folder: "+folder);
return images;
How can i get the gargabe collector to work after every iteration?
I tried to let the Thread.sleep(10) after System.gc() but it does�nt help.
Thank you every much
JackNeilHm yes.. i now that System.gc() is only a
suggestion.
But i know what my program is doing and that it
does�nt need the temporary image anymore after i have
a scaled down version. And the temporay image will become unreachable as soon as reading the next one overwrites your temp variable. Setting the variable to null doesn't have much effect.
It would be smarter to load the new image over the
old temporary image and not to expand the heapsize to
maximum.Then look at the possibitly of loading the next image into the same bufferedimage. -
How intelligent is the garbage collector?
Hi all,
I've got a question about how garbage collection works in an ABAP objects context.
If there are no more references an object is cleaned up and the memory released. Fine.
What about an object O1, which in turn creates another object O2. O2 however also contains a refernce to O1.
When the original process completes, this leaves two objects that reference each other, but with no other contact to the 'outside world'. Is the garbage collector intelligent enough to dump them?
Any input appreciated,
Cheers
MikeHi,
Thanks for the feedback. I am still not sure - you say 'when the program ends it's part'. This is exactly the point - when the program ends, do the objects remain because they still contain references to each other?
More detail:
The references are public instance attributes of both objects (different classes). I would prefer to use functional methods, but am stuck with public attributes which I'm populating in the constructor.
So a process P declares a local var LV_O1 and does a CREATE OBJECT LV_O1.
In the constructor of O1, it does a
CREATE OBJECT me->ATTR_O2 to populate it's attribute with a reference to an instance of O2.
O2 doesn't need to do a CREATE OBJECT, but assigns a ref to O1 to it's attribute ATTR_O1.
So now O1 and O2 refer to each other by public attributes:
O1->ATTR_O2 and O2->ATTR_O1.
The big question is what happens when process P ends and it's variable LV_O1 ceases to exist?
In case anyone's wondering about the overall wisdom of the design, they have to be public attributes (used in Workflow) and they have to refer to each other as instantiation may also happen the other way around.
Cheers
Mike -
Not getting swing components when the applet reloads
I am facing one problem that is related to reloading of swing components. I Have a JPanel inside JApplet which contains some swing components and a button to launch the same JPanel again in the JApplet. When I click on the button the JPanel should get added in the JApplet once again below the upper JPanel. But new JPanel is not getting displayed although the reference of the new JPanel is created.
My guess is that you haven't added your swing components to the right place. Are you adding them to the contentpane?
-
Is it possible to disable the garbage collector?
I was just wondering if it's possible to disable the garbage collector... Thank you.
Try running with the -Xnoclassgc parameter.
http://java.sun.com/developer/JDCTechTips/2004/tt0420.html#2 -
About the difference between the AWT components and the swing components
As we know that an AWT component has a peer, but dose a lightweight component(e.g: a swing component) has also a peer? And could somebody tell me that who and how creates the CLICKED_EVENT when I click a JButton(a swing component), and where the CLICKED_EVENT to go util it was processed by a method like btn_ok_clicked(Event e)? And if I click a Button(an AWT component), what's the difference?
Thanks a lot!The main difference is that AWT are native components, and Swing components are entirely in java written. That's besides the look and feel. There are a lot of other differences between AWT and Swing, and there are better described in the documentation.
-
Showing swing components in the palette og jdeveloper
hello, I have found a problem with jdeveloper, as a user uses jdeveloper since 2 years I have never found a problem like this. (IDE problem).
like habitude I created a simple java project which contains a jframe and an entry class (main class), but when I try to drag and drop from the component palette somes swing components I didn't find
the page of swing components ( I found in the components palette just "my components" page in the jcombobox selection category). this problem will delay a big project and causes big problems
please any person who know some things help me as soon as possible.
Best Regards.Check this post -
showing palette containing JButton,JLabel,JPanel ... components -
Swing components and Mac OS/9
Hi all,
I 've developped an app using swing components (Simple app using a hsqlbd database) and i read that it was kind of possible to port it on a mac os/9.
I had a look on the net, on the apple site, without any luck as how to "add" swing library on the mac os/9.
If someone could point me to the right direction (Web site, book etc), I 've been looking for a while without finding any clear and precise info on how to tackle this kind of problem.
Thanks in advance.As per my knowledge, u can use JJEdit editor on MAC OS X. By using this editor, you can run the applets. Here I am using JJEdit as a java editor on MAC OS X. just dowload the JJEdit. and run the applet on it.
regards
nr konjeti -
How does the Garbage Collector handle reference-free objects?
Hi,
I am interested to know how the GC handle's objects created
without a reference.
The reason this has become of interest to me is that I have
created a "Title" class. The Title class animates each letter of
Title.text to appear in a cloud of smoke.
The smoke is a simple particle system class, when a particle
dies it removes it's associated MovieClip so that eventually all
MovieClip's have been destroyed.
Now in the Title class for each letter I do the following
(psuedo-code):
for( Title.text.length) {
CurrentLetter.twAlpha = new Tween( blah, blah, blah); //
object created
with a reference
new Smoke( CurrentLetter.x, CurrentLetter.y); // object
created
without a reference
Although this is technique is not one I would ever have
thought of in a language that doesn't use a garbage collector it is
mentioned in the Tween documentation and my class works as
intended.
The thing is although it works, it always bothers me when I
don't know precisely
why it works!
If it's working due to the short life span of the class in
question and thus simply missing the GC's window then this could be
problematic. If at some point it is still alive when the GC is
called then my class could be prematurely deleted.
Maybe a class which has a reference to an "alive" MovieClip
is immune from GC?
I Hope someone can shed some light on this topic as the GC is
something that is thinly documented to say the least!
:theory
p.s. first post!Hi,
I would say it would be better use FREE itab at the end of the processing in your code. In the end-of-selection in your code, you can FREE all your itabs that were used in the program. This is one of the good approach of optimizing the memory.
Regards
Vimal -
Swing Components and Fullscreen Exclusive
Hi, I am trying to write a program that can switch between full screen and windowed mode seamlessly, while still being able to use standard swing components. I have never worked with the full screen exclusive mode before and am having trouble getting the components to repaint properly.
The following code works fine in the windowed and undecorated modes, however when I switch to fullscreen exclusive mode all components disappear and only repaint upon mouseover or other interaction. Calls to repaint do not make any difference.
import java.awt.Dimension;
import java.awt.DisplayMode;
import java.awt.FlowLayout;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class FullscreenTests {
private static boolean hwlast = false;
private static GraphicsDevice dev = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
public static void main(String[] args) {
final JFrame fMain = new JFrame("Title");
fMain.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
fMain.getRootPane().setLayout(new FlowLayout());
JButton win = new JButton("Windowed");
win.addActionListener(new ActionListener () {
public void actionPerformed (ActionEvent e) {
setWindowed(fMain);
fMain.getRootPane().add(win);
JButton ful = new JButton("Fullscreen");
ful.addActionListener(new ActionListener () {
public void actionPerformed (ActionEvent e) {
setFullscreen(fMain);
fMain.getRootPane().add(ful);
JButton hw = new JButton("HW Fullscreen");
hw.addActionListener(new ActionListener () {
public void actionPerformed (ActionEvent e) {
setHWFullscreen(fMain);
fMain.getRootPane().add(hw);
setWindowed(fMain);
public static void setWindowed(JFrame f) {
if (hwlast == true) {
dev.setFullScreenWindow(null);
hwlast = false;
f.dispose();
f.setIgnoreRepaint(false);
f.setUndecorated(false);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
f.setSize(new Dimension(808,634));
f.setMinimumSize(new Dimension(808,634));
f.setLocation((d.width-f.getWidth())/2, (d.height-f.getHeight())/2);
f.setVisible(true);
public static void setHWFullscreen(JFrame f) {
hwlast = true;
f.dispose();
f.setUndecorated(true);
f.setIgnoreRepaint(true);
f.setVisible(true);
dev.setFullScreenWindow(f);
dev.setDisplayMode(new DisplayMode(800, 600, 32, DisplayMode.REFRESH_RATE_UNKNOWN));
public static void setFullscreen(JFrame f) {
if (hwlast == true) {
dev.setFullScreenWindow(null);
hwlast = false;
f.dispose();
f.setIgnoreRepaint(false);
f.setUndecorated(true);
f.setSize(Toolkit.getDefaultToolkit().getScreenSize());
f.setLocation(0,0);
f.setVisible(true);
}On the same note, if there are better ways of doing this I would be glad to know. This is just a quick test so I haven't done any checks etc when switching to fullscreen exclusive.public static void setHWFullscreen (JFrame f) {
hwlast = true;
f.dispose ();
f.setUndecorated (true);
f.setIgnoreRepaint (true);
// add this
Graphics g = f.getGraphics ();
f.paintAll (g);
f.setVisible (true);
dev.setFullScreenWindow (f);
dev.setDisplayMode (new DisplayMode (800, 600, 32, DisplayMode.REFRESH_RATE_UNKNOWN));
}db -
Problem with the garbage collector
Well, I have a problem, and it's quite a big deal.
I don't understand why the java BC refuses to free the memory.
I have made a VERY LITTLE example, so that everybody will see what the problem is made with.
a frame with 2 buttons. the first one make another frame appears, which contains a quite big Swing component. The other is here in order to try to free the memory of this frame.
Here are the 2 classes :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FirstClass extends JFrame{
private JButton jButton1 = new JButton("free memory");
private JButton jButton2 = new JButton("load frame");
private MyFrameColor frame;
public FirstClass() {
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
jButton1_mouseClicked(e);
jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
jButton2_mouseClicked(e);
this.getContentPane().add(jButton1, BorderLayout.NORTH);
this.getContentPane().add(jButton2, BorderLayout.SOUTH);
this.show();
public static void main(String[] args)
new FirstClass();
import javax.swing.*;
import java.awt.*;
public class MyFrameColor extends JFrame {
JColorChooser jColorChooser1 = new JColorChooser();
public MyFrameColor() {
this.getContentPane().add(jColorChooser1, BorderLayout.CENTER);
}Some more info on garbage collection:
http://www.javaworld.com/javaworld/jw-12-2001/jw-1207-java101.html?
http://developer.java.sun.com/developer/technicalArticles/ALT/RefObj/ -
Flash components and the properties inspector
Don't know if any of you have had experience in creating
components that
can have properties set using the properties inspector or
not, but here
we go.
I have several components which all have a 'data' property
that is an
Object in which a developer can store arbitrary data.
I have set this up in the component so that it will display
in the
properties inspector, thusly:
[Inspectable(name="Data")]
private var _data:Object;
Now, it does indeed appear in the inspector, but when I click
on the
field it brings up a box which then doesn't allow me to enter
any data
(see screenshot:
http://www.moloko.f2s.com/filetransfer/componentParams.png)
However, if I right-click on the symbol in the library, go to
Component
Definition, then click on the Data property in the Value
column, it
brings up a box that's a little bit more what I was expecting
in that it
contains the '+' and '-' buttons to allow me to add/delete
data, and
also some up/down arrows to allow me to move the data around
in the list
(see screenshot:
http://www.moloko.f2s.com/filetransfer/componentDefinition.png)
Anyone know what's going on with this? Is this a bug in the
Flash IDE or
am I doing something wrong??
MOLOKO
Macromedia Certified Flash MX 2004 Developer
Macromedia Certified Flash MX Developer
::remove _underwear_ to reply::
'There ain't no devil - it's just God when he's drunk' Tom
Waits
GCM/CS/IT/MC d-- S++:- a- C++ U--- P+ L++ !E W+++$ N++ O? K+
w+++$ !O M+
VMS? PS+++ PE- Y PGP+ t+ 5-- X-- R* tv++ b++++ DI++++ D+ G e
h-- r+ y++The flashComponents are standard AS2 components, and you can look up their APIs in the Flash documentation. Here is the button docs:
http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00001977.html#3654189
Looking at that, there is a getStyle() method, and I just tested it out:
put sprite(1).getStyle("borderStyle")
-- "inset"
put sprite(1).getStyle("fontFamily")
-- "_sans"
put sprite(1).getStyle("color")
-- 734012.0000
I'm not real sure what that means (probably the hex color converted to a float), but you can change the color like this:
sprite(1).setStyle("color","red")
And here is a page that lists all the styles that are defined:
http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00001973.html#3136306 -
Components and The Transform Tool
I'm relitivly new to action script, and am just starting
component creation. I'm trying to deturmine if there is any way to
detect and respond to changes to an instance. Primarily, How can I
tell if the transform tool has been used on a specific instance and
respond accordingly. I'm interested in this both in the final SWF
and in the live preview.
For example, is there anyway to detect the change in the live
preview instance, and pass the value to xch the way you would from
the custom interface?
Thank you for your time and helphedger,
How do you expect anyone to help when we don't know a darned thing about the file, abut your setup, exact version of Photoshop and your OS, machine specs, etc.?
BOILERPLATE TEXT:
Note that this is boilerplate text.
If you give complete and detailed information about your setup and the issue at hand,
such as your platform (Mac or Win),
exact versions of your OS, of Photoshop (not just "CS6", but something like CS6v.13.0.6) and of Bridge,
your settings in Photoshop > Preference > Performance
the type of file you were working on,
machine specs, such as total installed RAM, scratch file HDs, total available HD space, video card specs, including total VRAM installed,
what troubleshooting steps you have taken so far,
what error message(s) you receive,
if having issues opening raw files also the exact camera make and model that generated them,
if you're having printing issues, indicate the exact make and model of your printer, paper size, image dimensions in pixels (so many pixels wide by so many pixels high). if going through a RIP, specify that too.
etc.,
someone may be able to help you (not necessarily this poster, who is not a Windows user).
a screen shot of your settings or of the image could be very helpful too.
Please read this FAQ for advice on how to ask your questions correctly for quicker and better answers:
http://forums.adobe.com/thread/419981?tstart=0
Thanks! -
Third party Swing components and ADF need tips
Need some tips how to
1 - create databindings for them
2 - automatically bind component when drag&drop from component palette
Message was edited by:
matalMatal,
we don't yet have documentation for creating custom Swing bindings to ADF. Automating the binding with drag and drop would be the logical follow up. Note that if your custom component shares the same model with another existing components, you can first drag and drop the existing component to then change it to an instance of your custom component.
Frank -
Determine event handler for swing components from the API browser
Is there a way to determine what event handlers are assocaited with the different swing classes. For example JTextField is associated to ActionListener, and JCheckbox uses the event handle ItemListener. Is there a way to determine this by looking at the class via the Java API?
Yes, there is. You'll observe that JTextField has an "addActionListener(ActionListener)" method, for a start. And JTextField is a subclass of JTextComponent, which has "addCaretListener(CaretListener)" and "addInputMethodListener(InputMethodListener)" methods. And JTextComponent is a subclass of JComponent, which has an "addAncestorListener(AncestorListener)" method. And so on... there are more. All of this can be found in the API documentation by looking for methods whose names start with "add".
Maybe you are looking for
-
Mail update and lost inbox messages
I am new to Macs. When I opened my Macbook today it automatically started an update to Mail. when it was done my settings for my ISP changed (eliminated my account settings and all that's there now is .Mac which I signed up for as a curiosity but don
-
I am looking in the Preferences folders and don't see what i'm looking for, but that's not surprising, I'm no guru, and I don't want to delete anything without knowing for sure. Thanks for any help!
-
How to configure print settings in smartforms
Hello friends, I was given a task to develop smart forms..everythign is working fine but i am left with following queires, so far i ddint maintain any print related parameters, i understand i need to work on three parameters now 1)CONTROL_PARAM
-
Imac G% tiger and Front Row Problem
Front Row do not recognize my Itunes library (tv shows, music etc...) How can I make it work I have Tiger and Imac g5(isisght) Thanks
-
2" Vertical Black Border on my desk-top
What have I done for this 2" black border on the left side and right side of my desk-top to show up? Why did it happen and What can I do to un-do it?