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-
Related
I have a java program which keeps records of databases. I have made use of JComboBox for adding data to my db system. I have to initialize more than 10000 string into my JComboBox. I have used keylistener to make my program auto search elements inside JComboBox.
The problem is that it is taking a lot of time to search a single key. Is there a programming technique to make search faster with keylistener for more than 10000 string elements in JComboBox. Should I have to make use of multithreading to keylistener?
Generally:
Never show so much elements at once into a List,Table,ComboBox etc. It makes the program lags and you spent a lot of memory.Maximum items to be shown per time must be <=300.Basically the comboBox its not so good idea a list or table will fit better.
How?
Every time the list shows 300 items,the user can use next button to load the next 300 from the database or previous button to load the 300 previous items.
About Search:
On every key pressed by user you search into database table you have for the 300 or less best fitting the result and then you add them into the List and ComboBox removing the previous items first.
More about search:
If you want you can retrieve all the items matching to search and use pagination for search results.
Program Outline:
I plan to make a simple Java program that will load Vehicle objects (Vehicle being the superclass, EnginedVehicle and GoodsVehicle being the subclasses) from an XML file into an ArrayList which will then be displayed on a JList. The user will be able to show/hide the different Vehicle types using check boxes, add a new vehicle type or press the selected item in the JList and edit or delete it. The program will then put the Objects back into the ArrayList where it can be then saved back to the XML file.
Question: So, I am completely fine with the loading of the XML file into the ArrayList and putting that object onto the JList but the thing that is hurting my head is thinking about how I am going to:
What is the best way of getting the object back from the JList ready for it to be modified or deleted and put back into the ArrayList?
How would I show/hide the different types of Vehicles in the JList using the check boxes?
I understand this may seem a lot but, this is my first post and I am new to the community and I have fairly good knowledge of Java and OOP programming but I have just finished writing a fairly big website and going back to Java is a headache.
Since your ArrayList should be equal in size (item count) to your JList, your JList will have the index you're interested based on selection. Regardless if you want to modify or delete the item, store what index it was at and remove the item from the JList (You should be using a DefaultListModel). Use this index value to get the object from your ArrayList. If you're modifying, modify your object as needed, you shouldn't have to remove the object from the ArrayList for modifications, and place it back into your DefaultListModel. If it's a delete, then just remove the object from your ArrayList using the index value you stored.
As far as displaying (show/hide), clear your DefaultListModel (which will clear your JList), iterate through your ArrayList and add the items to the DefaultListModel that match your checkbox selection criteria.
EDIT:
I didn't take into consideration of possibly modifying/deleting items when items are hidden. For this, may want your objects to have a field that stores what index they are at in the ArrayList. This way when you do your filter, I would copy the items from your "Master" ArrayList into a sub list that you can populate your DefaultListModel. Then you apply the same logic to this sublist when selecting an item from you JList, then take your changes from your sublist and apply them to the "Master" ArrayList.
Keep in mind that when you remove an item, you'll have to reassign all items index location from that point on down.
I'm sure there is probably a cleaner way of doing this, but this is what first comes to mind for me.
I don't know if I'm horribly mistaken, but why change to a JList at all? Do you use your JList as a parameter to visualize the information in it? If yes, why dont you use your ArrayList instead? Then the checkboxes only change the visibility ot the Items of the List. So you dont have to care about indices, because they stay the same. And new entries, can be made as well... maybe im wrong but i guess you got kind of a GUI for the user to browse/alter/add new vehicles?
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.
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.
I have created a dialog with two JLists displayed and a button that takes the selected value from the second JList and does something (say it outputs the selected value). The list of values from the second JList is updated according to the selection of the value from the first JList.
When the dialog is displayed initially, the button is disabled. When a value is selected from the first and then from the second JList, the button is enabled an the required selection listener is added to it.
The problem is that every time the button is clicked the number of output messages is equal to the time a value of the second JList is selected. For example if I select a value from the second JList, then I change my mind and select another value, the click of the button will output the message two times.
Does anyone know a method to prevent such a thing?
Your ListSelectionListener should check for (e.getValueIsAdjusting() == false) otherwise you'll respond to all of user's selections and not just the final one.
Yes: don't cache the selections, just process the actual selection in your second list.
If possible, post the code that is executed once your button is pressed. I guess, you have some sort of collection (a list or queue) that stores all the selections you do on the dialog and when you press the button, each stored selection is processed.
This looks like an intended behaviour, because you usually don't code this by accident ;)
If it is intended and you just want to eliminate duplicates, consider using a Set instead of a list, as a Set will only contain unique values.
It does perfectly what it should do.
It fires two events,
1> Selection is removed from first item.
2> Selection is done to second item.
So as fbcocq said, you should check for getValueIsAdjusting(). Check this out, it'll help.
Are you adding an ActionListener to the button every time you enable it?