This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes How to make default JButton in child class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to make default JButton in child class" Watch "How to make default JButton in child class" New topic
Author

How to make default JButton in child class

Craig Hagerman
Greenhorn

Joined: Oct 08, 2001
Posts: 1
How can I make a button the default button?


I have read that I can use

getRootPane().setDefaultButton(buttonName);


This worked in a test program where there was only one class. However in another program involving two classes I can't get it to work.


Here is a snippet from the simple working class which extended JFrame:


...
Container contentpane = getContentPane();
JPanel panel = new JPanel();
JButton doit = new JButton("Do It");
panel.add(doit);
getRootPane().setDefaultButton(quit);
...


But in the second case the JPanel component above is a different class (TestPanel.java). And here this technique results in a nullPointer exception at runtime.


...
class TestPanel extends JPanel
{ public TestPanel()
{ JButton doit = new JButton("Do It");
add(doit);
getRootPane().setDefaultButton(doit);
....


I also tried other things like:

getParent().getRootPane().setDefaultButton(doit);


thinking that that would reference the parent JFrame-extended class, but no luck. (How do you use getParent()?)


Can anyone tell me how to set up a default JButton in such a case?
Craig Hagerman
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: How to make default JButton in child class
 
It's not a secret anymore!