【问题标题】:Dynamic jcombobox items inside jtablejtable中的动态jcombobox项目
【发布时间】:2013-02-22 16:55:38
【问题描述】:

我正在尝试创建一个每行有两个组合框的 Jtable。我检查了这方面的教程,发现我可以在组合框中添加静态数据。但是我怎么可以将动态数据加载到组合框中。

甚至,每当用户从行中选择组合框 1 时,都会基于此更新组合框 2。

有人可以帮我解决这个问题吗?

如果我从组合框中输入removeAllItems(),那么组合框 2 将更新,但我无法添加新条目。

提前致谢。

【问题讨论】:

    标签: java swing jtable dynamic-data jcombobox


    【解决方案1】:

    表格有两列都呈现为JComboBox. 现在,Column-2 项目的选择取决于 Column-1 的选择。

    import java.awt.Component;
    import java.awt.EventQueue;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.swing.DefaultCellEditor;
    import javax.swing.DefaultComboBoxModel;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    
    
    public class ComboBoxExample {
    
        private void createUI() {
    
            JFrame frame = new JFrame();
    
            Object[] columNames = {"Combo-1", "Combo-2"};
            Object[][] data = {{"", ""}, {"", ""}, {"", ""}, {"", ""}};
    
            JTable table = new JTable(data, columNames);
    
            table.getColumnModel().getColumn(0).setCellEditor(new CustomComboBoxEditor());
            table.getColumnModel().getColumn(1).setCellEditor(new CustomComboBoxEditor());
    
            frame.add(new JScrollPane(table));
            frame.setTitle("Column -2 based on Column - 1 ComboBox Selection.");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            Runnable r = new Runnable() {
    
                @Override
                public void run() {
                    new ComboBoxExample().createUI();
                }
            };
    
            EventQueue.invokeLater(r);
        }
    
    }
    
    class CustomComboBoxEditor extends DefaultCellEditor {
    
        // Declare a model that is used for adding the elements to the `ComboBox`
        private DefaultComboBoxModel model;
    
        private List<String> obtainedList;
    
        public CustomComboBoxEditor() {
            super(new JComboBox());
            this.model = (DefaultComboBoxModel)((JComboBox)getComponent()).getModel();
            obtainedList = new ArrayList<String>();
    
            obtainedList.add("One");
            obtainedList.add("Two");
            obtainedList.add("Three");
            obtainedList.add("Four");
            obtainedList.add("Five");
        }
    
        @Override
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
    
    
           if(column == 0) {
               model.removeAllElements();
               for(int i = 0; i < obtainedList.size(); i++) {
                   model.addElement(obtainedList.get(i));
                } 
            } else {
    
                 model.removeAllElements();
                 String selectedItem = (String) table.getValueAt(row, 0);
                 for(int i = 0; i < obtainedList.size(); i++) {
                        if(!selectedItem.equals(obtainedList.get(i)))
                        model.addElement(obtainedList.get(i));
                 } 
             } // Close else
    
            return super.getTableCellEditorComponent(table, value, isSelected, row, column);
         }
        }
    

    【讨论】:

    • 车,非常感谢您的帮助 :) 对我真的很有帮助 :)
    【解决方案2】:

    试试这样,你可以在这个例子中改变 DATA,然后重新绘制并做单元格的渲染器:

     public void example(){  
    
          TableColumn tmpColum =table.getColumnModel().getColumn(1);
          String[] DATA = { "Data 1", "Data 2", "Data 3", "Data 4" };
          JComboBox comboBox = new JComboBox(DATA);
    
          DefaultCellEditor defaultCellEditor=new DefaultCellEditor(comboBox);
          tmpColum.setCellEditor(defaultCellEditor);
          tmpColum.setCellRenderer(new CheckBoxCellRenderer(comboBox));
          table.repaint();
       }
    
    
    /**
       Custom class for adding elements in the JComboBox.
    */
    class CheckBoxCellRenderer implements TableCellRenderer {
            JComboBox combo;
            public CheckBoxCellRenderer(JComboBox comboBox) {
                this.combo = new JComboBox();
                for (int i=0; i<comboBox.getItemCount(); i++){
                    combo.addItem(comboBox.getItemAt(i));
                }
            }
            public Component getTableCellRendererComponent(JTable jtable, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                combo.setSelectedItem(value);
                return combo;
            }
        }
    

    【讨论】:

      【解决方案3】:

      removeAllItems() 调用后,将组合框中的项目添加为

        combobox.addItem("one");
        combobox.addItem("two");
      

      【讨论】:

        猜你喜欢
        • 2013-02-24
        • 1970-01-01
        • 2011-11-15
        • 2021-06-24
        • 1970-01-01
        • 1970-01-01
        • 2013-08-06
        • 1970-01-01
        相关资源
        最近更新 更多