Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Jframe title setting question

  1. #1
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Jframe title setting question

    i was wondering what the difference between these 2 to set the title of a jframe?

    super("Main Frame");

    and

    setTitle("Main Frame");


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Jframe title setting question

    tl;dr version: there is no real difference. One calls the constructor, another calls the setTitle() method. They both go up to Frame, which sets its title variable.

    Look at the code for JFrame (the source is available):

       public JFrame(String title) throws HeadlessException {
            super(title);
            frameInit();
        }
     
       //there is no setTitle() method in JFrame

    And for Frame:
        public Frame(String title) throws HeadlessException {
            init(title, null);
        }
     
        private void init(String title, GraphicsConfiguration gc) {
            this.title = title;
            SunToolkit.checkAndSetPolicy(this, false);
        }
     
        public void setTitle(String title) {
            String oldTitle = this.title;
            if (title == null) {
                title = "";
            }
     
     
            synchronized(this) {
                this.title = title;
                FramePeer peer = (FramePeer)this.peer;
                if (peer != null) {
                    peer.setTitle(title);
                }
            }
            firePropertyChange("title", oldTitle, title);
        }

    Does that answer your question?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following 2 Users Say Thank You to KevinWorkman For This Useful Post:

    derekxec (June 15th, 2011), william (June 16th, 2011)

  4. #3
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: Jframe title setting question

    that does thank you very much...the book im reading says you can set either way but doesnt explain why you can or why you should set it this way over that way

  5. #4
    Member
    Join Date
    Jun 2011
    Location
    Rhode Island
    Posts
    69
    My Mood
    Bored
    Thanks
    11
    Thanked 7 Times in 6 Posts

    Default Re: Jframe title setting question

    Quote Originally Posted by KevinWorkman View Post

    firePropertyChange("title", oldTitle, title);

    [/highlight]
    Kevin, I have a question for you. You used firePropertyChange what is the purpose for this and why. Sorry I have never figured out how to use this properly.

    thanks,

  6. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Jframe title setting question

    Quote Originally Posted by derekxec View Post
    that does thank you very much...the book im reading says you can set either way but doesnt explain why you can or why you should set it this way over that way
    Use the constructor if you know the title at time you're creating a JFrame (which will be most cases, which is probably why it's provided. Use setTitle() if you have to set or change the title after the JFrame has already been created.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Jframe title setting question

    Quote Originally Posted by william View Post
    Kevin, I have a question for you. You used firePropertyChange what is the purpose for this and why. Sorry I have never figured out how to use this properly.

    thanks,
    Correction- I didn't use anything, that's just code I copied from the JDK source. Presumably, the firePropertyChange() method is called to generate a PropertyChangeEvent, which will be caught by any PropertyChangeListeners listening for the "title" property. For example:

    import java.awt.event.*;
    import java.beans.PropertyChangeEvent;
    import java.beans.PropertyChangeListener;
     
    import javax.swing.*;
     
     
    public class FrameListenersTest {
     
     
    	public FrameListenersTest(){
     
    		final JFrame frame = new JFrame("Listeners Test");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(200, 200);
     
    		frame.addPropertyChangeListener("title", new PropertyChangeListener(){
     
    			@Override
    			public void propertyChange(PropertyChangeEvent e) {
    				System.out.println("Old: " + e.getOldValue());
    				System.out.println("New: " + e.getNewValue());
    				System.out.println();
    			}
     
    		});
     
     
     
    		frame.setVisible(true);
     
     
    		Timer timer = new Timer(2000, new ActionListener(){
    			int i = 0;
    			public void actionPerformed(ActionEvent e){
    				i++;
    				frame.setTitle("Title " + i);
    			}
    		});
     
    		timer.start();
    	}
     
     
     
    	public static void main(String... args){
    		new FrameListenersTest();
    	}
    }

    PS- The person who wrote the code that uses the PropertyChangeListener in the Frame class seems to be Sami Shaio. I think he wrote a lot of the stuff in AWT. That's your bit of trivia for the day.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. The Following User Says Thank You to KevinWorkman For This Useful Post:

    william (June 16th, 2011)

Similar Threads

  1. JFrame Question
    By jayjay89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 29th, 2011, 07:24 AM
  2. taking JFrame title into account when calling pack()
    By gib65 in forum AWT / Java Swing
    Replies: 3
    Last Post: October 7th, 2010, 09:19 PM
  3. Setting JFrame maximum size that pack() can not violate
    By Javabeginner in forum AWT / Java Swing
    Replies: 3
    Last Post: September 3rd, 2010, 05:53 AM
  4. Setting JFrame to be only selectable window
    By aussiemcgr in forum AWT / Java Swing
    Replies: 3
    Last Post: July 22nd, 2010, 12:43 PM
  5. JFrame, frame's title doesnt appear.
    By chronoz13 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 26th, 2009, 03:38 PM