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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:22:32+00:00 2026-06-06T12:22:32+00:00

I’m trying to place a JComboBox inside a certain column of a JTable. I

  • 0

I’m trying to place a JComboBox inside a certain column of a JTable.
I have this code, and it is working:

    model = new DefaultTableModel();
    JComboBox<String> optionComboCell = new JComboBox<String>();
    optionComboCell.addItem("Option 1");
    optionComboCell.addItem("Option 2");
    optionComboCell.setSelectedIndex(1);


    table = new JTable(model);
    // Adding here all the columns, removed for clarity
    model.addColumn("Options");
    TableColumn optionsColumn = table.getColumn("Options");
    optionsColumn.setCellEditor(new DefaultCellEditor(optionComboCell));

My problem with this, is that it doesn’t show as JComboBox until a cell in that column is selected.
When the JFrame is loaded, the whole table looks the same, as if all the cells where only text.
When clicked, it shows the combo box’s arrow and options, but again when deselected, it looks like a regular cell.

Any way to get around that?

  • 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-06T12:22:34+00:00Added an answer on June 6, 2026 at 12:22 pm

    Yes, use a JComboBox to render your cells:

    import java.awt.Component;
    import java.util.Enumeration;
    import java.util.Vector;
    
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.SwingUtilities;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableCellRenderer;
    import javax.swing.table.TableColumn;
    
    public class Test4 {
    
        private static class ComboBoxCellRenderer extends JComboBox implements TableCellRenderer {
    
            public ComboBoxCellRenderer(int column) {
                for (int i = 0; i < 10; i++) {
                    addItem("Cell (" + i + "," + column + ")");
                }
            }
    
            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                setSelectedItem(value);
                return this;
            }
        }
    
        protected void initUI() {
            JFrame frame = new JFrame("test");
            frame.add(getTable());
            frame.pack();
            frame.setVisible(true);
        }
    
        private Component getTable() {
            Vector<Vector<String>> data = new Vector<Vector<String>>();
            for (int i = 0; i < 10; i++) {
                Vector<String> row = new Vector<String>();
                for (int j = 0; j < 3; j++) {
                    row.add("Cell (" + i + "," + j + ")");
                }
                data.add(row);
            }
            Vector<String> columns = new Vector<String>();
            columns.add("Column 1");
            columns.add("Column 2");
            columns.add("Column 3");
            DefaultTableModel model = new DefaultTableModel(data, columns);
            JTable table = new JTable(model);
            table.setRowHeight(20);
            int i = 0;
            Enumeration<TableColumn> c = table.getColumnModel().getColumns();
            while (c.hasMoreElements()) {
                TableColumn column = c.nextElement();
                column.setCellRenderer(new ComboBoxCellRenderer(i));
                i++;
            }
            JScrollPane scroll = new JScrollPane(table);
            scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
            return scroll;
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new Test4().initUI();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

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.