ei Pessoal,
Programei um JavaBeans pra colocar dentro da Palheta do NetBeans, e esta’ dando um Estranho Erro quando salva, alem de nao dar o Refresh dentro do Form NetBeans.
E isto esta acontencendo quando uso java.awt.Image como propriedade.
seque o Erro :
‘Cannot Save the value of “backgroundImage” Property’
codigo do JavaBeans :
public class JPanel_Image extends JPanel {
java.awt.Image img = null;
public void setBackgroundImage(java.awt.Image newImage) {
Image old_img = img;
img = newImage;
propertyChangeListeners.firePropertyChange("backgroundImage", old_img, img );
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if ( !(img == null) ) {
Graphics2D g2D = (Graphics2D)g;
w = getSize().width;
h = getSize().height;
g2D.drawImage(img,0,0,img.getWidth(this),img.getHeight(this),this);
}
}
public Image getBackgroundImage() {
return img;
}
}
// BeanInfo
// --------------
public class JPanel_ImageBeanInfo extends SimpleBeanInfo {
// Bean descriptor information will be obtained from introspection.
private static BeanDescriptor beanDescriptor = null;
private static BeanDescriptor getBdescriptor(){
beanDescriptor = new BeanDescriptor(JPanel_Image.class);
return beanDescriptor;
}
// Properties information will be obtained from introspection.
private static PropertyDescriptor[] properties = null;
private static PropertyDescriptor[] getPdescriptor(){
PropertyDescriptor[] pds= null;
PropertyDescriptor _backgroundImage = null;
try {
_backgroundImage = new PropertyDescriptor("backgroundImage", JPanel_Image.class, "getBackgroundImage", "setBackgroundImage");
_backgroundImage.setDisplayName("Image Background");
_backgroundImage.setShortDescription("Image Background");
_backgroundImage.setPropertyEditorClass(EditorImage.class);
}
catch (IntrospectionException e) {
e.printStackTrace();
}
properties = new PropertyDescriptor[] {_backgroundImage };
return properties;
}
// Event set information will be obtained from introspection.
private static EventSetDescriptor[] eventSets = null;
private static EventSetDescriptor[] getEdescriptor(){
return eventSets;
}
// Method information will be obtained from introspection.
private static MethodDescriptor[] methods = null;
private static MethodDescriptor[] getMdescriptor(){
return methods;
}
private static java.awt.Image iconColor16 = null;
private static java.awt.Image iconColor32 = null;
private static java.awt.Image iconMono16 = null;
private static java.awt.Image iconMono32 = null;
private static String iconNameC16 = null;
private static String iconNameC32 = null;
private static String iconNameM16 = null;
private static String iconNameM32 = null;
private static int defaultPropertyIndex = -1;
private static int defaultEventIndex = -1;
public JPanel_ImageBeanInfo() {
super();
iconNameC16 = "JP1616C.gif";
iconNameC32 = "JP3232C.gif";
iconNameM16 = "JP1616M.gif";
iconNameM32 = "JP3232M.gif";
// defaultPropertyIndex = 1;
}
public BeanDescriptor getBeanDescriptor() {
return getBdescriptor();
}
public PropertyDescriptor[] getPropertyDescriptors() {
return getPdescriptor();
}
public EventSetDescriptor[] getEventSetDescriptors() {
return getEdescriptor();
}
public MethodDescriptor[] getMethodDescriptors() {
return getMdescriptor();
}
public int getDefaultPropertyIndex() {
return defaultPropertyIndex;
}
public int getDefaultEventIndex() {
return defaultEventIndex;
}
public java.awt.Image getIcon(int iconKind) {
switch ( iconKind ) {
case ICON_COLOR_16x16:
if ( iconNameC16 == null )
return null;
else {
if( iconColor16 == null )
iconColor16 = loadImage( iconNameC16 );
return iconColor16;
}
case ICON_COLOR_32x32:
if ( iconNameC32 == null )
return null;
else {
if( iconColor32 == null )
iconColor32 = loadImage( iconNameC32 );
return iconColor32;
}
case ICON_MONO_16x16:
if ( iconNameM16 == null )
return null;
else {
if( iconMono16 == null )
iconMono16 = loadImage( iconNameM16 );
return iconMono16;
}
case ICON_MONO_32x32:
if ( iconNameM32 == null )
return null;
else {
if( iconMono32 == null )
iconMono32 = loadImage( iconNameM32 );
return iconMono32;
}
default: return null;
}
}
public BeanInfo[] getAdditionalBeanInfo() {
Class superclass = JPanel_Image.class.getSuperclass();
try {
BeanInfo superBeanInfo = Introspector.getBeanInfo(superclass);
return new BeanInfo[] { superBeanInfo };
} catch (IntrospectionException ex) {
ex.printStackTrace();
return null;
}
}
}
// Now
import java.awt.;
import java.beans.;
import java.awt.Image;
public class EditorImage extends PropertyEditorSupport {
private EditorImageSelect editor;
public java.awt.Image img =null;
public String sfile = “”;
public EditorImage() {
}
public boolean supportsCustomEditor() {
return true;
}
public Component getCustomEditor() {
if (editor == null) {
editor = new EditorImageSelect(this);
}
return editor;
}
public void setValue(Object value) {
super.setValue(value);
img = (java.awt.Image)value;
}
public Object getValue() {
return img;
}
public String getJavaInitializationString() {
return “new javax.swing.ImageIcon(”"+ sfile + “”).getImage()";
}
public boolean isPaintable() {
return false;
}
}
//
/*
- EditorImageSelect.java
- Created on 17 de Abril de 2007, 22:17
*/
package com.menezes.javax.swing.bean;
import java.beans.*;
/**
-
@author root
/
import java.awt.;
import java.beans.;
import javax.swing.;
import java.io.;
import java.awt.event.;
import java.awt.Image;
import javax.swing.border.*;
public class EditorImageSelect extends JPanel {
private PropertyEditorSupport editor;
JFileChooser jFileChooser1 = new JFileChooser();
JPanel painelDireito = new JPanel();
JLabel desenhoPainelCentro = new JLabel();
JPanel painelCentro = new JPanel();
JButton selecioneBotao = new JButton();
JButton limpaBotao = new JButton();
javax.swing.ButtonGroup grupoBotoes = new ButtonGroup();
//
BorderLayout borderLayout1 = new BorderLayout();
int ret;
File arquivo;
Image imagem;
JScrollPane jScrollPane1 = new JScrollPane();
BorderLayout borderLayout2 = new BorderLayout();
JLabel jLabel1 = new JLabel();
JToggleButton jToggleButton1 = new JToggleButton();
JToggleButton jToggleButton2 = new JToggleButton();
JToggleButton jToggleButton3 = new JToggleButton();
FlowLayout flowLayout1 = new FlowLayout();
TitledBorder titledBorder1;
TitledBorder titledBorder2;
JToggleButton jToggleButton4 = new JToggleButton();
JToggleButton jToggleButton5 = new JToggleButton();
public EditorImageSelect() {
super();
initComponents();
}
public EditorImageSelect(PropertyEditorSupport editor) {
this();
this.editor = editor;
}
private void initComponents() {
grupoBotoes.add(jToggleButton1);
grupoBotoes.add(jToggleButton2);
grupoBotoes.add(jToggleButton3);
grupoBotoes.add(jToggleButton4);
grupoBotoes.add(jToggleButton5);
titledBorder1 = new TitledBorder("");
titledBorder2 = new TitledBorder("");
this.setLayout(borderLayout1);
jFileChooser1.setFileView(new ImageFileView() );
jFileChooser1.setFileFilter(new ImageFilter() );
jFileChooser1.setAccessory(new ImagePreview(jFileChooser1) );
selecioneBotao.setBorder(BorderFactory.createRaisedBevelBorder());
selecioneBotao.setPreferredSize(new Dimension(88, 27));
selecioneBotao.setText("Selecao");
selecioneBotao.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
selecioneBotao_actionPerformed(e);
}
});
limpaBotao.setBorder(BorderFactory.createRaisedBevelBorder());
limpaBotao.setPreferredSize(new Dimension(88, 27));
limpaBotao.setText("Limpa");
limpaBotao.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
limpaBotao_actionPerformed(e);
}
});
painelDireito.setBorder(BorderFactory.createLineBorder(Color.black));
painelDireito.setPreferredSize(new Dimension(90, 37));
painelDireito.setLayout(flowLayout1);
//
painelCentro.setBackground(Color.white);
painelCentro.setBorder(BorderFactory.createLineBorder(Color.black));
painelCentro.setPreferredSize(new Dimension(250, 200) );
painelCentro.setToolTipText("");
painelCentro.setLayout(borderLayout2);
//jLabel1.setText("jLabel1");
desenhoPainelCentro.setHorizontalAlignment(SwingConstants.CENTER);
jToggleButton1.setPreferredSize(new Dimension(88, 27));
jToggleButton1.setText("Metal");
jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jToggleButton1_actionPerformed(e);
}
});
jToggleButton2.setPreferredSize(new Dimension(88, 27));
jToggleButton2.setText("Solares");
jToggleButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jToggleButton2_actionPerformed(e);
}
});
jToggleButton3.setPreferredSize(new Dimension(88, 27));
jToggleButton3.setMinimumSize(new Dimension(60, 27));
jToggleButton3.setText("windows");
jToggleButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jToggleButton3_actionPerformed(e);
}
});
jToggleButton4.setPreferredSize(new Dimension(88, 27));
jToggleButton4.setText("Mac");
jToggleButton5.setPreferredSize(new Dimension(88, 27));
jToggleButton5.setText("Default");
painelCentro.add(desenhoPainelCentro, BorderLayout.CENTER );
this.add(painelDireito, BorderLayout.EAST);
painelDireito.add(selecioneBotao, null);
painelDireito.add(limpaBotao, null);
painelDireito.add(jToggleButton1, null);
painelDireito.add(jToggleButton2, null);
// Define Tamanho
painelDireito.add(jToggleButton3, null);
painelDireito.add(jToggleButton4, null);
painelDireito.add(jToggleButton5, null);
//
jScrollPane1.getViewport().add(desenhoPainelCentro, null);
painelCentro.add(jScrollPane1, BorderLayout.CENTER);
this.add(painelCentro, BorderLayout.CENTER);
try {
if ( ((EditorImage)editor).getValue() != null ) {
desenhoPainelCentro.setIcon( new ImageIcon( (java.awt.Image) ((EditorImagem)editor).getValue() ) );
}
} catch ( java.lang.NullPointerException nuloEx ) {}
}
/*
Look this Method
…
.
.
v
V
*/
void selecioneBotao_actionPerformed(ActionEvent e) {
ret = jFileChooser1.showOpenDialog(jFileChooser1);
if (ret == 0 ) { // ret == 0 se Botao Ok foi Pressionado
arquivo = jFileChooser1.getSelectedFile();
try {
imagem = getToolkit().getDefaultToolkit().getImage(arquivo.getCanonicalPath() );
desenhoPainelCentro.setIcon( new ImageIcon(imagem) );
((EditorImagem)editor).setValue( imagem ); // H E R E
((EditorImagem)editor).caminhoNomeArquivo = arquivo.getCanonicalPath();
editor.firePropertyChange();
}catch( java.io.IOException ioe ) {
ioe.printStackTrace();
}catch(java.lang.NullPointerException npe ) {}
}
}
void limpaBotao_actionPerformed(ActionEvent e) {
desenhoPainelCentro.setIcon(null);
}
void jToggleButton1_actionPerformed(ActionEvent e) {
AssumeLookAndFeel.setLookAndFeel(this,AssumeLookAndFeel.LOOK_METAL );
AssumeLookAndFeel.setLookAndFeel(jFileChooser1,AssumeLookAndFeel.LOOK_METAL );
}
void jToggleButton2_actionPerformed(ActionEvent e) {
AssumeLookAndFeel.setLookAndFeel(this,AssumeLookAndFeel.LOOK_MOTIF );
AssumeLookAndFeel.setLookAndFeel(jFileChooser1,AssumeLookAndFeel.LOOK_MOTIF );
}
void jToggleButton3_actionPerformed(ActionEvent e) {
AssumeLookAndFeel.setLookAndFeel(this,AssumeLookAndFeel.LOOK_WINDOWS );
AssumeLookAndFeel.setLookAndFeel(jFileChooser1,AssumeLookAndFeel.LOOK_WINDOWS );
}
}
class AssumeLookAndFeel {
static String lookAtual;
Component comp;
static final String LOOK_MAC = “com.sun.java.swing.plaf.mac.MacLookAndFeel”;
static final String LOOK_WINDOWS = “com.sun.java.swing.plaf.windows.WindowsLookAndFeel”;
static final String LOOK_METAL = “javax.swing.plaf.metal.MetalLookAndFeel”;
static final String LOOK_MOTIF = “com.sun.java.swing.plaf.motif.MotifLookAndFeel”;
static void setLookAndFeel( Component comp, String qual ) {
if ( qual.equals(LOOK_MAC) ) lookAtual = LOOK_MAC;
else if ( qual.equals(LOOK_WINDOWS) ) lookAtual = LOOK_WINDOWS;
else if ( qual.equals(LOOK_METAL) ) lookAtual = LOOK_METAL;
else if ( qual.equals(LOOK_MOTIF) ) lookAtual = LOOK_MOTIF;
try {
UIManager.setLookAndFeel(lookAtual);
SwingUtilities.updateComponentTreeUI(comp);
}
catch( java.lang.Exception ec2 ) {}
}
}