Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8083883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:37:05+00:00 2026-06-05T17:37:05+00:00

In the code below, for various rows of same table, I am trying to

  • 0

In the code below, for various rows of same table, I am trying to set an Editable comboBox as editor for first row ( so that the user can select from the available choices or type its own), a filechooser for second row and the default textFiled for the rest of rows.

The Problem: and steps to reproduce it:

1- Run the code,
2- click on second row and choose a folder (the row turns yellow)
3- now click on first row to select the type of movie (just click , no need to type anything or to choose)
4- now make another click back on second row(Folder selection)

you will see the contents of this row will be copied to first row?!

I know there are many things that I did not do right, perhaps handling swings which are not thread safe, handling references and so on. I was wondering if you guys can help me to fix this bug and turn this code to something solid.

Program output before user makes selections

after following steps above:

enter image description here

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import javax.swing.table.TableModel;

public class CCellEditor extends DefaultCellEditor {

    private JTable m_Table = null;

    public CCellEditor(JFrame parentFrame, JTable table) { 
        super(new JTextField());
        super.setClickCountToStart(1);
        m_Table = table;
    }
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, final int row, int column){

        if(row==0) // comBoBox for First row
        {
            Object[] objectArray = {"3D","2D"};
            JComboBox comboBox = new JComboBox(objectArray);
            comboBox.setEditable(true);
            comboBox.setSelectedItem(value);

            ItemListener itemListener = new ItemListener() {
                public void itemStateChanged(ItemEvent e) {
                    if(e.getStateChange() == ItemEvent.SELECTED) {
                        if(null != m_Table.getCellEditor()){ 
                            m_Table.getCellEditor().stopCellEditing();
                        }

                        m_Table.setValueAt(e.getItem(), row, 1);
                    }
                }
            };
            comboBox.addItemListener(itemListener);

            PopupMenuListener popMenuEvent = new PopupMenuListener() {

                public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                }

                public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
                    String sValue = (String)m_Table.getValueAt(row, 1);
                    if(null != m_Table.getCellEditor()){ 
                        m_Table.getCellEditor().stopCellEditing();
                    }
                    m_Table.setValueAt(sValue, row, 1);
                }

                public void popupMenuCanceled(PopupMenuEvent e) {   
                }

            };
            comboBox.addPopupMenuListener(popMenuEvent);

            return comboBox;

        }

        else  if(row==1)  // fileChooser for Second row
        {
            JFileChooser fileChooser;
            fileChooser = new JFileChooser("c:\\");
            fileChooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY );
            fileChooser.setVisible(true);
            int returnVal = fileChooser.showOpenDialog(null);

            JTextField textField = (JTextField)super.getTableCellEditorComponent(table, value, isSelected, row, column);
            textField.setBackground(Color.yellow);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File m_fFirmware= fileChooser.getSelectedFile();

                textField.setText(m_fFirmware.getPath());
                return textField;

            }else
            {
                return textField; 

            }
        }
                                // for any other rows
        JTextField textField = (JTextField)super.getTableCellEditorComponent(table, value, isSelected, row, column);
        return textField;

    }

    public static void main(String[] a) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        String[] columnTitles = { "Name", "Value"};
        Object[][] dataEntries = {
                { "Movie Type:", "3D" }, {"Folder:","C:"}, {"# of Movies requested:","5"}};
        TableModel model = new EditableTableModel(columnTitles, dataEntries);
        JTable table = new JTable(model);
        table.createDefaultColumnsFromModel();
        table.setDefaultEditor(Object.class, new CCellEditor(frame, table));
        frame.add(new JScrollPane(table));
        frame.setSize(300, 200);
        frame.setVisible(true);
    }

}

and here is the EditableTableModel class:

import javax.swing.table.AbstractTableModel;


class EditableTableModel extends AbstractTableModel {
  String[] columnTitles;

  Object[][] dataEntries;

  int rowCount;

  public EditableTableModel(String[] columnTitles, Object[][] dataEntries) {
    this.columnTitles = columnTitles;
    this.dataEntries = dataEntries;
  }

  public int getRowCount() {
    return dataEntries.length;
  }

  public int getColumnCount() {
    return columnTitles.length;
  }

  public Object getValueAt(int row, int column) {
    return dataEntries[row][column];
  }

  public String getColumnName(int column) {
    return columnTitles[column];
  }

  public Class getColumnClass(int column) {
    return getValueAt(0, column).getClass();
  }

  public boolean isCellEditable(int row, int column) {
    return true;
  }

  public void setValueAt(Object value, int row, int column) {
    dataEntries[row][column] = value;
  }



}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T17:37:07+00:00Added an answer on June 5, 2026 at 5:37 pm

    I must admit it took me a while but I found the issue. You have not overridden the DefaultCellEditor.getCellEditorValue() function, which is important for your implementation. This function is called when when the cell being edited loses focus or whatever else causes editing to be complete. Anyway, in your case, it just gets the final value from the last default TableCellEditor (from DefaultCellEditor). This is why the first row keeps getting replaced with the last edited cell value.

    Here is a way to fix your problem:

    1. Create a global reference to the comboBox.
    2. Override getCellEditorValue() and return comboBox.getSelectedItem() if the last edited item was the second row. Otherwise, just return super.getCellEditorValue().

    This will fix your current issue but your code could use a lot of improvement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the code below that I have found. I am trying to learn
Code below is working well as long as I have class ClassSameAssembly in same
The code below working for only first div. I can't display other divs when
The code below removes www., etc. from the beginning of websites that are entered
The code below allows the user to enter in a phrase in plain English,
Is there any way to maintain the same functionality in the code below, but
I've manage to put together the below code through various examples, which seems to
So, essentially, we're writing a service-level application that can alter attributes of various user-level
The code below works for the class that I hard coded XCCustomers in my
I inherited some code that has two non-UI threads that update various WinForm controls.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.