Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: How to use ComboBox as PropertyEditor for Property of Java Bean Component?

  1. #1
    Join Date
    Aug 2007
    Posts
    45
    Rep Power
    0

    Default How to use ComboBox as PropertyEditor for Property of Java Bean Component?

    I created a Java Bean component named as "PnlForQuery" that has a property named "database":-

    /*Default value for this property is "ORACLE" but It can be: "MS-ACCESS", "SQL-SERVER", "MYSQL",....etc.*/
    String database = "ORACLE";

    How can I create a comboBox editor for this property to accept one of above string values for database so that I can use it using Beanbox provided by Netbeans or any other IDE ?
    How to use ComboBox as PropertyEditor for Property of Java Bean Component? (Swing / AWT / SWT forum at JavaRanch)

    If we need to use PropertyEditor class for this purpose then plese give me an example...

    I got suggestion about my problem from following thread:-

    There is a class PropertyEditorSupport present in the java.beans package. We need to extend it and override the getCustomEditor() method to return your own component (also I think the supportsCustomEditor() method must also be overridden). You should start from there.

    I created a class DatabaseEditor as suggested by Mr. Mannar...

    view plaincopy to clipboardprint?
    Note: Text content in the code blocks is automatically word-wrapped
    Java Code:
    package gajeshBeans;  
      
    import java.awt.Component;  
    import java.beans.*;  
    import javax.swing.JComboBox;  
      
    /** 
     * PropertyEditor class for java bean property: database 
     * A combobox with values: ORACLE/ACCESSSQL-SERVER/MY-SQL 
     * @author gajesh 
     */  
    public class DatabaseEditor extends PropertyEditorSupport {  
          
        public DatabaseEditor() {  
        }  
      
        @Override  
        public Component getCustomEditor() {  
    //        return super.getCustomEditor(); //To change body of generated methods, choose Tools | Templates.  
            JComboBox jcDatabase;  
            jcDatabase = new JComboBox(new String[]{"ORACLE","SQL-SERVER","ACCESS","MY-SQL"});  
            jcDatabase.setSelectedIndex(0);  
            return jcDatabase;  
        }  
      
        @Override  
        public boolean supportsCustomEditor() {  
    //        return super.supportsCustomEditor(); //To change body of generated methods, choose Tools | Templates.  
            return true;  
        }  
      
    }
    Kindly modify & test my code so that it can work...

    Thanks In Advance
    GAJESH
    Attached Files Attached Files

  2. #2
    Join Date
    Aug 2007
    Posts
    45
    Rep Power
    0

    Default How to use ComboBox as PropertyEditor for Property of Java Bean Component?

    I created a Java Bean component named as "PnlForQuery" that has a property named "database":-

    /*Default value for this property is "ORACLE" but It can be: "MS-ACCESS", "SQL-SERVER", "MYSQL",....etc.*/
    String database = "ORACLE";

    How can I create a comboBox editor for this property to accept one of above string values for database so that I can use it using Beanbox provided by Netbeans or any other IDE ?
    How to use ComboBox as PropertyEditor for Property of Java Bean Component? (Swing / AWT / SWT forum at JavaRanch)

    If we need to use PropertyEditor class for this purpose then plese give me an example...
    my_javaranch_post
    I got suggestion about my problem from following thread:-

    There is a class PropertyEditorSupport present in the java.beans package. We need to extend it and override the getCustomEditor() method to return your own component (also I think the supportsCustomEditor() method must also be overridden). You should start from there.

    I created a class DatabaseEditor as suggested by Mr. Mannar...

    Java Code:
    package gajeshBeans;  
      
    import java.awt.Component;  
    import java.beans.*;  
    import javax.swing.JComboBox;  
      
    /** 
     * PropertyEditor class for java bean property: database 
     * A combobox with values: ORACLE/ACCESSSQL-SERVER/MY-SQL 
     * @author gajesh 
     */  
    public class DatabaseEditor extends PropertyEditorSupport {  
          
        public DatabaseEditor() {  
        }  
      
        @Override  
        public Component getCustomEditor() {  
    //        return super.getCustomEditor(); //To change body of generated methods, choose Tools | Templates.  
            JComboBox jcDatabase;  
            jcDatabase = new JComboBox(new String[]{"ORACLE","SQL-SERVER","ACCESS","MY-SQL"});  
            jcDatabase.setSelectedIndex(0);  
            return jcDatabase;  
        }  
      
        @Override  
        public boolean supportsCustomEditor() {  
    //        return super.supportsCustomEditor(); //To change body of generated methods, choose Tools | Templates.  
            return true;  
        }  
      
    }
    Kindly modify & test my code so that it can work...

    Thanks In Advance
    GAJESH
    Last edited by Gajesh Tripathi; 05-28-2013 at 08:09 AM.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: How to use ComboBox as PropertyEditor for Property of Java Bean Component?

    I've merged two threads here. Please don't use the breowser's back button to edit your posts, as that creates multiple submissions.

    db
    Gajesh Tripathi likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Join Date
    Aug 2007
    Posts
    45
    Rep Power
    0

    Talking Re: How to use ComboBox as PropertyEditor for Property of Java Bean Component?

    Quote Originally Posted by DarrylBurke View Post
    I've merged two threads here. Please don't use the breowser's back button to edit your posts, as that creates multiple submissions.

    db
    Thanks db, I got your point & I'll take care about it in future.
    Now, I want to delete one post please tell me can I do it? If yes then how?

    Dear Friends,

    My question is very simple that I want to use ComboBox as a Property Editor to use this editor for Java Bean Component's property = "database" {To restrict user select one value from multiple choice through ComboBox) with a bean box like NetBeans IDE bean box,....etc.

    I tried it using class
    DatabaseEditor
    that extends
    PropertyChangeSupport
    class and then I register it using following code fragement:-
    //....
    Java Code:
    private static final int PROPERTY_database = 25;
    //....
    PropertyDescriptor[] properties = new PropertyDescriptor[113];
    //....
    properties[PROPERTY_database] = new PropertyDescriptor ( "database", gajeshBeans.PnlForQuery.class, "getDatabase", "setDatabase" ); // NOI18N
    //....
    properties[PROPERTY_database].setPropertyEditorClass ( DatabaseEditor.class );
    But still NetBeans IDE is not showing Combo Box for this(database) property..

    I provide all required classes in attachment, you can compile & run it after few changes... To check my code, you also need any Java IDE like NetBeans OR Java Bean Box.

    If any one have example for using ComboBox as a Property Editor then please help me by sending it to me so that I'll try.

    Thanks In Advance
    GAJESH

  5. #5
    Join Date
    Aug 2007
    Posts
    45
    Rep Power
    0

    Smile [SOLVED] How to use ComboBox as PropertyEditor for Property of Java Bean Component?

    When I use this property DatabaseEditor.class then comboBox is showing but when changing value from combo then following error is showing:-
    java.lang.IllegalArgumentException: argument type mismatch
    Please check my DatabaseEditor class & tell me why this error is occurred if you know about that.

    Any ways, my main problem is solved & its my duty to submit solution here for closing this Thread.
    Modification in PnlForQuery.java
    Java Code:
    private int database;
    public static final int ACCESS = 1;
    public static final int MYSQL = 2;
    public static final int ORACLE = 3;
    public static final int SQLSERVER = 3;
    Modification in PnlForQueryBeanInfo.java
    Java Code:
    //First, I remove following line from source code (We dont need to use DtabaseEditor for ComboBox):-
    //properties[PROPERTY_database].setPropertyEditorClass ( DatabaseEditor.class );
    //Second, I add following code lines to source code for adding ComboBox values:-
    properties[PROPERTY_database].setValue("enumerationValues",new Object[]{
                "ACCESS", new Integer(PnlForQuery.ACCESS), "PnlForQuery.ACCESS",
                "MY-SQL", new Integer(PnlForQuery.MYSQL), "PnlForQuery.MYSQL",
                "ORACLE", new Integer(PnlForQuery.MYSQL), "PnlForQuery.ORACLE",
                "SQL-SERVER", new Integer(PnlForQuery.SQLSERVER), "PnlForQuery.SQLSERVER"});
    and its successfully working.

    Advantage of This Thread:-How to use combo box as a Property Editor? and conclusion is that we do not require to extend PropertyEditorSupport for selecting one value from multiple choice. Instead we set the multiple choice values to enumerationValues attribute of PropertyDescriptor's object.

    Thanks for viewing this Thread...
    GAJESH
    Last edited by Gajesh Tripathi; 05-29-2013 at 05:49 PM.

Similar Threads

  1. How to set dynamically bean property value ?
    By fanjavaid in forum Spring
    Replies: 1
    Last Post: 07-11-2013, 04:49 AM
  2. Replies: 0
    Last Post: 03-17-2011, 12:07 PM
  3. How to set the property of Bean by constructor injection
    By Java Tip in forum Spring Framework
    Replies: 1
    Last Post: 05-05-2008, 09:01 AM
  4. property of a java bean
    By suloverboy in forum New To Java
    Replies: 3
    Last Post: 12-14-2007, 09:31 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •