如何在jTable中的某个单元格上获取jComboBox?

时间:2016-01-26 15:17:02

标签: java swing jtable jcombobox

我试图从jTable在某个单元格中创建一个jComboBox。如果在第4列的同一行中你有值#34; FN",你将在第5列上有一个带有3个选项的jComboBox(" SSAA-MM-JJ"," ; SSAA / MM / JJ"," SAAMMJJ"),但如果来自同一行的第4列上的单元格的值不是"则第5列中的所有其他单元格必须保持不变。 FN"

我做错了什么?

以下是我的尝试:

1  #include <functional>
2  #include <vector>
3  #include <iostream>
4  using namespace std;
5  void f1(int x, int y){}
6  void f2(int x, vector<string> v) {}
7  int main ()
8  {
9      f2(2, {{"hello", "it's", "me"}});
10     auto g1 = bind(f1, placeholders::_1, 3);
11     auto g2 = bind(f2, placeholders::_1, {{"hello", "it's", "me"}});
12     return 0;
13 }

电话:

In file included from test.cpp:1:0:
/usr/include/c++/5/functional: In instantiation of ‘struct std::_Bind_check_arity<void (*)(int, std::vector<std::__cxx11::basic_string<char> >)>’:
/usr/include/c++/5/functional:1439:12:   required from ‘struct std::_Bind_helper<false, void (&)(int, std::vector<std::__cxx11::basic_string<char> >)>’
/usr/include/c++/5/functional:1462:5:   required by substitution of ‘template<class _Func, class ... _BoundArgs> typename std::_Bind_helper<std::__is_socketlike<_Func>::value, _Func, _BoundArgs ...>::type std::bind(_Func&&, _BoundArgs&& ...) [with _Func = void (&)(int, std::vector<std::__cxx11::basic_string<char> >); _BoundArgs = {}]’
test.cpp:11:67:   required from here
/usr/include/c++/5/functional:1410:7: error: static assertion failed: Wrong number of arguments for function
       static_assert(sizeof...(_BoundArgs) == sizeof...(_Args),
       ^
test.cpp: In function ‘int main()’:
test.cpp:11:67: error: no matching function for call to ‘bind(void (&)(int, std::vector<std::__cxx11::basic_string<char> >), const std::_Placeholder<1>&, <brace-enclosed initializer list>)’
     auto g2 = bind(f2, placeholders::_1, {{"hello", "it's", "me"}});
                                                                   ^
In file included from test.cpp:1:0:
/usr/include/c++/5/functional:1462:5: note: candidate: typename std::_Bind_helper<std::__is_socketlike<_Func>::value, _Func, _BoundArgs ...>::type std::bind(_Func&&, _BoundArgs&& ...) [with _Func = void (&)(int, std::vector<std::__cxx11::basic_string<char> >); _BoundArgs = {}; typename std::_Bind_helper<std::__is_socketlike<_Func>::value, _Func, _BoundArgs ...>::type = std::_Bind<void (*())(int, std::vector<std::__cxx11::basic_string<char> >)>]
     bind(_Func&& __f, _BoundArgs&&... __args)
     ^
/usr/include/c++/5/functional:1462:5: note:   candidate expects 1 argument, 3 provided
/usr/include/c++/5/functional:1490:5: note: candidate: template<class _Result, class _Func, class ... _BoundArgs> typename std::_Bindres_helper<_Result, _Func, _BoundArgs>::type std::bind(_Func&&, _BoundArgs&& ...)
     bind(_Func&& __f, _BoundArgs&&... __args)
     ^
/usr/include/c++/5/functional:1490:5: note:   template argument deduction/substitution failed:
test.cpp:11:67: note:   couldn't deduce template parameter ‘_Result’
     auto g2 = bind(f2, placeholders::_1, {{"hello", "it's", "me"}});
                                                                   ^

输出: jComboBox来自第5列的每个单元格。

enter image description here

1 个答案:

答案 0 :(得分:1)

您的图像显示了单元格渲染器的输出,而不是单元格编辑器,因为任何时候都只能看到一个单元格编辑器。您不希望渲染器看起来像JComboBox,而是显示为文本,作为标签。这表明您的计划存在其他问题。

其他问题:

  • 上面的代码冒着NPE的风险,因为lastSelected在启动时可能为null。
  • 你为什么检查那行== 0?你是否只在第一行使用JComboBox编辑器?
  • 发布最小的示例程序,如果仍然卡住,

例如,我的:

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;

@SuppressWarnings("serial")
public class ComboEditorEg extends JPanel {
    private MyTableModel model = new MyTableModel();
    private JTable table = new JTable(model);

    public ComboEditorEg() {
        for (int i = 0; i < 10; i++) {
            String textA = i % 2 == 0 ? "SA" : "FN";
            String textB = i % 2 == 0 ? "A" : "B";

            Object[] row = new String[] { textA, textB };
            model.addRow(row);
        }

        table.getColumnModel().getColumn(1).setCellEditor(new MyCellEditor());

        setLayout(new BorderLayout());
        add(new JScrollPane(table));
    }

    private static void createAndShowGui() {
        ComboEditorEg mainPanel = new ComboEditorEg();

        JFrame frame = new JFrame("ComboEditorEg");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            createAndShowGui();
        });
    }
}

@SuppressWarnings("serial")
class MyTableModel extends DefaultTableModel {
    public static final String[] COL_NAMES = { "Foo 1", "Foo 2" };

    public MyTableModel() {
        super(COL_NAMES, 0);
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return String.class;
    }
}

@SuppressWarnings("serial")
class MyCellEditor extends AbstractCellEditor implements TableCellEditor {

    DefaultCellEditor other = new DefaultCellEditor(new JTextField());
    DefaultCellEditor checkbox = new DefaultCellEditor(new JComboBox<String>(new String[] { "abc",
            "def", "ghi" }));

    private DefaultCellEditor lastSelected = other; // so it's not null

    @Override
    public Object getCellEditorValue() {

        return lastSelected.getCellEditorValue();
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected,
            int row, int column) {
        if (table.getValueAt(row, column - 1).toString().contains("FN")) {
            lastSelected = checkbox;
            return checkbox.getTableCellEditorComponent(table, value, isSelected, row, column);
        }
        return other.getTableCellEditorComponent(table, value, isSelected, row, column);
    }

}
相关问题