views:

562

answers:

3

How do I get my netbeans drag and drop widget to know whether it is rendering inside the netbeans design view window or the running application?

I'm trying to do some custom rendering. I think it has to do with the root container.

A: 

Try java.beans.Beans.isDesignTime().

Dave Ray
A: 

This is another method:

Component c = javax.swing.SwingUtilities.getRoot(this);
String className = c.getClass().getCanonicalName();
if (!"org.netbeans.core.windows.view.ui.MainWindow"
    .equalsIgnoreCase(className)) {

Although I think the

 Beans.isDesignTime()

method is better

hawkeye
A: 

Testing the

Beans.isDesignTime()

with the following example

package test;

import java.awt.Graphics;
import java.beans.Beans;

import javax.swing.JLabel;

public class TestLabel extends JLabel {
private static final long serialVersionUID = -2438507032083091628L;

public TestLabel() {
 super();
}

public void paint(Graphics g) {
 super.paint(g);

 if (Beans.isDesignTime()) 
  this.setText("Design Time");
 else
  this.setText("Production runtime");
}
}

It works - that's quite incredible.

hawkeye
I'm also amazed when things actually work.
Dave Ray