i was wondering what the difference between these 2 to set the title of a jframe?
super("Main Frame");
and
setTitle("Main Frame");
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.
i was wondering what the difference between these 2 to set the title of a jframe?
super("Main Frame");
and
setTitle("Main Frame");
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):
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!
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
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!
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!
william (June 16th, 2011)