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 8342745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:51:28+00:00 2026-06-09T05:51:28+00:00

Hi I’m using a table which two columns namely instrument and method. On click

  • 0

Hi I’m using a table which two columns namely instrument and method. On click of a button, I’ll add a row with the combo box as an editor in each cell. Also, those combo box wil have the actionlistener. Like when I’m selecting instrument, method combo box list should be changed. I’m using only two combo box and I’m instantiating it every time when I add a row. My problem is whenever I add a new row, the existing rows combos are getting reloaded with the latest value. That means, the combo boxes are unique even though I’m instantiating it differently. What is the way to create the combo box dynamically which should have the values of its own. Posting my code below for your reference.

addItem.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {
            comboInstrument = new CeNComboBox();
            comboMethod = new CeNComboBox();
            TableColumn instrumentColumn = table.getColumn("Instrument Used");
            TableColumn methodColumn = table.getColumn("Method Title");
            comboInstrument.removeAllItems();
            listInstruments = analyticalUtil.getInstruments(listAnalysisSummaryPrefs);
            iterateInstruments = listInstruments.iterator();
            while(iterateInstruments.hasNext()){
                comboInstrument.addItem(iterateInstruments.next());
            }
            dtm.addRow(new Object[]{" "," "});
            comboInstrument.setEditable(true);
            instrumentColumn.setCellEditor(new MyComboBoxEditor(comboInstrument));
            instrumentColumn.setCellRenderer(new MyComboBoxRenderer());
            comboMethod.setEditable(true);
            methodColumn.setCellEditor(new MyComboBoxEditor(comboMethod));
            methodColumn.setCellRenderer(new MyComboBoxRenderer());

            comboInstrument.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent argEvent){
                    comboInstrumentActionPerformed();
                }
            });
            comboMethod.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent argEvent){
                    comboMethodActionPerformed();
                }
            });
        }
    });

MyComboBoxEditor.java

public class MyComboBoxEditor extends DefaultCellEditor {



    public MyComboBoxEditor(JComboBox combobox) {
        super(combobox);
    }
}

I’m very new to Swing. Please help me out.

Adding some sample code with hardcoded values. If you run this you will understand my problem. Test in this way.
1. Select a value from first column, first row and select value from another column.
2. Do the same in second row and now check the second column of first row. All the values will be reloaded based on the selection made for the second row.
This is the problem I’m facing. The below code copied and edited from the following link
JComboBox Action listener

private static final long serialVersionUID = 1L;
        private JComboBox mainComboBox;
        private JComboBox subComboBox;
        private Hashtable<Object, Object> subItems = new Hashtable<Object, Object>();

        public Testing() {
            String[] items = {"","Select Item", "Color", "Shape", "Fruit", "Size"};
            mainComboBox = new JComboBox(items);
            mainComboBox.addActionListener(this);
            mainComboBox.addItemListener(this);
            mainComboBox.setEditable(true);
            //prevent action events from being fired when the up/down arrow keys are used
            //mainComboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
//          getContentPane().add(mainComboBox, BorderLayout.WEST);
            subComboBox = new JComboBox();//  Create sub combo box with multiple models
            subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4
            subComboBox.addItemListener(this);
//          getContentPane().add(subComboBox, BorderLayout.CENTER);
            String[] subItems1 = {"Select Color", "Red", "Blue", "Green"};
            subItems.put(items[1], subItems1);
            String[] subItems2 = {"Select Shape", "Circle", "Square", "Triangle"};
            subItems.put(items[2], subItems2);
            String[] subItems3 = {"Select Fruit", "Apple", "Orange", "Banana"};
            subItems.put(items[3], subItems3);
            String[] subItems4 = {"Select Size", "Big", "Middle", "Small"};
            subItems.put(items[4], subItems4);
            DefaultTableModel model = new DefaultTableModel(new String[]{"Instrument Used","Method Title"},0);
            JTable table = new JTable(model);
            table.getColumn("Instrument Used").setCellEditor(new MyComboBoxEditor(mainComboBox));
            table.getColumn("Instrument Used").setCellRenderer(new MyComboBoxRenderer());
            table.getColumn("Method Title").setCellEditor(new MyComboBoxEditor(subComboBox));
            table.getColumn("Method Title").setCellRenderer(new MyComboBoxRenderer());
            model.addRow(new String[]{""});
            model.addRow(new String[]{""});
            getContentPane().add(table, BorderLayout.CENTER);
        }

        public void actionPerformed(ActionEvent e) {
            String item = (String) mainComboBox.getSelectedItem();
            JOptionPane.showMessageDialog(null, "Action Performed "+item);
            Object o = subItems.get(item);
            if (o == null) {
                subComboBox.setModel(new DefaultComboBoxModel());
            } else {
                subComboBox.setModel(new DefaultComboBoxModel((String[]) o));
            }
        }

        public void itemStateChanged(ItemEvent e) {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                if (e.getSource() == mainComboBox) {
                    if (mainComboBox.getSelectedIndex() != 0) {
                        FirstDialog firstDialog = new FirstDialog(Testing.this,
                                mainComboBox.getSelectedItem().toString(), "Please wait,  Searching for ..... ");
                    }
                } 
            }
        }

        private class FirstDialog extends JDialog {

            private static final long serialVersionUID = 1L;

            FirstDialog(final Frame parent, String winTitle, String msgString) {
                super(parent, winTitle);
                //setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
                JLabel myLabel = new JLabel(msgString);
                JButton bNext = new JButton("Stop Processes");
                add(myLabel, BorderLayout.CENTER);
                add(bNext, BorderLayout.SOUTH);
                bNext.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent evt) {
                        setVisible(false);
                    }
                });
                javax.swing.Timer t = new javax.swing.Timer(1000, new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        setVisible(false);
                    }
                });
                t.setRepeats(false);
                t.start();
                setLocationRelativeTo(parent);
                setSize(new Dimension(400, 100));
                setVisible(true);
            }
        }

        public static void main(String[] args) {
            JFrame frame = new Testing();
            frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
  • 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-09T05:51:29+00:00Added an answer on June 9, 2026 at 5:51 am

    Thank you for new code. It’s very helpful.

    I play with your examlpe and rewrite it to work with editor properly.

    Pay attention to following parts:

    1. Testing. I remove subComboBox, you don’t need it here. I remove actionListener for mainComboBox.
    2. MyComBobBoxEditor. I implement method getTableCellEditorComponent(). This is a main method in editor. In this method I check which instrument is selected in current row and prepare editor comboBox for the particular instrument.

    This example not includes editable comboboxes yet. But I hope it will be helpful for you.

    public class Testing extends JFrame implements ItemListener{
        private static final long serialVersionUID = 1L;
            private JComboBox mainComboBox;
            private Hashtable<Object, Object> subItems = new Hashtable<Object, Object>();
    
            public Testing() {
                String[] items = {"","Select Item", "Color", "Shape", "Fruit", "Size"};
                mainComboBox = new JComboBox(items);
                mainComboBox.addItemListener(this);
                mainComboBox.setEditable(true);
                String[] subItems1 = {"Select Color", "Red", "Blue", "Green"};
                subItems.put(items[2], subItems1);
                String[] subItems2 = {"Select Shape", "Circle", "Square", "Triangle"};
                subItems.put(items[3], subItems2);
                String[] subItems3 = {"Select Fruit", "Apple", "Orange", "Banana"};
                subItems.put(items[4], subItems3);
                String[] subItems4 = {"Select Size", "Big", "Middle", "Small"};
                subItems.put(items[5], subItems4);
                DefaultTableModel model = new DefaultTableModel(new String[]{"Instrument Used","Method Title"},0);
                JTable table = new JTable(model);
                table.getColumn("Instrument Used").setCellEditor(new DefaultCellEditor(mainComboBox));
                //table.getColumn("Instrument Used").setCellRenderer(new MyComboBoxRenderer());
                table.getColumn("Method Title").setCellEditor(new MyComboBoxEditor());
                //table.getColumn("Method Title").setCellRenderer(new MyComboBoxRenderer());
                model.addRow(new String[]{""});
                model.addRow(new String[]{""});
                getContentPane().add(table, BorderLayout.CENTER);
            }
    
           public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    if (e.getSource() == mainComboBox) {
                        if (mainComboBox.getSelectedIndex() != 0) {
                            FirstDialog firstDialog = new FirstDialog(Testing.this,
                                    mainComboBox.getSelectedItem().toString(), "Please wait,  Searching for ..... ");
                        }
                    } 
                }
            }
    
            private class FirstDialog extends JDialog {
    
                private static final long serialVersionUID = 1L;
    
                FirstDialog(final Frame parent, String winTitle, String msgString) {
                    super(parent, winTitle);
                    //setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
                    JLabel myLabel = new JLabel(msgString);
                    JButton bNext = new JButton("Stop Processes");
                    add(myLabel, BorderLayout.CENTER);
                    add(bNext, BorderLayout.SOUTH);
                    bNext.addActionListener(new ActionListener() {
    
                        public void actionPerformed(ActionEvent evt) {
                            setVisible(false);
                        }
                    });
                    javax.swing.Timer t = new javax.swing.Timer(1000, new ActionListener() {
    
                        public void actionPerformed(ActionEvent e) {
                            setVisible(false);
                        }
                    });
                    t.setRepeats(false);
                    t.start();
                    setLocationRelativeTo(parent);
                    setSize(new Dimension(400, 100));
                    setVisible(true);
                }
            }
    
            public static void main(String[] args) {
                JFrame frame = new Testing();
                frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
    
        class MyComboBoxEditor extends AbstractCellEditor implements TableCellEditor, ItemListener {
    
                private JComboBox editorComboBox;
    
                public MyComboBoxEditor() {
                    editorComboBox = new JComboBox();
                editorComboBox.addItemListener(this);
                }
    
                public Object getCellEditorValue()
                {
                    return editorComboBox.getSelectedItem();
                }
    
                public Component getTableCellEditorComponent(JTable table,
                        Object value,
                        boolean isSelected,
                        int row,
                        int column)
                {
                    // which instrument is selected?
                    String instrument = (String) table.getValueAt(row, 0);
                    String[] methods = (String[]) subItems.get(instrument);
                    editorComboBox.setModel(new DefaultComboBoxModel(methods));
                    editorComboBox.setSelectedItem(value);
                    return editorComboBox;
                }
    
            public void itemStateChanged(ItemEvent e)
            {
                stopCellEditing();
            }
            }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables with like below codes: Table: Accounts id | username |
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I would like to run a str_replace or preg_replace which looks for certain words

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.