删除组合框作为jtable单元格的单元格编辑器

时间:2014-08-25 06:18:53

标签: java swing jtable jcombobox tablecelleditor

我已将组合框作为列的单元格编辑器。我想当我创建一个新行时,该列中的单元格不应该将组合框作为单元格编辑器,并且应该将JTextField保留为单元格编辑器。这是我到目前为止所做的。

 addRow(mainWindow.salesTable);
    final TableColumn items = mainWindow.salesTable.getColumnModel().getColumn(0);
    final JTextField tfield = new JTextField();
  DefaultCellEditor editorqty = new DefaultCellEditor(tfield);
  items.setCellEditor(editorqty);

   private static void addRow(JTable table) {
    DefaultTableModel model = (DefaultTableModel) table.getModel();
    Vector row = new Vector();
    row.add("");
    row.add("");
    row.add("");
    row.add("");
    row.add("");
    row.add("");
    row.add("");
    row.add("");
    model.addRow(row);
}

1 个答案:

答案 0 :(得分:1)

覆盖JTble的getCellEditor(...)方法是一种方法:

import java.awt.*;
import java.util.List;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;

public class TableComboBoxByRow extends JPanel
{
    List<String[]> editorData = new ArrayList<String[]>(3);

    public TableComboBoxByRow()
    {
        setLayout( new BorderLayout() );

        // Create the editorData to be used for each row

        editorData.add( new String[]{ "Red", "Blue", "Green" } );
        editorData.add( new String[]{ "Circle", "Square", "Triangle" } );
        editorData.add( new String[]{ "Apple", "Orange", "Banana" } );

        //  Create the table with default data

        Object[][] data =
        {
            {"Color", "Red"},
            {"Shape", "Square"},
            {"Fruit", "Banana"},
            {"Plain", "Text"}
        };
        String[] columnNames = {"Type","Value"};

        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        JTable table = new JTable(model)
        {
            //  Determine editor to be used by row
            public TableCellEditor getCellEditor(int row, int column)
            {
                int modelColumn = convertColumnIndexToModel( column );

                if (modelColumn == 1 && row < 3)
                {
                    JComboBox<String> comboBox1 = new JComboBox<String>( editorData.get(row));
                    return new DefaultCellEditor( comboBox1 );
                }
                else
                    return super.getCellEditor(row, column);
            }
        };

        JScrollPane scrollPane = new JScrollPane( table );
        add( scrollPane );
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("Table Combo Box by Row");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new TableComboBoxByRow() );
        frame.setSize(200, 200);
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}