Solved

JPanel don't paint its children

Posted on 1998-11-11
5
401 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.
Suggested Courses

628 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