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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:21:07+00:00 2026-05-28T04:21:07+00:00

Hi if You put JComboBox in JTable and String[] array to JComboBox everything works

  • 0

Hi if You put JComboBox in JTable and String[] array to JComboBox everything works fine. Buf if You put your own data type to JComboBox selecting values in same column becomes complicated. Here is official example. Try changing following part:

JComboBox comboBox = new JComboBox();
comboBox.addItem("Snowboarding");
comboBox.addItem("Rowing");
comboBox.addItem("Knitting");
comboBox.addItem("Speed reading");
comboBox.addItem("Pool");
comboBox.addItem("None of the above");
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));

Into:

JComboBox comboBox = new JComboBox();
comboBox.addItem(new Test("Snowboarding"));
comboBox.addItem(new Test("Rowing"));
comboBox.addItem(new Test("Knitting"));
comboBox.addItem(new Test("Speed reading"));
comboBox.addItem(new Test("Pool"));
comboBox.addItem(new Test("None of the above"));
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));

And create new data type:

public class Test {
    private String name;

    public Test(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }
}

You will see, that when you click on table cell in witch there is JComboBox with custom data type. First column cell’s value get’s selected automaticlly. How to fix this issue?

EDIT 1: I added SSCCE.

Main Class:

import java.awt.BorderLayout;

public class windw extends JFrame {

    private JPanel contentPane;
    private JTable table;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    windw frame = new windw();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public windw() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();

        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        table = new JTable();
        String[] grupes2 = new String[3];
        grupes2[0] = "first";
        grupes2[1] = "second";
        grupes2[2] = "third";

        table.setModel(new DefaultTableModel(
            new Object[][] {
                {new JComboBox<String>(grupes2)},
                {new JComboBox<String>(grupes2)},
                {new JComboBox<String>(grupes2)},
                {new JComboBox<String>(grupes2)},
                {new JComboBox<String>(grupes2)},
                {new JComboBox<String>(grupes2)},
                {new JComboBox<String>(grupes2)},
            },
            new String[] {
                "Column name"
            }
        ));
        TableColumn sportColumn = table.getColumnModel().getColumn(0);
        sportColumn.setCellEditor(new DefaultCellEditor(new JComboBox<String>(grupes2)));
        sportColumn.setCellRenderer(new Renderer(grupes2));
        contentPane.add(table, BorderLayout.CENTER);
    }

}

Renderer:

import java.awt.Component;

import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

public class Renderer extends JComboBox implements TableCellRenderer {
    public Renderer(String[] items) {
        super(items);
    }

    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        if (isSelected) {
            setForeground(table.getSelectionForeground());
            super.setBackground(table.getSelectionBackground());
        } else {
            setForeground(table.getForeground());
            setBackground(table.getBackground());
        }

        // Select the current value
        setSelectedItem(value);
        return this;
    }
}
  • 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-05-28T04:21:08+00:00Added an answer on May 28, 2026 at 4:21 am

    The problem is that your TableModel is storing a String object and the ComboBox contains a Test object. These objects are not equal so there is no item to select and it looks the first is automatically highlighted.

    Change your code to the following and you will see the same problem with an unknown string:

    {"Joe", "Brown", "Pool?????", new Integer(10), new Boolean(false)}
    

    To fix the problem, I would guess you need to do the following:

    {"Joe", "Brown", new Test("Pool"), new Integer(10), new Boolean(false)}
    

    You would then need to implement the equals() method in your Test class to compare the name property of both components. As well, you would need to implement the hashcode() method.

    In the future, as Andrew suggested, include your SSCCE with your question as we don’t have time to copy/paste/edit and test code because we never know if we do it exactly the same way you do.

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

Sidebar

Related Questions

I want to put individual JComboBoxes into each cells of a JTable. ie. The
We put all of our unit tests in their own projects. We find that
Simply put, is there a way to create a 2D javascript array using similar
If I put: variableName = namecombobox.selectedItem or Dim variablename as type = namecombobox.SelectedIndex Visual
Actually I want to add a JCombobox in a JTable and fire its action
This is how my JTable is reconstructed from the serialized data: table = new
I want to put a JComboBox inside a JRadioButtonMenuItem in a popup menu, is
What is the maximum number of items you can put in a WPF ComboBox
Put differently: Is there a good reason to choose a loosely-typed collection over a
Put it another way: what code have you written that cannot fail. I'm interested

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.