Link to home
Start Free TrialLog in
Avatar of richi111098
richi111098

asked on

JPanel don't paint its children

This is the code of the simple bean NeuJList:

public class NeuJList extends Component {
  private JButton button1;
  private JButton button2;
  private JPanel jPanel1;
  FlowLayout flowLayout1 = new FlowLayout();
  LineBorder lineborder =new LineBorder(Color.black,4);

  public NeuJList() {
    try  {
      jbInit();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  private void jbInit() throws Exception {
    button1=new JButton("hello1");
    button2=new JButton("hello2");

    jPanel1 = new JPanel();
    jPanel1.setBackground(new Color(192,192,255));
    jPanel1.setLayout(flowLayout1);
    jPanel1.setDoubleBuffered(false);

    button1.setSize(71,71);
    button1.setBackground(new Color(255,192,192));
    button2.setSize(71,23);
    button2.setBackground(new Color(255,192,192));

    jPanel1.add(button1,null);
    jPanel1.add(button2,null);
    jPanel1.setBorder(lineborder);
    jPanel1.setOpaque(true);
    jPanel1.setVisible(false);
  }

  public void paint(Graphics g) {
    jPanel1.setSize(getSize().width-10, getSize().height-10);
    jPanel1.paint(g);
    jPanel1.paintChildren(g);
  }
}

By using the bean (instantiate it), only the the jpanel is shown without the two buttons.
NeuJList is inherited from Component, so that the UI-Designer (e.g. the Beanbox)
identify the bean as a component and not as a container.
I had to overwrite the method paint, so that the JPanel is painted.

Why is only shown the panel and not the buttons, and how can I paint the children of this panel?
(If I write buttton1.paint(g) and button2.paint(g), the buttons are shown but only in the left-upper corner.)
ASKER CERTIFIED SOLUTION
Avatar of heyhey_
heyhey_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of richi111098
richi111098

ASKER

BEAN1 is inherited from COMPONENT, because of the behaviour of the bean in an UI-Designer.
I use Jbuilder2. If the bean is inherited from CONTAINER, like PANEL or JPANEL the
UI-designer can put other components into BEAN1. I have not found a possibility to prevent that.

I have tested your recommendations: There is no difference using jPanel1.add(button1);
instead of jPanel1.add(button1, null);.

The button1 have the dimension, which I have written in the initialization sequence: (71,71).
I can paint the button by using button1.paint(g); but it is only shown, and I can not use it.

Thanks

Richard
1. if you want ONLY to paint the buttons on the Panel you can use Graphics object clipping / transalting capavilities

  Grapfics g1 = g.create(int x, int y, int width, int height)
  b1.paint(g1);
  Grapfics g2 = g.create(int x, int y, int width, int height)
  b1.paint(g2);

  so buttons will be shown in the left-upper corner of g1 / g2, which is the appropriate posistion (x,y,w,h) in g.
(maybe you can do the same thing using the LayoutManager, don't know ...)
2. if you need to USE the buttons, you have to use Container and add the buttons to it. when
you are drawing in the paint method you have only pictures / images of buttons - no functionality at all
if you need the functionality you have to use buttons as components ...

3. so your bean have to be Container ... but you can try to prevent adding new components inside it from the BeanBuilder
by overriding the add methods and doing nothing ...

  hope this helps
    heyhey

The question is answered. But I still can not make a Bean, which consists of other Beans, and which can not admit other beans in a designer during designtime. To overwrite the add method is not enough for designtime.

Thank you

Richard Leitner
and as far as I remember different BeanBuilders act very differently ...
but what are the exact problems that you came upon when overriding the add method ? (and JavaBean builder are you using ?)
I though that for general compatibility the builder should use your add methods .... also do you have your own BeanInfo? If you have some code (isolated example please :) you can send it to me (heyhey@nettaxi.com) so I can takew a look at it ...

stay cool
  heyhey