Solved

JPanel don't paint its children

Posted on 1998-11-11
5
377 Views
Last Modified: 2012-08-13
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.)
0
Comment
Question by:richi111098
  • 3
  • 2
5 Comments
 
LVL 16

Accepted Solution

by:
heyhey_ earned 50 total points
ID: 1227192
i see some weird thing in the code:
1. why don't you add following line in jbInit method
 add( jPanel1)
2. why do you use
  jPanel1.add(button2,null);
instead of
  jPanel1.add(button2);

calling somobody else's paint method in your paint method is not good practice.
you'd better use the container-component architecture and ADD your components in their parent

when you add components, you don't have to think about painting, resizing etc...

(Hint: can't test your code at the moment, but your buttons are probably size 0x0 because the FlowLayoutManager had not done his job, because your jPanel1 have not been initialized on screen ...)
0
 

Author Comment

by:richi111098
ID: 1227193
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
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1227194
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

0
 

Author Comment

by:richi111098
ID: 1227195
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
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1227196
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
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now