Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

129
Views
Setting a JComboBox editor using TableModelListener

I wanted to set my 2nd column as JComboBox editor using TableModelListener. Also wanted to change the Model of ComboBox in 2nd column based on selected ComboBox in 1st column. Here I implemented a Listener that's listen to 1st column.

private class TableScheduleListener implements TableModelListener
{
    //Listening for data changes.
    @Override
    public void tableChanged(TableModelEvent e) 
    {
        int row = e.getFirstRow();
        int column = e.getColumn();
    }

    if(column == 1)
    {
            for(int i = 0; i < createScheduleView.tblSchedule.getRowCount(); i++)
            {
                createScheduleView.tblSchedule.getCellEditor(i, 2);
            }

            int col = createScheduleView.tblSchedule.convertColumnIndexToModel(2);

            if(col == 2 && createScheduleView.tblSchedule.getRowCount() < 7)
            {
                DefaultComboBoxModel cbModel = new DefaultComboBoxModel(createScheduleModel.setData().toArray());
                createScheduleView.cbBreakStartTime.setModel(cbModel);
            }
     }
}

But I'm thinking how can I set as JComboBox as editor in my 2nd column. After settings it's model? Any help would appreciated.

UPDATE

I followed camickr tips from https://tips4java.wordpress.com/2009/06/28/combo-box-table-editor/ But wanted to override TableCellEditor from different class.

private class TableScheduleEditor extends JTable
{
    //  Determine editor to be used by row
    @Override
    public TableCellEditor getCellEditor(int row, int column)
    {
        int modelColumn = convertColumnIndexToModel(column);

        if(modelColumn == 2 && row < 7)
        {
            DefaultComboBoxModel model = new DefaultComboBoxModel(createScheduleModel.setData().toArray());
            createScheduleView.getCbBreakStartTime().setModel(model);
            return new DefaultCellEditor(createScheduleView.getCbBreakStartTime());
        }

        else
            return super.getCellEditor(row, column);
    };
}

I'm thinking how this class can register in my JTable? I tried to register this in my constructor this.createScheduleView.tblSchedule.setCellEditor(new TableScheduleEditor()); But it says cannot be converted to TableCellEditor because it extends JTable. Any trick to do this?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Also wanted to change the Model of ComboBox in 2nd column based on selected ComboBox in 1st column

Override the getCellEditor(...) method of the JTable to return the appropriate editor.

Here is a basic example to get you started:

import java.awt.*;
import java.util.List;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.event.*;
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 the above example the "model" changes based on the row being edited.

In your case you will need to modify the logic to return the editor based on the value in the first column.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!