JRootPane?

K

Knute Johnson

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,
 
A

Andrew Thompson

Knute Johnson (or possibly someone impersonating the
aforementioned said:
...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.?
 
A

Andrew Thompson

Roedy said:
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. ]
 
R

Roedy Green

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?
 
K

Knute Johnson

Andrew said:
Knute Johnson (or possibly someone impersonating the
aforementioned said:
...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,
 
A

Andrew Thompson

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.
 
K

Knute Johnson

Andrew said:
Knute Johnson wrote:
..



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

Andrew Thompson

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.
 
M

Michael Dunn

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());
}
}
}
 
K

Knute Johnson

Michael said:
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.
 
R

Roedy Green

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.
 
R

Roedy Green

?? 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.
 
M

Michael Dunn

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

Knute Johnson

Michael said:
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,
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top