Solved

JPanel don't paint its children

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

738 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