Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

Creating a CustomRenderer with JComboBox for enums

Thread view: 
malpropio - 15 Aug 2007 16:40 GMT
I was reading the post about the customrenderer using JCombobox to
represent an enum. I have a similar problem which is ,I need to build
a table that takes enums and display JCombobox so my customRenderer
wont be for one particular enum but for enums in general, do you have
any idea how that could be done ?
Manish Pandit - 15 Aug 2007 18:38 GMT
>  I was reading the post about the customrenderer using JCombobox to
> represent an enum. I have a similar problem which is ,I need to build
> a table that takes enums and display JCombobox so my customRenderer
> wont be for one particular enum but for enums in general, do you have
> any idea how that could be done ?

If you're refering to JDK 1.5 Enums, then you can use values() to get
an array of values held in that enum and populate the list.

Tutorial: http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html

-cheers,
Manish
malpropio - 15 Aug 2007 21:49 GMT
I was able to use the values() method to get the element of the enum
but now I have another problem, the ComBox does not drop down when I
click on it to select an element other than the default selected one
and I implement the getTableVellRendererComponent but the value I'm
getting are always null for some reason here is the renderer's code:

public class EnumRenderer<V extends Enum<V>> extends JComboBox
implements TableCellRenderer {

    Class<V> valueType;

    public EnumRenderer() {
        // TODO Auto-generated constructor stub
    }

    public EnumRenderer(ComboBoxModel arg0) {
        super(arg0);
        // TODO Auto-generated constructor stub
    }

    public EnumRenderer(Object[] arg0) {
        super(arg0);
        // TODO Auto-generated constructor stub
    }

    public EnumRenderer(Vector<?> arg0) {
        super(arg0);
        // TODO Auto-generated constructor stub
    }

    public EnumRenderer( Class valueType ){
        super(valueType.getEnumConstants());
        this.setEnabled(true);
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object
value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        // TODO Auto-generated method stub

        System.out.println(value);

        /*
        if(arg1 != null ){

            System.out.println(arg1);

            Class<V> valueType = ((Enum)arg1).getDeclaringClass();

            for(V v : valueType.getEnumConstants()){
                 addItem(v);
             }
         }
        */
        return this;
    }

}
malpropio - 16 Aug 2007 22:12 GMT
Found the problem I was using the wrong element. If you're trying to
use a combobox to change the value of a field you should use a custom
CellEditor extending a JComboBox instead of the CustomCellRender
because that will not let you edit/change the value of the cell via
the combobox.
Sebastian Millies - 20 Aug 2007 09:34 GMT
Am Thu, 16 Aug 2007 21:12:10 -0000 schrieb malpropio:

> Found the problem I was using the wrong element. If you're trying to
> use a combobox to change the value of a field you should use a custom
> CellEditor extending a JComboBox instead of the CustomCellRender
> because that will not let you edit/change the value of the cell via
> the combobox.

Hi, could you post your working example code? In any case, that would
help me a lot, not having much experience with Swing.
-- Thank you, Sebastian


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.