Solved

JPanel don't paint its children

Posted on 1998-11-11
5
383 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to parse custom JSON to POJO java 4 72
backtracking recursion  code 19 57
throw exception 21 42
Java: How do I open the default windows program for a given file type 3 28
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

813 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

15 Experts available now in Live!

Get 1:1 Help Now