JRootPane?

Discussion in 'Java' started by Knute Johnson, Nov 5, 2005.

  1. 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,

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Nov 5, 2005
    #1
    1. Advertising

  2. 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.?
     
    Andrew Thompson, Nov 5, 2005
    #2
    1. Advertising

  3. Knute Johnson

    Roedy Green Guest

    On Sat, 05 Nov 2005 07:13:20 GMT, Andrew Thompson
    <> 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.
    ..
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Nov 5, 2005
    #3
  4. Roedy Green wrote:

    > On Sat, 05 Nov 2005 07:13:20 GMT, Andrew Thompson
    > <> 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.
    > ..


    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. ]
     
    Andrew Thompson, Nov 5, 2005
    #4
  5. Knute Johnson

    Roedy Green Guest

    On Sat, 05 Nov 2005 10:35:22 GMT, Andrew Thompson
    <> 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?
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Nov 5, 2005
    #5
  6. Andrew Thompson wrote:
    > 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.?


    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,

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Nov 5, 2005
    #6
  7. Knute Johnson wrote:
    ...
    > 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.
     
    Andrew Thompson, Nov 5, 2005
    #7
  8. Andrew Thompson wrote:
    > Knute Johnson wrote:
    > ..
    >
    >> 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.
    >

    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 :).

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Nov 6, 2005
    #8
  9. Knute Johnson wrote:
    .....
    > 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.
     
    Andrew Thompson, Nov 6, 2005
    #9
  10. Knute Johnson

    Michael Dunn Guest

    "Knute Johnson" <> wrote in message
    news:ticbf.7405$...

    > 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());
    }
    }
    }
     
    Michael Dunn, Nov 6, 2005
    #10
  11. Michael Dunn wrote:
    > "Knute Johnson" <> wrote in message
    > news:ticbf.7405$...
    >
    >
    >>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


    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.

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Nov 6, 2005
    #11
  12. Knute Johnson

    Roedy Green Guest

    On Sat, 05 Nov 2005 10:53:56 -0800, Knute Johnson
    <> 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.

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Nov 6, 2005
    #12
  13. Knute Johnson

    Roedy Green Guest

    On Sat, 05 Nov 2005 21:35:49 GMT, Andrew Thompson
    <> 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.

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Nov 6, 2005
    #13
  14. Knute Johnson

    Michael Dunn Guest

    "Knute Johnson" <> wrote in message
    news:GAibf.7463$...
    > Michael Dunn wrote:
    >> "Knute Johnson" <> wrote in message
    >> news:ticbf.7405$...


    >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");
    }
    }
     
    Michael Dunn, Nov 6, 2005
    #14
  15. Michael Dunn wrote:
    > "Knute Johnson" <> wrote in message
    > news:GAibf.7463$...
    >
    >>Michael Dunn wrote:
    >>
    >>>"Knute Johnson" <> wrote in message
    >>>news:ticbf.7405$...

    >
    >
    >>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");
    > }
    > }
    >
    >


    That's a not a bad idea Michael, I just might use that one.

    Thanks,

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Nov 6, 2005
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page