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

Posted

技术标签:

【中文标题】如何使用绑定类通过代码将 JComboBox selectedItem 绑定到 Jtable?【英文标题】:How to bind a JComboBox selectedItem to a Jtable by code using binding classes? 【发布时间】:2017-03-21 08:42:05 【问题描述】:

我使用此代码成功地将 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,告诉我如何操作。 谢谢

【问题讨论】:

【参考方案1】:

首先,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

【讨论】:

问题不在于将表格列渲染为组合框,而在于将组合框内容的事件绑定到内容(列的值)

以上是关于如何使用绑定类通过代码将 JComboBox selectedItem 绑定到 Jtable?的主要内容,如果未能解决你的问题,请参考以下文章

如何将JComboBox添加到JTable单元格?

JComboBox 自动完成

如何将 JButton 放在 JComboBox 中

使用 WHERE 子句选择的数据加载 JCombobox

java中如何将数据显示到jcombobox上?

在 jComboBox 中显示数据