JcomboBox multiple selection - swing

I have a jcombobox in my application.
and i want it to support multiple selection (like it do in a jlist).
is there any code example?

I think it's not possible, unless you use a JList, like you said.
The JComboBox API reports:
The user can select a value from the drop-down list, which appears at
the user's request.
And a JComboBox tutorial:
Lists are not terribly attractive, but they're more appropriate than
combo boxes when the number of items is large (say, over 20) or when
selecting multiple items might be valid.
Update:
I reviewed this answer, because actually it's "possible" to do that using a ListCellRenderer, adding a checkbox to each item. Please consider this answer to implement this "solution".
However, I don't think it's a good idea for the following reasons:
there's a control like a JList that allows multiple selection;
a JComboBox control is designed just for single item selection;
it doesn't make sense to me to still use a JComboBox control and allow a multiple selection.

It's not quite impossible but there's a lot of work to do to get the job done. You'll need to create your own classes to extend/implement all of these:
ListCellRenderer (so you can mark the selected items when the popup is showing and indicate (at least) that there are multiple selections when it is not).
JComboBox (obviously)
BasicComboBoxUI (this is actually where most of the work is)
BasicListUI (at least I had to)
ComboBoxModel
And you'll need to develop your own class that implements ItemListener, MouseListener, PopupMenuListener, MouseMotionListener, PropertyChangeListener, KeyListener, ListSelectionListener.
Hint: you'll need to override a lot of createXXXListener() methods in the UI classes in order to bypass the many places where multiple selections are discarded.
[And it still doesn't allow for an editable multi-selection combo.]

Related

Dropdown select with delete option next to each option value

I need to implement a drop-down which has a delete 'X' option next to each option item. Somewhat like the image shown below.
The drop-down is populated dynamically and I need a way that does not inlvolve using list as an alternative.
EDIT: The icons next to each dropdown item refers to 'Edit'/'Delete'
You cannot put a checkbox into the usual <select> or multi-select HTML element.
However, here is another question where several good options are discussed.
This looks like the most useful and best suited to your purpose:
https://stackoverflow.com/a/27547021/1447509
And here is an example of how to change the default checkmark to an X:
https://stackoverflow.com/a/40123793/1447509
Sources:
How to use Checkbox inside Select Option
After selecting check box Instead of Tick symbol need X in html
UPDATE:
Given that you need both the HTML markup and the javascript to make it do what you want, you have two (possibly 3) steps to do:
This answer provides a good example of how to create the custom-rolled <select> control.
This answer shows you how to replace the checkbox created in step 1 with an icon/image of your choosing.
The javascript to remove the x'd <option> is very simple:
$(this).closest('option').remove();
IF you also need to save these results, then you also need to learn:
4a. Server-side SESSIONS (so that each user's customizations are saved for them)
4b. A login system, so you know for which user to save the current customizations.
4c. Just the basics of how to use a back-end database, such as MySQL/MariaDB, in which to store the user customizations.
4d. AJAX - so you can schlep info to the back-end for insertion into the database without refreshing (or navigating away from) the current page. AJAX replaces the ancient and no-longer-used <form> construct. Frankly, once you've used AJAX a couple of times, you'll never go back. Totally easy.
If you are in a bind and need someone to create the whole thing for you, I refer you to one of these websites - I have used such services myself and can recommend them.

filteringSelect with multichecked

is there any ready widget like that?
If not, can I combine the dijit filteringSelect with dojox multichecked?
if not, is it easy to create one of my own or has any one started doing this? it's so necessary for my project.
There is no widget like that as far as I know (the multichecked widget itself is not even a standard widget, since it's a part from DojoX).
About your question of combining both. I don't think that will be easy, because the dijit/form/FilteringSelect does not use a <select> as widget and I don't think they're made to be ran together.
And the answer to your last question: it all depends on what functionality you exactly need. A dijit/form/FilteringSelect only allows you to select 1 value (it's a textbox with a dropdown in fact, and a textbox can only have 1 value).
If you want to create a variant with radio buttons, then that is possible, but it is not an easy job. The multichecked is working with a simple DOM node, however, the dijit/form/FilteringSelect offers a lot more things like autocomplete, stores, ... . To make your widget work, you need to listen to all these events and adapt yuur radio buttons to it.
I made a simple example that is only displaying radio buttons for each item in the filtering select, the code can be seen at JSFiddle. However, it does not react to changes, that's a part that you will have to implement.

QTableView drag move rows

I'm using a QTableView with a QAbstractTableModel and a QSortFilterProxyModel so that I can sort the items by clicking on the table headers. I'd like to add the option for the user to sort the rows in the view manually, by dragging them. I don't need to be able to do drag and drop from/to any external application, just to change the order in the list. I also don't need to change the data in the model, I only want the order to be different in the view.
I've been looking through the documentation, and it seems like I have to implement mimeTypes, mimeData, and dropMimeData, but this gets very complicated fast! Some of the data in my model is not actually displayed in the view, and like I said I don't want to change the order of data in the model. Is there a way to simply drag items to change their sorting (just like the headers are already able to do) without a huge amount of coding?
Updated for QT5 to remove deprecated methods
If you are using PyQT All you need to do for your requirements is this:
your_tableview.verticalHeader().setSectionsMovable(True)
your_tableview.verticalHeader().setDragEnabled(True)
your_tableview.verticalHeader().setDragDropMode(QAbstractItemView.InternalMove)
Then rinse and repeat with horizontalHeader if you want those rearrange-able too.
You are absolutely right, you shouldn't need to touch or even know what the model is for this functionality. This is further demonstrated by your proper use of the QSortFilterProxyModel decorator over the model itself.
The stuff you saw about mimeTypes and all of that stuff is for drag-and-drop of actual objects of varying sources from other windows/applications/desktop/etc and isn't needed for what you are trying to accomplish currently.

Dojo/HTML equivalent for list box control

We are porting our MFC based client to Dojo. Is there any widget similar to listbox control. At present I am using DataGrid, but that seems heavy and overkill for our purposes. Alternatively what is best widget to replace listbox.
Update: I have already looked at dijit.form.multiselect, and I dont think that meets my requirement. MFC Listbox typically looks like this. I dont see (or rather dont know) how to replicate this with multiselect. It is possible that DataGrid is best fit for the control.
If you use dojo 1.7, take a look at the new DGrid.
For an example looking like yours, go to the tests page and pickup the Selector.html example.
Here are some options depending on which part of the ListBox you want to create:
dijit.Dialog to create a basic dialog box.
dijit.form.FilteringSelect to create a drop-down with your options (single select only).
You could also use radio buttons or checkboxes for your options depending on whether multiple selections are allowed.

Html - From checkbox to combobox

In the web app I am currently developing I have a form, and one of the fields is a boolean "IsFixedCost" (true/false).
The natural way, to me, to represent it on the form is with a checkbox: FixedCost? (checked = true, unchecked = false).
My boss forced me to change it in a combobox: FixedCost? -> option1: Fixed, option2: Variable
because he says that "it's more clear and understandable".
I find it totally senseless (because if it isn't fixed, it's variable, no other possibilities) and less usable (requires 2 clicks to change instead of one)
I'd like to hear opinions about this.
You should go for checkboxes, since a multiple choice combobox (I assume you mean select) is less accessible. In other words... Your boss is wrong. Radio buttons is to be avoided since they're not accessible.
Whilst you are correct, it isn't 100% obvious. Remember your application has to cater for the lowest common denominator (i.e. the stupidist person needs to be able to use it.)