Visual inheritance in Java

The team I work for is having problems with visual form inheritance in the Java platform.

We want to create a base JFrame with some controls in it and have inherited forms from that JFrame. Netbeans 6.1 designer does not render base form controls in inherited form so we cannot use it the way we want. At run time, the inherited forms are nicely colored, with parent controls.

Netbeans Designer

Has anyone solved this? Should we change the IDE?

+1


source to share


3 answers


AFAIK, Matisse is not really meant to create invisible shapes. It's for composition (i.e. Creates a custom JComponent and drags it into your Matisse form).

Form designer doesn't use java file to create form, it uses form file. It does not subside. If you absolutely need to use inheritance (which I would recommend against), then you will need to copy the contents of the "form" file into your child class.



Typically, you should create your own JPanel of some type (using Matisse or not) and then drop that form into your JFrame. Any custom code / events that need to be reused should be attached to the JFrame, created in beans, and then added to the form.

Netbeans 7 is considering updating the Matisse code. I would suggest including this as an extension request at www.netbeans.org .

+2


source


Just a general comment, not a real answer to your situation - visual components are not meant to be inherited. For example see Dreadful Cursed Sun made by inheriting everything from a Component - you now have a JList which you cannot use .add () to add items to. If you can't fulfill the entire superclass contract (and I only know a few classes that can), composition is preferred over inheritance. Highlight the methods you plan to execute and the Component getAsComponent () method explicitly declaring that the component should not be directly modified. I find this much easier than thinking "what should I do if someone uses add (JLabel) for my component? Or setLayout ()?".



As for your specific predicament - I use JFormDesigner in IntelliJ IDEA, but it can apply as well. JFD has a nice way of using the BeanInfo information (for a component named "MyThingy" the information class would be "MyThingyBeanInfo"). Using this class, you can define many things to consume only design time, as well as special drawing methods and other niceties.

+1


source


Jaguard, you must change the access control var (right click on the control, select "customize code", see window botton)

0


source







All Articles