简体   繁体   English

在jtable中创建组合框

[英]creating comboboxes inside jtable

My aim is to provide an interface like a matrix, each cell in matrix will have 2 values and user will select best among the two. 我的目标是提供一个像矩阵一样的界面,矩阵中的每个单元都有2个值,用户将在这两个值之间进行选择。

So i thought of going for jTable and combo boxes, the problem is in my matrix the value of each cell in a column is different. 所以我想到了jTable和组合框,问题是在我的矩阵中,列中每个单元格的值都不同。 But the following code that adds the combo box to whole column of the table and if i change the combo box value, it changes for the whole table. 但是下面的代码将组合框添加到表的整个列中,并且如果我更改组合框的值,它将更改整个表。 How to insert combo boxes into the table such that each one has different values 如何将组合框插入表中,以使每个框具有不同的值

javax.swing.JComboBox k = new javax.swing.JComboBox();
        k.addItem("1");
        k.addItem("2");
        k.addItem("3");
        k.setEnabled(true);
        k.setVisible(true);
this.jTable1.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(k));

I also tried with DefaultTableModel 我也尝试了DefaultTableModel

code is 代码是

  DefaultTableModel t =new javax.swing.table.DefaultTableModel();
    t.setColumnCount(10);
    t.setRowCount(10);
    t.setValueAt(k, 0, 0);
 jTable1.setModel(t);

but i get the output in the gui as 但我在gui中得到的输出为

javax.swing.JComboBox[,0,0,0x0,invalid,layout=javax.swing.plaf.metal.MetalComboBoxUI$MetalComboBoxLayoutManager,alignmentX=0.0,alignmentY=0.0,border=,flags=16777544,maximumSize=,minimumSize=,preferredSize=,isEditable=false,lightWeightPopupEnabled=true,maximumRowCount=8,selectedItemReminder=1]

I tried typecasting "k" as JComboBox and JComponent in setValueAt method, which didn't work 我尝试在setValueAt方法中将“ k”类型转换为JComboBox和JComponent,但没有用

Someone please help 有人请帮忙

Override the getCellEditor(....) method. 重写getCellEditor(....)方法。 For example: How to add unique JComboBoxes to a column in a JTable (Java) 例如: 如何向JTable(Java)中的列添加唯一的JComboBoxes

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

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