Java Forum / General / November 2005
JRootPane?
Knute Johnson - 05 Nov 2005 07:45 GMT I've got a JButton on a JPanel on a JTabbedPane on a JFrame. I'm trying to set the JButton to the default button so that when you press the enter key the button is pressed. The problem is I can't get the RootPane to call setDefaultButton() on. I get NullPointerExceptions on any of the calls to getRootPane(). Any ideas why? Is there something funny about JTabbedPanes?
Thanks,
 Signature Knute Johnson email s/nospam/knute/
Andrew Thompson - 05 Nov 2005 08:13 GMT Knute Johnson (or possibly someone impersonating the aforementioned - poorly) wrote:
> ...Is there something funny about JTabbedPanes? ..dunno. Maybe there is something funny about that weed you seem to be smoking.
<sscce> import java.awt.*; import javax.swing.*;
/* JButton on a JPanel on a JTabbedPane on a JFrame Huh? 'b3' is default here.. (no NPE) */ public class DefaultButton { public static void main(String[] args) { JFrame f = new JFrame("Default Button"); JButton b1 = new JButton("Button 1"); JButton b3 = new JButton("Button Default"); JButton b2 = new JButton("Button 2"); JPanel p = new JPanel(new GridLayout(0,1)); p.add(b1); p.add(b3); p.add(b2); JTabbedPane tp = new JTabbedPane(); tp.add( p ); f.getContentPane().add(tp); f.getRootPane().setDefaultButton(b3);
f.pack(); f.setVisible(true); } } </sscce>
BTW - Why did you post this to c.l.j.p, rather than c.l.j.g.?
Roedy Green - 05 Nov 2005 10:51 GMT On Sat, 05 Nov 2005 07:13:20 GMT, Andrew Thompson <seemysites@www.invalid> wrote, quoted or indirectly quoted someone who said :
>..dunno. Maybe there is something funny about that >weed you seem to be smoking. For god sake Andrew, that bitchiness was totally uncalled for. .
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Andrew Thompson - 05 Nov 2005 11:35 GMT > On Sat, 05 Nov 2005 07:13:20 GMT, Andrew Thompson > <seemysites@www.invalid> wrote, quoted or indirectly quoted someone [quoted text clipped - 5 lines] > For god sake Andrew, that bitchiness was totally uncalled for. > .. If I had thought Knute would see that as 'bitchiness' I'd not have said it. [ I sure would not have said it to you. ]
Roedy Green - 05 Nov 2005 15:57 GMT On Sat, 05 Nov 2005 10:35:22 GMT, Andrew Thompson <seemysites@www.invalid> wrote, quoted or indirectly quoted someone who said :
>If I had thought Knute would see that as 'bitchiness' I'd >not have said it. [ I sure would not have said it to you. ] What possible benefit could come from an insult like that?
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Knute Johnson - 05 Nov 2005 19:53 GMT > Knute Johnson (or possibly someone impersonating the > aforementioned - poorly) wrote: [quoted text clipped - 33 lines] > BTW - Why did you post this to c.l.j.p, rather > than c.l.j.g.? Andrew:
It was late, the smoke was thick and I was really tired of trying to get this sucker to work. What I didn't explain clearly was that I don't have a reference to the containing JFrame in my JPanel. So I attempted to use JComponent.getRootPane() and SwingUtilities.getRootPane(JComponent) and getTopLevelAncestor().getRootPane() all with the same result, null.
And thanks for sticking up for me Roedy.
Thanks,
 Signature Knute Johnson email s/nospam/knute/
Andrew Thompson - 05 Nov 2005 22:35 GMT ..
> And thanks for sticking up for me Roedy. ?? I am astounded, I thought you would be able to laugh at that.
Apparently very poor judgement on my part.
FWIW - Sorry.
And to Roedy, I am astounded that *you* would take exception to that statemnet, yet feel free to 'defend' people while in the same breath accusing them of being N*zi's (a word I - and most civilised people - consider to be more offensive than all the four letter swear words and drug allusions combined).
FTR Roedy, if the best you can say in my defense includes the word N*zi, please don't bother, or clearly label it as an attack, so that I can feel free to respond to it in the tone it deserves.
Knute Johnson - 06 Nov 2005 01:40 GMT > .. > [quoted text clipped - 5 lines] > > FWIW - Sorry. Andrew:
I guess I was too subtle with that too. I'm not unhappy about your reply, never was. In fact I thought it was kind of funny and I know that you meant it as a joke. And I only thanked Roedy because he thought that I might have been offended and came to my defense. I appreciate when people stick up for me even if I didn't need it.
So let's go back to yesterday and help me figure out how to get at the RootPane from inside my JPanel :-).
 Signature Knute Johnson email s/nospam/knute/
Andrew Thompson - 06 Nov 2005 01:32 GMT ....
> So let's go back to yesterday and help me figure out how to get at the > RootPane from inside my JPanel :-). (phew!) Happy to.
I'll give it some more thought, but don't see any immediate inspirations 'jumping out at me'. [ AFAIU - you know a load more about some aspects of GUIs/GUI programming than I do, so I doubt I will be the source of the arcane facts you are seeking, in any case! ]
I still feel, though I might not have clearly stated, that this thread would get better information on c.l.j.gui.
Michael Dunn - 06 Nov 2005 05:41 GMT > how to get at the RootPane from inside my JPanel :-). It has to be added to the content pane before you call your getParent()'s I gather you have no control over this, just the Panel. Perhaps a timer might be one way.
Something like this
import java.awt.*; import java.awt.event.*; import javax.swing.*; class Testing extends JFrame { public Testing() { setSize(100,100); setLocation(300,200); setDefaultCloseOperation(EXIT_ON_CLOSE); JTabbedPane tp = new JTabbedPane(); TabPanel p = new TabPanel(); tp.addTab("tab 1",p); tp.addTab("tab 2",new JPanel()); //p.setDefault(p.getParent());//if no timer - NPE here getContentPane().add(tp); //p.setDefault(p.getParent());//if no timer - OK here } public static void main(String[] args){new Testing().setVisible(true);} } class TabPanel extends JPanel { JButton btn = new JButton("OK"); javax.swing.Timer timer; public TabPanel() { add(btn); btn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ JOptionPane.showMessageDialog(null,"OK");}}); ActionListener al = new ActionListener(){ public void actionPerformed(ActionEvent ae){ setDefault(TabPanel.this.getParent());}}; timer = new javax.swing.Timer(100,al); timer.start(); } public void setDefault(Container c) { if(c.isVisible()) { if(c instanceof JRootPane) { ((JRootPane)c).setDefaultButton(btn); timer.stop(); return; } else setDefault(c.getParent()); } } }
Knute Johnson - 06 Nov 2005 07:49 GMT >>how to get at the RootPane from inside my JPanel :-). > [quoted text clipped - 3 lines] > > Something like this Thanks very much. I shouldn't be working this late at night or I would have figured that one out (maybe?). Your example has the one problem though that I was worried about and that is that the button gets pressed even if the tabbed pane that it lives on is not the current tab.
I think I'm going to give up on this route.
 Signature Knute Johnson email s/nospam/knute/
Michael Dunn - 06 Nov 2005 17:44 GMT >the button gets pressed even if the tabbed pane that it lives on is not >the current tab. seerms to work OK doing it this way (1.5.0_03)
import java.awt.*; import java.awt.event.*; import javax.swing.*; class Testing extends JFrame { public Testing() { setSize(100,100); setLocation(300,200); setDefaultCloseOperation(EXIT_ON_CLOSE); JTabbedPane tp = new JTabbedPane(); TabPanel p = new TabPanel(); tp.addTab("tab 1",p); tp.addTab("tab 2",new JPanel()); getContentPane().add(tp); } public static void main(String[] args){new Testing().setVisible(true);} } class TabPanel extends JPanel { JButton btn = new JButton("OK"); public TabPanel() { add(btn); btn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ buttonAction();}}); KeyboardFocusManager.getCurrentKeyboardFocusManager() .addKeyEventDispatcher(new KeyEventDispatcher(){ public boolean dispatchKeyEvent(KeyEvent ke){ if(TabPanel.this.isVisible() && ke.getID() == KeyEvent.KEY_PRESSED) { if(ke.getKeyCode() == KeyEvent.VK_ENTER) buttonAction(); } return false;}}); } public void buttonAction() { JOptionPane.showMessageDialog(null,"OK"); } }
Knute Johnson - 06 Nov 2005 22:55 GMT >>the button gets pressed even if the tabbed pane that it lives on is not >>the current tab. [quoted text clipped - 43 lines] > } > } That's a not a bad idea Michael, I just might use that one.
Thanks,
 Signature Knute Johnson email s/nospam/knute/
Roedy Green - 06 Nov 2005 08:08 GMT On Sat, 05 Nov 2005 21:35:49 GMT, Andrew Thompson <seemysites@www.invalid> wrote while drunk out of his mind on Watney's said:
>?? I am astounded, I thought you would be able to laugh at that. There is nothing in the least bit funny about insulting someone or accusing them of illegal activity or incompetence.
Humour requires an element of surprise, a sudden revealing of a double meaning, some element of bizarre incongruity, some clever word play...
An insult can be humorous, but an insult in and of itself is not funny. And even humorous insults are still insults. The point of them is ridicule. Look at all the energy that goes into humorously lampooning political figures.
Your joke might work, however, if you accused Mother Theresa of toking up.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 06 Nov 2005 07:54 GMT On Sat, 05 Nov 2005 10:53:56 -0800, Knute Johnson <nospam@ljr-2.frazmtn.com> wrote, quoted or indirectly quoted someone who said :
>to use JComponent.getRootPane() and >SwingUtilities.getRootPane(JComponent) and >getTopLevelAncestor().getRootPane() all with the same result, null. JRootPane rootPane = jframe.getRootPane();
Your real problem was getting the JFrame.
GetParent iteratively with a check for JFrame instance is I guess what you needed, or embed a back link when you added the JPanel.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|