Amaan
Amaan

Reputation: 9

How to add JComboBox on JTable cell on MouseClick and based on other cells values from the Same JTable

JTable:

https://i.sstatic.net/LDorA.jpg

I have created an JTable in NetBeans in which i am bring values from database in certain columns like as shown in image i am bringing values for TESTNAME,UNITS,SPECIFICRANGE columns but the second column OBSERVED VALUE i have kept empty for user input,the user input is such that whenever the user clicks on the cell in front of Color he should get an JComboBox in the second columns cell i mean the cell in front of the Color on MouseEvent and for other cells i am using editCellAt() In Order to accompalish this i have written the below code and when i click on the cell in front of color i am getting JComboBox also when i click on other cells i am getting the JComboBox but i need to get the editCellAt() functionality

I think DefaultCellEditor is getting fixed for whole column but i need it only for particular cell on MouseClick

if(table.getValueAt(table.getSelectedRow(),0).toString().equals("Color"))
{
   TableColumn colorColumn = table.getColumnModel().getColumn(1);
   JComboBox comboBox = new JComboBox();
   comboBox.addItem("Red");
   comboBox.addItem("Greyish");
   comboBox.addItem("Yellow");
   colorColumn.setCellEditor(new DefaultCellEditor(comboBox));
}               
else
{
   table.editCellAt(table.getSelectedRow(), 1);
}

Upvotes: 0

Views: 666

Answers (1)

camickr
camickr

Reputation: 324137

Here is an example that shows how to dynamically return a custom editor:

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

In your case you need to modify the getCellEditor(...) method to return then combo box based in the data in column 0 of the TableModel, otherwise return the default editor. You may also need to override to editCellAt(...) method to only make the cell editable based on the data in column 0.

Upvotes: 1

Related Questions