简体   繁体   English

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

[英]Removing combobox as the cell editor of a jtable cell

I have made a combobox as cell editor of a column. 我做了一个组合框,作为一列的单元格编辑器。 I want when i create a new row the cell in that column should not have the combobox as the cell editor and should retain JTextField as the cell editor. 我想要当我创建新行时,该列中的单元格不应具有组合框作为单元格编辑器,而应保留JTextField作为单元格编辑器。 Here is what i have done so far. 这是我到目前为止所做的。

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

Overriding the getCellEditor(...) method of JTble is one approach: 重写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();
            }
        });
    }
}

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

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