0

I am faced with the following problem:

I have a JCheckbox, with an associated JComboBox. (Well, its only associated in the fact that it sits next to each other horizontally on the tab, & also, the naming convention is that the first couple of characters of their names are always the same, indicating they belong together). I have listeners on these types of components, to do some stuff when it changes.

Now, these types of elements, can appear in a tabbed pane (on the different tabs). Checkboxes, may appear without Comboboxes, but a combobox cannot appear without a Checkbox associate.

At the moment, I am interested in constructing a log string, like this: (Name of Tabbed Panel)>(Name of JPanel)>(Name of Checkbox)>(Name of Combobox)

--> Of course I would stop at the Checkbox name, if the combobox is not changed, BUT if the Combobox changed, I would like to associate it with its Checkbox. Of course all this happens in the action event handler of the two components... Also, I already get the name of the tabbed panel, the jpanel and checkbox.

The question here is about somehow binding a combobox with a checkbox, in order to get checkbox info, when the combobox changed.

I hope this is clear, please ask if there is anything that needs clarification

Harriet
  • 1,633
  • 5
  • 22
  • 36
  • So what's the problem? i.e. what have you tried so far and what part of it didn't work? – Tim B Jun 03 '15 at 14:22
  • Hello Tim - I am trying to figure out how to go about solving the problem, I thought sharing it might spark some interest amoung those willing to share. The point of the question is to discuss ways in which it could be approached, at least that was my intent. – Harriet Jun 03 '15 at 14:27
  • 2
    `I hope this is clear` - not to me. Post a proper [SSCCE](http://sscce.org/) demonstrating what is currently happening. Then try again to explain what you want to happen. "A picture is worth a thousand words". In this case the picture is the SSCCE. – camickr Jun 03 '15 at 14:43
  • 1
    Did you consider creating a complete new **combined** control? (like: CheckedComboBox -> a ComboBox with a CheckBox on the Left/Right)? By doing this, you only have one Ctrl (ComboBox including CheckBox). When ComboBox is not needed in your new **combined** Ctrol, you can just make it invisible. And (of course) you have an association between the ComboBox and the CheckBox. – Ben Jun 03 '15 at 14:56
  • Hello Ben, thank you for your suggestion, this does seem to be an option I have not yet thought off. I was thinking maybe a mapped list, with key value pairs. Another option is to use the naming convention, to show that controls logically belong to each other. Your suggestion seems to be the most elegant, and does not rely on weird programming concepts. I already have a JTabbedPane, with tabs where these controls are populated. Would it be easy to replace the existing Checkbox - Combobox with a control? – Harriet Jun 03 '15 at 15:10
  • 1
    if you want me to get notified when you write an comment, you should use @ben...i will prepare an answer soon... – Ben Jun 03 '15 at 15:55

1 Answers1

1

As described in the comments, you could create a new combined control and use this instead of single ComboBox and CheckBox.

Simple Example for combined Control:

public class CheckCombo extends javax.swing.JPanel
{
   private final javax.swing.JCheckBox jCheckBox1;
   private final javax.swing.JComboBox jComboBox1;

   public CheckCombo()
   {
       jCheckBox1 = new javax.swing.JCheckBox();
       jComboBox1 = new javax.swing.JComboBox();

       this.setLayout(new java.awt.BorderLayout());

       this.add(jCheckBox1, java.awt.BorderLayout.WEST);
       this.add(jComboBox1, java.awt.BorderLayout.CENTER);
   }

   public javax.swing.JComboBox getComboBox()
   {
      return jComboBox1;
   }
   public void setCheckboxSelected(boolean b)
   {
       jCheckBox1.setSelected(b);
   }
   public void setComboBoxVisible(boolean b)
   {
       jComboBox1.setVisible(b);
   }
   public void setCheckboxText(String s)
   {
       jCheckBox1.setText(s);
   }
}

Just replace your regular ComboBox with CheckCombo from my Example and remove the associated Checkbox.
You can use the public methods to modify the CheckCombo's state.

Cheers.

Ben
  • 3,378
  • 30
  • 46
  • PropertyChangeListener with firePropertyChange for bounds property, – mKorbel Jun 03 '15 at 19:54
  • Hello @ben, Thank you for replying and taking the time to prepare this, it is a very good example, and the most elegant solution. I tried to implement it this way, but it would require a lot of code changing ( I am making modifications to an existing solution). So for now, I am going to use a mapped list, but I definetly just want to re-write the current solution to be more elegant. It is legacy code that is very old, so go figure. My deadline is coming up - so thanks again for your input. – Harriet Jun 04 '15 at 08:06