繁体   English   中英

如何使用绑定类通过代码将JComboBox selectedItem绑定到Jtable?

[英]How to bind a JComboBox selectedItem to a Jtable by code using binding classes?

我使用以下代码成功地将JTextField绑定到JTable中的选定行:

public static final void BindTableToFields(JTable Table, Object[] Fields) {

    for (Object Field : Fields) {

        BeanProperty<JTable, Object> tableBeanProperty;

        String DesignCallID;
        String PropertyName;

        if (Field instanceof JTextField) {
            BeanProperty<JTextField, String> BindingFieldProperty;
            Binding<JTable, Object, JTextField, String> binding;
            JTextField jTextField = (JTextField) Field;
            DesignCallID = jTextField.getText();
            PropertyName = "selectedElement." + GetCorrespondingColumn(Table, DesignCallID);
            jTextField.setText("");
            System.out.println("property name =" + PropertyName.trim());

            tableBeanProperty = BeanProperty.create(PropertyName);
            BindingFieldProperty = BeanProperty.create("text");
            binding = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
                    Table,
                    tableBeanProperty,
                    jTextField,
                    BindingFieldProperty);
            Converter<Object, String> old = binding.getConverter();

            Converter<Object, String> converter = new Converter<Object, String>() {
                @Override
                public String convertForward(Object value) {
                    try {

                        if (value instanceof HashMap) {
                            HashMap hashMap = (HashMap) value;
                            System.out.println(hashMap);
                            // Object get = hashMap.get(GetCorrespondingColumn(Table, DesignCallID));
                            return (String) hashMap.get(GetCorrespondingColumn(Table, DesignCallID));

                        } else if (value instanceof String) {
                            return (String) value;
                        } else if (value instanceof Integer) {
                            Integer integer = (Integer) value;
                            return integer.toString();
                        } else if (value instanceof Float) {
                            Float float1 = (Float) value;
                            return float1.toString();
                        } else if (value instanceof Double) {
                            Double double1 = (Double) value;
                            return double1.toString();
                        } else if (value instanceof Date) {
                            Date date = (Date) value;
                            return date.toLocalDate().format(DateTimeFormatter.ISO_LOCAL_DATE);
                        }
                        MyLogger.Log_to_local(new Exception("Binding Class Not Handled" + value.getClass().getName()));
                        return "";

                    } catch (Exception e) {
                        e.printStackTrace();
                        MyLogger.Log_to_local(e);

                    }
                    return "impossible";
                }

                @Override
                public Object convertReverse(String value) {
                    try {
                        return value;

                    } catch (Exception e) {

                        e.printStackTrace();

                    }
                    return null;
                }

            };
            binding.setConverter(converter);
            binding.bind();
     } else 
     //
     //
     //    other component as JPasswordField .....

然后它来了部分是我尝试绑定JComboBox我尝试了这两个版本:

                    if (Field instanceof JComboBox) {
                    //<editor-fold>
                    System.out.println("binding ");
                    JComboBox jComboBox = (JComboBox) Field;

                    BeanProperty<JComboBox, String> BindingFieldProperty;
                    Binding<JTable, Object, JComboBox, String> binding;
                    DesignCallID = jComboBox.getName();
                    PropertyName = "selectedElement." + GetCorrespondingColumn(Table, DesignCallID);
                    System.out.println("property name =" + PropertyName.trim());

                    tableBeanProperty = BeanProperty.create(PropertyName);
                    BindingFieldProperty = BeanProperty.create("selectedItem");
                    binding = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
                            Table,
                            tableBeanProperty,
                            jComboBox,
                            BindingFieldProperty);
                    Converter<Object, String> old = binding.getConverter();

                    Converter<Object, String> converter = new Converter<Object, String>() {
                        @Override
                        public String convertForward(Object value) {
                            try {

                                if (value instanceof HashMap) {
                                    HashMap hashMap = (HashMap) value;
                                    System.out.println(hashMap);
                                    // Object get = hashMap.get(GetCorrespondingColumn(Table, DesignCallID));
                                    return (String) hashMap.get(GetCorrespondingColumn(Table, DesignCallID));

                                } else if (value instanceof String) {
                                    return (String) value;
                                } else if (value instanceof Integer) {
                                    Integer integer = (Integer) value;
                                    return integer.toString();
                                } else if (value instanceof Float) {
                                    Float float1 = (Float) value;
                                    return float1.toString();
                                } else if (value instanceof Double) {
                                    Double double1 = (Double) value;
                                    return double1.toString();
                                } else if (value instanceof Date) {
                                    Date date = (Date) value;
                                    return date.toLocalDate().format(DateTimeFormatter.ISO_LOCAL_DATE);
                                }
                                MyLogger.Log_to_local(new Exception("Binding Class Not Handled" + value.getClass().getName()));
                                return "";

                            } catch (Exception e) {
                                e.printStackTrace();
                                MyLogger.Log_to_local(e);

                            }
                            return "impossible";
                        }

                        @Override
                        public Object convertReverse(String value) {
                            try {
                                return value;

                            } catch (Exception e) {

                                e.printStackTrace();

                            }
                            return null;
                        }

                    };
                    binding.setConverter(converter);
                    binding.bind();
                       //</editor-fold>
                }

另一个版本是:

                    System.out.println("binding jcombobox");
                    JComboBox jComboBox = (JComboBox) Field;

                    BeanProperty<JComboBox, Integer> BindingFieldProperty;
                    Binding<JTable, Object, JComboBox, Integer> binding;
                    DesignCallID = jComboBox.getName();
                    PropertyName = "selectedElement." + GetCorrespondingColumn(Table, DesignCallID);
                    System.out.println("property name =" + PropertyName.trim());

                    tableBeanProperty = BeanProperty.create(PropertyName);
                    BindingFieldProperty = BeanProperty.create("selectedItem");
                    binding = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
                            Table,
                            tableBeanProperty,
                            jComboBox,
                            BindingFieldProperty);
                    Converter<Object, Integer> old = binding.getConverter();

                    Converter<Object, Integer> converter = new Converter<Object, Integer>() {
                        @Override
                        public Integer convertForward(Object value) {
                            try {

                                if (value instanceof HashMap) {
                                    HashMap hashMap = (HashMap) value;
                                    System.out.println(hashMap);
                                    // Object get = hashMap.get(GetCorrespondingColumn(Table, DesignCallID));
                                    return Integer.parseInt(""+hashMap.get(GetCorrespondingColumn(Table, DesignCallID)));

                                } else if (value instanceof String) {
                                    return Integer.parseInt(""+value);
                                } else if (value instanceof Integer) {
                                    Integer integer = (Integer) value;
                                    return integer;
                                } else if (value instanceof Float) {
                                    Float float1 = (Float) value;
                                    return  float1.intValue();
                                } else if (value instanceof Double) {
                                    Double double1 = (Double) value;
                                    return double1.intValue();
                                } else if (value instanceof Date) {
                                    Date date = (Date) value;
                                    MyLogger.Log_to_local(new Exception("Binding Class Not Handled" + value.getClass().getName()));
                                    return 1;
                                }
                                MyLogger.Log_to_local(new Exception("Binding Class Not Handled" + value.getClass().getName()));
                                return 1;

                            } catch (Exception e) {
                                e.printStackTrace();
                                MyLogger.Log_to_local(e);

                            }
                            return 1;
                        }

                        @Override
                        public Object convertReverse(Integer value) {
                            try {
                                return value;

                            } catch (Exception e) {

                                e.printStackTrace();

                            }
                            return null;
                        }

                    };
                    binding.setConverter(converter);
                    binding.bind();

我不知道我错过了,它不适用于JcomboBox! 那么请有任何错误或错过的逻辑告诉我,还有一个解决方案,通过代码将我的JComboBox绑定到我的JTable告诉我如何。 谢谢

首先,IMO,您提供的代码包含100多行代码,因此大多数人都不愿意回答这个问题。
回到你的问题,你的问题的可能答案是使用setCellEditor 。如下面的代码

//provided you have instatiate the table.
//then instantiate the `TableColumn` object to accept the column
TableColumn sportColumn = table.getColumnModel().getColumn(2);
...
//add the desired items to be selected by the user
JComboBox comboBox = new JComboBox();
comboBox.addItem("Snowboarding");
comboBox.addItem("Rowing");
comboBox.addItem("Chasing toddlers");
comboBox.addItem("Speed reading");
comboBox.addItem("Teaching high school");
comboBox.addItem("None");
//add the combo box into the column
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));

以下是参考: https//docs.oracle.com/javase/tutorial/uiswing/components/table.html#combobox

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM