Creating a checkbox java list - java

i am trying to generate a checkbox list feeded by some records from a database just like the image (JAVA Swing), initially i tried with several chexkboxes but it didn't work, now i am trying with a multiple selection list but it didn't work as well and none of the answered questions here looks like to solve the specific needs i have.
Specifications:
Multiple selection list.
Every node of the list must have a checkbox object.
When checked every node must stay highlighted.
It must have an scrollbar if the content is bigger to the initially setted dimentions.

It must have an scrollbar if the content is bigger to the initially set dimensions.
Put the JList inside a JScrollPane.
When checked every node must stay highlighted.
This is going to confuse the user. The check marks are sufficient.
Every node of the list must have a check box object.
You'll have to extend a JList and a ListCellRenderer to gain this functionality. The ListSelectionListener that I have is over 100 lines of code.
You might find an already existing check box JList on the web. I have one in a book.

Related

Java selecting an option when clicking specific position within JScrollPane

I have developed a chat room application in Java that allows users to broadcast messages to every online user. I want to develop a new functionality that will allow the user to click on one of the online users in order to have a private chat with him/her. For this purpose I have developed a scroll-able text area that will contain those online users.
I think I could set the coordinates of each position that the name will be located at, in the text area so then when a mouse button is clicked I will compare this position against a position of the name in the string output of array list that was passed from the server (i.e. [Adrian, Buddy, Bob]).
As I have mentioned before, I am using a scroll-able text area for showing the online list, therefore I am not entirely sure if this approach would work out once I scroll down the list and click one of the positions.
Perhaps you could recommend me another approach I could adapt, or clarify if this one would work.
Here is the image of my chat client:
As I have mentioned before, I am using a scroll-able text area for
showing the online list, therefore I am not entirely sure if this
approach would work out once I scroll down the list and click one of
the positions.
Working with Scrollable TextArea for selecting items and responding with it the way you described: it will just be a hazard. Use JList with ListSelectionListener instead.
Tutorial Resource:
How to use List
How to Write a List Selection Listener

Get all selected checkboxes on button click [duplicate]

I have a dialog in Java that presents ~ 15 checkboxes to the user. Is there a way to get the names of all the checked checkboxes at once? Currently, I'm looking one by one if they are selected, which isn't that fancy of a solution.
I'm looking for something similar to Getting all selected checkboxes in an array but then in Java
When you are adding your Checkboxes to your dialog also keep a reference in a Collection of some sort. Then when you want to see which are checked you can just Iterate over the collection and check the state of each of them. You can get the name by calling getText on it.
List<JCheckBox> checkboxes = new ArrayList<JCheckBox>();
for( Component comp : panel.getComponents() ) {
if( comp instanceof JCheckBox) checkboxes.add( (JCheckBox)comp );
}
This assumes all of the JCheckBox instances are a direct child of the container panel. If not then you'd need to recursively visit all the containers of panel using the same logic. Now, while you can do this it's typically better to save these references as you created them into a list. Then you can easily iterate over all of the checkboxes without having to do this code above. If you have embedded components it's better to ask the embedded component to perform whatever operation you want over the checkboxes it owns (as opposed to pulling them out of the component through a getter so you can mess them in some way).

Java PropertyResolutionException when binding netbeans combobox selected item

I have a JComboBox which I designed in the Netbeans GUI designer. I have successfully bound the "elements" of the box to an ArrayList<String> so that the box populates properly. However, it currently automatically displays the first element of the list as the selectedItem.
I tried to bind the selectedItem to an int at first, thinking it would just show the item at the indicated index. No go. I got a ginormous exception that I didn't understand.
Then, I noticed that in the "Binding" window, it said the element had to be an Object. So, I bound it to a String, thinking that it would find the element that matched that string and show that element. No dice. Now I'm getting some sort of "PropertyResolutionException". I've even tried to use an Integer object with the same result.
Any ideas would be appreciated.
Absent your sscce, it's hard to say where things have gone awry. For reference, setSelectedItem() works as advertised in this example that selects L&F by name.

SWT ComboBoxCellEditor editable

I have a TableViewer where the values in one column should typically come from a dynamic list.
I'm currently using org.eclipse.jface.viewers.ComboBoxCellEditor , which is actually a Select-List: it stores the index of the selected value. If I change the underlying list (calling setItems(String[]), it's clumsy to keep the previous selected value... (specially if it's not included in the list anymore!) What I'd wish is actually a cell editor that stores, not the index from the list, but the string (perhaps letting the user edit it freely, perhaps not), where the list is just used as a suggestion at input time - like a "combobox" was supposed to work in the good old days... Is this possible?
I would suggest you to have your CellEditor to mimic the behavior that you are looking for. Extend ComboBoxViewerCellEditor and override doGetValue() method. Add modify listener on Combo control and also filter (which filters list items based on input text) to comboviewer.
You should look at :
org.eclipse.wst.xml.ui.internal.properties.StringComboBoxCellEditor This class comes from WTP project; It's an extended ComboBoxCellEditor that selects and returns Strings.
codemirror.eclipse.ui.xquery.viewers.StringComboBoxCellEditor It's the copy/paste of WTP StringComboBoxCellEditor; it adds the capability to add the item in the combo when it is not found.

Fixing the maximum size of a JList and changing the look of empty cells

I'm working on UI stuff with a JList that can contain at most 8 items. Logically I have prevented the application from adding to the JList once it has reached it's limit, however, I was wondering if there was a way to perhaps explicitly set a limit on the JList itself. Which would help with the next thing I want to do, which is to repaint empty cells (up to 8 cells at most) to indicate that they are empty slots that can be filled. I'm trying to find a way to do this (perhaps by extending ListUI) which does not involve adding placeholder elements into the JList to represent empty spaces.
I think the most simple way is to implement a list model (using DefaultListModel or AbstractListModel) My idea is that the model will always have 8 items. All of them with a empty message.
If you add a new item to the model you can replace the empty text with a specific text for that item. The model will keep the track about how many items are added to the model. If you try to add more that MAX_ITEMS you can thow an exception or something like that.
Implement DefaultListModel is really easy and you have a lot of samples on Internet.
You can try the approach shown here, which limits growth to N entries, with scrolling enabled thereafter. A ListCellRenderer, shown here, may be used to alter the item's appearance.

Categories

Resources