Sign in to follow this  

[java] Simple Java+Swing Questions

This topic is 3319 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have three beginner questions regarding Swing and Java. Consider the following example:
public class HelloWorld {
 
    public static void main(final String[] args) {
       // Create frame with title "Hello, World!"
       JFrame frame = new JFrame("Hello, World!");
 
       //This stops the app on window close.
       frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
 
       // add a label in the frame, same as frame.getContentPane().add
       frame.add(new JLabel("Hello, World!"));
 
       // make sure Size of the frame is set according to its content
       frame.pack();
 
       // set the frame visible
       frame.setVisible(true);
    }
}
(1) When does the Swing draw/event thread officially start? (2) It seems to be the case that when you create a object like JFrame some other object maintains a duplicate reference to it. Is there a global swing object that keeps track of all widgets? Can I forget about maintaining references to widgets when I don't need to refer to them again? (3) In C++ for example, one must block the main thread while other worker threads execute. I noticed that in Java the main thread can end, but the swing thread will continue to run. Is that right?

Share this post


Link to post
Share on other sites
(1) When does the Swing draw/event thread officially start?

____

No clue myself but you can hedge some bets of it possibly being different on some JVMs.

(2) It seems to be the case that when you create a object like JFrame some other object maintains a duplicate reference to it. Is there a global swing object that keeps track of all widgets? Can I forget about maintaining references to widgets when I don't need to refer to them again?

_____

There are three types of memory in Java: The stack which contains your instructions, the 'forgoten name here' which contains primitives including the primitive type that points/references Objects in the heap, The heap which contains objects.

This means that when you pass primitives (int, char, double, and likely everything that's listed as implementing the Comparable class) you're passing it by value. when passing a normal object you're passing the primitive that holds the memory location/reference/pointer to all the data so essentially by reference.

This means stuff like this happens:

When setting something like say a new Jframe to an old one you take that reference to the old one instead of taking that old one and making a new copy

when passing objects around you pass that reference. and can obviously mess up that object by changing it or do very favorable stuff like passing Graphics from a frame to your sprite class.

//before passing to method
primitive value: 0 String value: Hello, World true Object: January 1, 2006
//after passing to method before changing
primitive value: 0 String value: Hello, World true Object: January 1, 2006
//after changing in method
primative value: 5 String value: Not very pointy-reffy true Object: January 1, 2007
//after exiting method
primitive value: 0 String value: Hello, World true Object: January 1, 2007




public static void main(String[] args)
{
// TODO Auto-generated method stub

// Create frame with title "Hello, World!"
JFrame frame = new JFrame("Hello, World!");
//EDIT HERE
JFrame secondFrame = frame;

//This stops the app on window close.
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

// add a label in the frame, same as frame.getContentPane().add
//EDIT HERE
secondFrame.add(new JLabel("Pointy-Reffy, World!"));

// make sure Size of the frame is set according to its content
frame.pack();

// set the frame visible
frame.setVisible(true);
//secondFrame.setVisible(true);

int primativeType = 0;
String objectType = "Hello, World"; //The class types for primitives are goony
GregorianCalendar someDay = new GregorianCalendar(2006, 0, 1); //Calendars are a fancy wrapper class around a long
long DateInMS = someDay.getTimeInMillis();

DateFormat longDate = DateFormat.getDateInstance( DateFormat.LONG );

//display
System.out.println("primative value: " + primativeType +
" String value: " + objectType +
" true Object: " + longDate.format(DateInMS));

//call method
quickEdit(primativeType, objectType, someDay);

//demo changes
DateInMS = someDay.getTimeInMillis();
System.out.println("primative value: " + primativeType +
" String value: " + objectType +
" true Object: " + longDate.format(DateInMS));
}

public static void quickEdit(int newPrim, String objectRef, GregorianCalendar anotherDay)
{
//display before changes
DateFormat longDate = DateFormat.getDateInstance( DateFormat.LONG );
long DateInMS = anotherDay.getTimeInMillis();

System.out.println("primative value: " + newPrim +
" String value: " + objectRef +
" true Object: " + longDate.format(DateInMS));

//display after changes
newPrim = 5;
objectRef = "Not very pointy-reffy";
anotherDay.add(Calendar.YEAR, 1);
DateInMS = anotherDay.getTimeInMillis();

System.out.println("primative value: " + newPrim +
" String value: " + objectRef +
" true Object: " + longDate.format(DateInMS));
}






(3) In C++ for example, one must block the main thread while other worker threads execute. I noticed that in Java the main thread can end, but the swing thread will continue to run. Is that right?

Yes it will. The JVM in theory perfectly schedules all the threads and in the optimal way for that system and that OS :big laugh here:.

However java JVMs tend to implement threading in slightly different ways The most noticeable being that XP's timer resolution is increments of 15ms while other OS tend to be 1 to 5ms meaning that that's the minimal sleep time. The JVMs also treat threading differently some will favor doing one task till it's done while others will fairly split the time between threads(you can control it yourself by using thread.sleep(milliseconds) and thread.yield() though)

Share this post


Link to post
Share on other sites
The garbage collect also removes everything in the hash, stack, 'whatever that name is here' when there is nothing that can reference it in ANY shape or form. Which means you will have difficulty getting a memory leak but still can.

The garbage collector also runs 'whenever' throughout the program. you can 'encourage' it to run with System.gc() but the JVM can decide not to.

Share this post


Link to post
Share on other sites

This topic is 3319 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this