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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:05:27+00:00 2026-06-14T16:05:27+00:00

I need to share data between two(or maybe more) comboboxes, but I want to

  • 0

I need to share data between two(or maybe more) comboboxes, but I want to choose elements independently. For example, if I choose Object1 in first comboBox, my second ComboBox also chooses Object1, because they have same model(DefaultComboBoxModel and this model also manages chosen objects). But I don’t want this behavior. I want to choose objects in my comboBoxes independently. When I choose object in first comboBox my second comboBox shouldn’t change.

At this moment I’am thinking about supermodel for two models. Supermodel will send events to submodels and they will update comboboxes data, but not the state. But I think this is not the best way.

Are there more intresting and simple aproaches?

Here is the short code to understand what I mean:

package hello;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JComboBox;
public class Comboboxes extends JFrame
{
private JPanel contentPane;
public static void main(String[] args)
{
    EventQueue.invokeLater(new Runnable()
    {
        public void run()
        {
            try
            {
                Comboboxes frame = new Comboboxes();
                frame.setVisible(true);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    });
}

public Comboboxes()
{
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JComboBox one = new JComboBox();
    one.setBounds(10, 11, 414, 26);
    contentPane.add(one);

    JComboBox two = new JComboBox();
    two.setBounds(10, 52, 414, 26);
    contentPane.add(two);

    DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>();
    model.addElement("Item 1");
    model.addElement("Item 2");
    model.addElement("Item 3");

    one.setModel(model);
    two.setModel(model);
}
}
  • 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-14T16:05:29+00:00Added an answer on June 14, 2026 at 4:05 pm

    Write a decorator for your ComboBoxModel. The decorator should manage the selectedItem property, while everything else is managed by the delegate.

    You would then have 1 original model, and place different decorators on the comboboxes:

    DefaultComboBoxModel original = ...;
    
    DecoratedModel firstModel = new DecoratedModel( original );
    JComboBox firstCombo = new JComboBox( firstModel );
    
    DecoratedModel secondModel = new DecoratedModel( original );
    JComboBox secondCombo = new JComboBox( secondModel );
    

    Changes to the data can then be performed on the original model, which will adjust the data in all comboboxes simultaneously

    Note: make sure listeners attached to the decorator receive events with the decorated model as source, and not with the delegate model. This is a common mistake when writing a decorator

    Edit

    An alternative is to have a base data structure which is not a ComboBoxModel and create an implementation of ComboBoxModel which uses that data structure. You can then create different combobox model instances which all share the same data structure.

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

Sidebar

Related Questions

I need to share a stack of strings between processes (possibly more complex objects
I'm developing a mechanism for interchanging data between two or more processes using shared
I'm trying to come up with some easy methods to share data between two
i'm working on multisite and I'd like to share session data between two or
I have a few different applications that i need to share code between to
For testing purposes, I need to share some definitions between Tcl and C. Is
I want to have an array of objects that is shared between two different
I need to build an encrypted connection between two peers, and I need to
I have searched through SO looking for different ways to share data between view
I am looking for the best way to effectively share chunks of data between

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.