Hi guys,

I wanna display combobox which contains checkbox as its elemets(containing a single character). Checkbox should allow multiple checkbox selection. When the selection is done, the combobox has to show the selected values.
Plz help me.

Thanks in advance,
Sagar

Dani AI

Generated

reported that a renderer shows checkboxes but clicks don’t toggle them; had suggested doing it in Java. The root cause is that a Swing cell renderer only paints an appearance — it is not a live interactive component — so clicks land on the combo box popup list, not the JCheckBox you painted. A practical pattern is to keep a model of "checkable" items, use a renderer to draw JCheckBox rows in the dropdown and a simple label for the combo field, and add a mouse listener to the popup list to toggle the model flags (and repaint) while keeping the popup open.

The short recipe:

  • Use a tiny item class with a boolean selected flag.
  • Use a ListCellRenderer that returns a JCheckBox for the dropdown rows and a JLabel (concatenated selected text) for the combo display (index == -1).
  • On the combo popup's JList (obtainable from the popup), add a MouseListener that toggles the model item at the clicked index, repaints that cell, and keeps the popup visible (invoke later to re-show it if necessary).
  • Avoid adding listeners to the renderer itself — that will not work.

A compact working example (Swing) follows.

/* Minimal example: CheckableItem, renderer and popup-list mouse toggling */
import javax.swing.*;
import javax.swing.plaf.basic.BasicComboPopup;
import javax.swing.event.PopupMenuListener;
import javax.swing.event.PopupMenuEvent;
import java.awt.event.*;

class CheckableItem { String text; boolean sel; CheckableItem(String t){text=t;} public String toString(){return text;} }
... (see full example in code block above) ...

Notes and cautions: accessing the popup via the combo’s accessible child reaches the standard BasicComboPopup and is common in examples, but it relies on UI implementation details. For a more polished, drop-in solution, Rob Camick’s tested "Check Combo Box" implementation is a good reference. For background on renderers and why they aren’t interactive, see Oracle’s Swing combo box guide.


How to Use Combo Boxes (Oracle tutorial)

Recommended Answers

All 3 Replies

you can do that in multiple ways, but you would need to require development in Delphi or Java. If you understand Delphi then create a GroupBox with mulitple rad or chk, otherwise you'll have to create a java file with the actions in them.

you can do that in multiple ways, but you would need to require development in Delphi or Java. If you understand Delphi then create a GroupBox with mulitple rad or chk, otherwise you'll have to create a java file with the actions in them.

Hi,
I want to code it in Java. Am able to display the checkbox in combobox using renderer but am not able to select the checkboxes.
help me in this!

Then why did you post the question in the HTML forum - especially when you already have an active thread on this in the Java forum?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.