I have a JDialog jd that has a JPanel jp added to its content pane.
The JPanel jp has a ComponentListener added to it using the
addComponentListener() method but when I call jp.setBounds(x,y,w,h)
the componentResized method never get executed! I can see that the
setBounds call is working if I override the setBounds method and put
in a debug statement it prints out. I even checked to make sure the
ComponentListener was properly added and it was. But it doesn't
execute. Anyone run into this before? Does this have something to do
with the fact that the JPanel is inside of a JDialog???
Failing that, is there a way to draw a JFrame without the top
decorations (icon, close/minimize/maximize buttons, etc.) ? I tried
using a JWindow originally but ran into trouble getting it to maintain
focus for keyboard input, hence the JDialog.
??? :(
-Will
Knute Johnson - 31 Jan 2008 23:58 GMT
> I have a JDialog jd that has a JPanel jp added to its content pane.
> The JPanel jp has a ComponentListener added to it using the
[quoted text clipped - 5 lines]
> execute. Anyone run into this before? Does this have something to do
> with the fact that the JPanel is inside of a JDialog???
componentResized() won't get called if the JPanel isn't actually
changing size. A subsequent call with the same bounds will not produce
a ComponentEvent.
> Failing that, is there a way to draw a JFrame without the top
> decorations (icon, close/minimize/maximize buttons, etc.) ? I tried
> using a JWindow originally but ran into trouble getting it to maintain
> focus for keyboard input, hence the JDialog.
> ??? :(
> -Will
Frame.setUdecorated(boolean state). The JFrame/Frame must not yet be
displayable when this method is called.
I can tell you that ComponentListener has a lot of quirks and they
aren't in the docs. There is some info in the Java Tutorial but I have
never been able to use it for much.

Signature
Knute Johnson
email s/nospam/knute/
Knute Johnson - 01 Feb 2008 00:03 GMT
> I have a JDialog jd that has a JPanel jp added to its content pane.
> The JPanel jp has a ComponentListener added to it using the
[quoted text clipped - 12 lines]
> ??? :(
> -Will
I forgot to put in my test program. This will show changes in bounds
but a second press of the button will not produce any ComponentEvent.
Another aggravating feature is that componentShown() doesn't get called
when the component is displayed only after it has been hidden and then
unhidden will it generate the event.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class test extends JPanel implements ComponentListener {
public test() {
setBackground(Color.BLUE);
addComponentListener(this);
}
public void componentHidden(ComponentEvent ce) {
System.out.println(ce);
}
public void componentMoved(ComponentEvent ce) {
System.out.println(ce);
}
public void componentResized(ComponentEvent ce) {
System.out.println(ce);
}
public void componentShown(ComponentEvent ce) {
System.out.println(ce);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setUndecorated(true);
f.setLayout(null);
f.setSize(640,480);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final test t = new test();
t.setBounds(20,10,200,150);
f.add(t);
JButton b = new JButton("Bounds");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
t.setBounds(50,40,100,75);
}
});
b.setSize(b.getPreferredSize());
b.setLocation(100,400);
f.add(b);
f.setVisible(true);
}
});
}
}

Signature
Knute Johnson
email s/nospam/knute/
Michael Dunn - 01 Feb 2008 11:13 GMT
> I even checked to make sure the ComponentListener was properly added and it was.
if the dialog is modal, and the componentListener is coded after setVisible(true),
then it has been added incorrectly.
how about posting the code?
larkmore@aol.com - 01 Feb 2008 13:44 GMT
Thanks for the help guys. I'll try to address everything said so far.
Knute Johnson: Yes, I am aware that resized only happens when size
changes. I am actually looking for the call the very first time that
setBounds() is ever called, so I would assume that counts as a change
in size. At least, it works in all my other programs that use a
JFrame as the very top level container. I've also run into the
componentShown() quirk you mentioned. Bit me when I was trying to use
it to trigger setting the focus. I cannot fathom why someone would
intentionally name a method like that and then not have it work the
first (and every) time that the component was shown... I'll have to
try the Frame.setUdecorated(boolean state) method you suggested.
Maybe that will fix everything.
Michael Dunn: I'm not sure I understand what you said. I have a
JDialog that is instantiated by another class that acts as the backend
to the system. It takes care of all the behind the scenes control and
coordination and talking to serial ports and sockets. The backend
then instantiates several classes that extend JPanel, each of which
has a custom componentListener added to it as part of its constructor
method. The backend then responds to various stimuli by choosing one
of the JPanel subclasses and adding it to the JDialog's content pane,
after removing any other JPanels that may have been contained by it.
As part of the method in the JDialog that adds the panels to the
content pane, it also resizes each panel by calling its setBounds()
method. The call to setBounds() is working fine, both visually and
through println statements inside the setBounds() method, but the
componentResized() method of the componentListener never gets called!
As of right now I'm manually fetching the component listener for each
JPanel and then manually calling its componentResized() method, but
that is a really big hack and really shouldn't be necessary!
Sorry I can't post any code, it's part of my work and isn't public.
-Will
Lew - 01 Feb 2008 13:48 GMT
> Sorry I can't post any code, it's part of my work and isn't public.
Consider constructing and posting to here an SSCCE [1] that evinces the
behavior of interest.
<http://www.physci.org/codes/sscce.html>
You want alt.clairvoyant.help otherwise.

Signature
Lew