简体   繁体   English

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

[英]How to get jComboBox on certain cell in jTable?

I'm trying to create a jComboBox in a certain cell from a jTable. 我正在尝试从jTable的某个单元格中创建一个jComboBox。 If on the same line in column 4 you have the value "FN", you will have on the column 5 a jComboBox with 3 options ("SSAA-MM-JJ", "SSAA/MM/JJ", "SAAMMJJ"), but all the other cells on column 5 must remain untouched if the value from the cell on column 4 on the same row is not "FN". 如果在第4列的同一行上具有值“ FN”,则在第5列上将具有一个jComboBox,其中包含3个选项(“ SSAA-MM-JJ”,“ SSAA / MM / JJ”,“ SAAMMJJ”),但是,如果同一行第4列的单元格的值不是“ FN”,则第5列的所有其他单元格必须保持不变。

What do I do wrong? 我做错了什么?

Here is what i've tried: 这是我尝试过的:

package rdjcsv;

import java.awt.Component;
import javax.swing.AbstractCellEditor;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.TableCellEditor;

/**
*
* @author acucu
 */
class MyCellEditor extends AbstractCellEditor implements TableCellEditor {

DefaultCellEditor other = new DefaultCellEditor(new JTextField());
DefaultCellEditor checkbox = new DefaultCellEditor(new JComboBox(new Object[] {"abc"}));

private DefaultCellEditor lastSelected;

@Override
public Object getCellEditorValue() {

    return lastSelected.getCellEditorValue();
}

@Override
public Component getTableCellEditorComponent(JTable table,
        Object value, boolean isSelected, int row, int column) {
    if(column == 4 && table.getValueAt(row, column-1).toString().contains("FN")){


    if(row == 0) {
        lastSelected = checkbox;
        return checkbox.getTableCellEditorComponent(table, value, isSelected, row, column);
    }
    lastSelected = other;
    return other.getTableCellEditorComponent(table, value, isSelected, row, column);
}
   return other.getTableCellEditorComponent(table, value, isSelected, row, column);
}

}

And the call: 并致电:

String[] values = new String[] {"SSAA-MM-JJ", "SSAA/MM/JJ", "SAAMMJJ"};
  TableColumn col = jTable1.getColumnModel().getColumn(4);
col.setCellEditor(new MyComboBoxEditor(values));
col.setCellRenderer(new MyComboBoxRenderer(values));

The output: jComboBox es on every cell from the 5th column. 输出:jComboBox es在第5列的每个单元格上。

在此处输入图片说明

Your image is showing the output from the cell renderer, not the cell editor, since only one cell editor should be visible at any time. 您的图像显示的是单元格渲染器(而不是单元格编辑器)的输出,因为在任何时候都只能看到一个单元格编辑器。 You don't want the renderer to look like a JComboBox but rather to display as text, as a label. 您不希望渲染器看起来像JComboBox,而是显示为文本和标签。 This suggests other problems with your program. 这表明您的程序存在其他问题。

Other issues: 其他事宜:

  • Your code above risks a NPE since lastSelected can be null when it starts out. 上面的代码有NPE的风险,因为lastSelected开始时可以为null。
  • Why are you checking that row == 0? 为什么要检查该行== 0? Do you to use the JComboBox editor for the first row only? 您是否仅将JComboBox编辑器用于第一行?
  • Post your minimal example program if still stuck, 如果您仍然遇到问题,请发布您的最小示例程序,

for example, mine: 例如,我的:

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);
    }

}

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

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