Creating a Button Group Allowing N checked boxes - java

I am programming in Java, using Swing.
I am currently working with an application which allows the user to display 2 or less hobbies. The list of hobbies is finite. I would like to provide a user with a list of checkboxes to select these items from, allowing them to check up to 2 options, but no more.
What are my options for implementing this? Is there a ButtonGroup like object that can hold these items?
I'm trying to avoid having 2 Combo boxes for this, as if the arbitrary 2 limit is increased (to size n) It'd be a pain to scale.

Hook up the action event of each checkbox and count how many are checked every time this handler is invoked. If as many are checked as it is allowed, disable every unchecked checkbox, else enable it.
Otherwise, just disable the submittion button and add a label explaining the situation to the user - and re-enable it as soon as the active number of checkboxes drops below the threshhold.

Consider using checkbox list (list control with checkbox next to each item). This approach scales better than producing a separate checkbox per item.

Related

Efficient Algorithm - Comparing lists with Virtual Scrolling

I need to test an application through UI where there are two panels.
One being available items and the other is selected items
These panes can contain thousands of items
Each available item has a checkbox used to select it and then the add button will add it to the selected pane.
My problem is there is virtual scrolling implemented on the panels.
Meaning I can only ever access a maximum of 16 items at a time,
(I've tried scrolling down to the bottom - but still, css selectors only work for the items I can see on the screen)
My Question is what is the best way to test this in terms of moving items from one panel to the next?
Possible approaches:
1) Move available items over to the selected items list (16 at a time) and assert they 16 got moved correctly ?
2) Move items over one by one asserting as I go they moved ok
3) Save all available items to a list then add them all over and compared the selected items to the saved list.
Could threading be an option?
Arquillian, Drone and Graphene are used so tests are done with page objects using Java

Showing a counter dynamically for JList items highlighted

I have a program that I created for work. This program takes an uploaded file, reads it, and puts the data into a JList in the GUI. The GUI actually has two lists and the user is able to move items between the left and right list by highlighting them like usual with a JList and then hitting an arrow to move the items. The lists are multiple-interval selection.
One small addition I would like to add is some type of counter that shows the user how many items they have selected before they actually move them between lists. This would need to be dynamic so if the user holds control down and begins clicking the counter will continue to update the number of highlighted items.
As the lists are often quite large and a user might need to move an odd number of transactions between the lists (Think 300 transactions in left list and the user needs to move exactly 50) it would be beneficial to have this counter.
Can anyone think of how this could be done? I'm not sure how to add an action listener to just clicking on the items. Please also let me know if I need to elaborate any more.
Generally my question is can I create an action listener just for when a user clicks a item in a JList that updates a counter for the current selected indices? Also it would need to change when they no longer have selected an indice.
Register a ListSelectionListener with your JList.
The listener could simply query how many rows are selected and update the number in the panel to that. Perhaps use getSelectedValues().size().
http://docs.oracle.com/javase/8/docs/api/javax/swing/JList.html#addListSelectionListener-javax.swing.event.ListSelectionListener-

Creating JRadioButton Matrix

I am creating a matrix of JRadioButtons (see image below). What I want to do is allow only one selection per row and column. Is there any way I can possibly assigned two button groups to a single button? Or is there any other suggestions how I may do this?
Description: I want the user to select one value from Highest, Normal, Least and Not Available option for Morning, Afternoon and Evening. However, I also want them to limit them to picking only one option from Morning, Afternoon and Evening for Highest, Normal, Least and Not available. So, basically, only one option needs to be selected for each row and only one option for each column, and yes, they can be the same option for the row and column.
Ideas? Suggestions?
You cannot use more than one ButtonGroup for a single JRadioButton. To solve the problem, just check for errors at click button, then display the proper error message if any error encountered.
Another smart idea is hiding per click. Handle click event for each one, so that when a radio button is selected its row and column are hidden setVisible(false) (or at least disabled setEnabled(false)).

Filtering a ListView in wicket using 2 drop down boxes

I am trying to filter a list that is placed into a listview through the use of 2 drop down boxes.
The first dropdown box is titled price and the second is owner.
I want to be able to select a value in one or more of these drop down boxes and then have the List view re-render with the filtered results.
The trouble is I do not know how to begin this task, would someone be so kind as to enlighten me :D
Thanks in advance!
Your best starting point is probably this example: (Source code also available on this page, ChoicePage.java is the name)
First of all, you have to use a dynamic model in your ListView that generates the list of items depending on what you had selected in the dropdown boxes.
Then the basic idea is that you add an AjaxFormComponentUpdatingBehavior to the components that control the updates (your two dropdown boxes in your case), and in the onUpdate() method of this behaviour you should add the component you want to update to that AjaxRequestTarget passed.

GWT RadioButton Grouping

In my application, I have URN-identified data coming in from the server. I'm in the process of abstracting as far as possible so there is very little to no logical code in my views, and I'm using a generic presenter that wraps those views. All widgets have URNs, making it super easy to map incoming data to a specific widget (until now, a 1 to 1 relationship). This has worked well for pretty much every widget, and now I've reached a point where I'm tripped up.
Assume I have (just for simplicity's sake) two RadioButton elements on a view. These buttons belong to a "group" (just by setting their name values to the same thing), but obviously they're 2 distinct elements. I can't map my URN-identified data to a single widget as in every other case because, in this case, it is two widgets.
Here's an example of what I mean:
Utility Company is a ListBox, so just one widget there. I map each item in the list to a specific Enum value.
Utility Rate is a TextBox, so again just one widget to map.
For Energy Usage, they can select to use either an average for the year or input 12 monthly values. I'm stuck here. I can't map to just one of the RadioButton elements, because then I'd need some extra logic in the view to handle the behavior appropriately.
Am I stuck mapping to just one widget and sticking (unwanted) logic in my view to determine what the state of all of the elements should be based on the value that came in for the one widget that is mapped?
How should I handle this case?
Edit (Solution):
Following the concepts of jusio's answer, I came up with a workable solution. Because I didn't want to go sticking special case handling through my logic to take care of a non-widget, I created a RadioButtonSet faux widget (public class RadioButtonSet <T extends Enum<?> & HasDisplayText> extends Widget implements HasValueChangeHandlers<T>, HasValue<T>), into which I manually pass the radios I intend to group. Having done that, I can get or set its value and have it fire appropriate events when the user changes the selection. Then mapping the collection of radios is no different than doing so for a listbox. Thanks jusio.
I believe in your case you shouldn't treat radio buttons as two separate widgets, basically in your case you can treat the radio button group as combo box, because behavior is almost the same (the only problem is that you have additional master detail). So basically what you will have to do is to wrap real BO objects into some kind of RadioButtonGroupModel, and give it to view, view can take this model and generate radio buttons (with some editors or whatever else). I remember running into this problem when i was extending databinding FW for JFace, and this was the best way I could find to solve this problem.
If I understood correctly the problem, there are 2 possible solutions:
Give each RadioButton a unique URN (ex: oldURN_1 , oldURN_2)
When you send data for a URN, disable the other one
Keep the same Name for each RadioButton but add a number variable in the data the server sends indicating which radioButton it is supposed to use (ex: 0 for Average and 1 for Monthly)

Categories