Let's say I want to make an app, where the user can add colors to a list, so I don't know how many colors there's gonna be. When the user is adding a color to the list, he/she also wants to type the colors which can be combined to make the decided color.
However I don't know in advance how many options the user needs, as it's different from color to color. Therefore, I think an array isn't an option (correct me if I'm wrong). Also, when the user search for blue or yellow, the app should give back green, as those combine to green.
I don't want you to write my code, so if you can just give me some reference where I can learn more, I'd be very pleased.
Forget about arrays, and learn Collections in java.
Just have l look at List interface and some implemented class of it, like ArrayList.
An ArrayList is by definition
Resizable-array implementation of the List interface
You can use ArrayList.
There is no need to specify size in ArrayList. You can add as many as you want.
ArrayList<Collection> color = new ArrayList<Collecton>();
Here your Collection type consist of all the attributes you want in your color
Related
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 design a GUI, and I am really stuck on a certain issue, which involves using JComboBox to change data in a JLabel, but the catch is the data in the JLabel comes from object data the combo box needs.
Example: hypothetical arraylist with objects in each locale:
ArrayList<Animals> FarmAnimals = new ArrayList<Animals>();
FarmAnimals.add(new Animals("Cows", "Black"));
FarmAnimals.add(new Animals("Pigs", "Pink");
FarmAnimals.add(new Animals("Sheep", "White"));
Now, I want to populate the JComboBox with "Cows", "Pigs", and "Sheep", which is easily done with making a string array of the values. The problem is that, when I, hypothetically, select "Cows", I want a color JLabel to display "Black", "Pink", and "White" respectively when chosen: ideally from the object data within the array (just in case I have more data for Cows or whatever like paw size or loudness of animal noise that I'd like to input later).
Is there any way to get an ItemListener to do this, or at least is there a tutorial that shows how to directly influence JLabel data from objects associated with what's in the JComboBox? Or am I just being stupid and there's a much better way of doing it? Thanks.
This might be a better example:
http://pokemonshowdown.com/damagecalc/
Notice that when you change the EV's or the IV's, or if you change the pokemon, the stats on the side directly change with respect to the Pokemon chosen and the change in the EV & IV inputs. I want this behavior in the GUI I'm making (stats change directly based on inputs, which I assume are directly connected to object data stored for each Pokey.) but I can't seem to mimic it.
One technique here is:
Store all your Animal data in an array, as you are currently doing.
Give the Animal class a method to receive the relevant object data, in whatever form most naturally represents it (for example, if all your animal-specific data is a list of strings like colors, a String[] or a List<String> would be a good way to hold this data). Let's call this method getData().
Override Animal.toString() to return the name of the animal that you want to display in the combo box.
Now, populate the combo box with a list of animals. Use JComboBox.addItem to add each Animal directly to the combo box.
Add an action listener to the combo box. In the action listener, you may use (Animal)comboBox.getSelectedItem() to retrieve the selected Animal directly.
Now that you have the selected Animal, you can call theAnimal.getData() to get the data you want to put in the label. You can call any other methods of Animal here too.
Basically, the idea above is to take advantage of JComboBox's ability to directly store and return your actual objects. Then you can get the relevant data directly from the selected Animal, and update other GUI elements (e.g. your label) with that data accordingly.
A couple of things to note about the above:
If overriding Animal.toString() to provide a display string is not appropriate for your situation for some reason (e.g., as MadProgrammer points out in the comments, toString() is typically used for debugging output rather than information meaningful to a user), you have other options. You could, e.g., add Strings to the combo box and, if the combo box is not sorted, take advantage of the fact that the combo box indices are the same as the indices into your Animal array. Then in your action listener, you get the selected index, and look up the animal that way. Another option is to define a custom ListCellRenderer, which is beyond the scope of this answer (but information is readily available on the internet).
You also may notice that you don't actually need the separate animal array with the above approach. You could just use the combo box itself as the "array". This may be sufficient for your needs, but bear in mind that it does tie your logic very closely to the GUI, and could cause problems in more complicated applications, especially when e.g. writing unit tests, or when writing methods that expect the collection of animals (and you end up having to pass the JComboBox/ComboBoxModel itself, or copy the items out of it into a temporary array), etc.
Sorry if that was a little vague, hopefully it gets you started down the right path.
I have a large set of data from which the user has to select one. I'm thinking of a way to implement it (of course, in a GUI). I have a few ideas. But just thought of posting here as there may be better alternatives..
Say, user has to select a name from a large set of user base. If I simply put a text field for user to enter the name, then there can be issues like entering same name in different formats, misspelling etc...
I see two options here
Using a combo box
Using a list (Actually i'm thinking of something like a tool tip. As I cant show the whole list always due to space issues)
But combo box won't be much user friendly i guess. As the user will have to scroll around the whole list to select an entry. If the number of entries are too large, this will be
Which means, now I'm left only one option. A popping up list, which will change the content according the text user is entering in the text field. So he can type first few letters and the list will show all the entries starting from the entered text. Got my point, right?
Are there any other better to achieve this kind of need?
If I'm going to implement above, what will be the best way to follow. I'm thinking of extending the JTextField to add required functionality. Well, I'll put some method to set the popup list entries. And I'll add some actionListner to watch the text field, and control the popup list accordingly...
Autocomplete is what you are probably looking for. Google for "java swing jcombobox autocomplete" and limit results for the last couple of years to get relevant results. There will be a lot of examples and ideas on how to implement this with custom code.
I believe there is also some custom libraries like "swingx" that provide at least partial or full implementations to save time.
http://swingx.java.net/
They have released code as recently as the beginning of this years so it appears active and might have what you need.
You could take a look at SwingLab's autocomplete feature, it allows you to attach it to a JCombBox, JList or JTextComponent
use AutoComplete JComboBox/JTextField
based on Standard Java Classes
no issue with larger sets of data
no issue with Focus, BackSpace Key, Caret
for better performance to required sort the array before use
simple workaround for setStrict(true/false), restrict input to array
I've been trying to freshen up on my Java knowledge and I've been building a small GUI program and I've been running into a bit of a problem.
Basically, I have a JList which I'm currently populating with strings from an object from one of my classes which implement AbstractListModel which we can call my ItemList class. It contains an ArrayList of objects of the type Item which implements Serializable.
But, what I'd like to do is rather than populate my JList with a bunch of strings I'd like to populate it with some kind of string + JTextField combination so I can see one property of each Item object while also being able to update another property by changing the JTextField.
Now, what I'm looking for is the simplest possible way of doing this, and I am assuming there is a (relatively) simple way to do this since it's such a common thing to want to do in a GUI application (although I wouldn't put it past Java and Swing to make it convoluted and complicated).
So, what is the correct way of doing this?
No need to ever use String objects. Instead:
Put Item objects in the JList.
Add a ListCellRenderer to the list, to show the Item object the most user friendly way.
When the user selects an item, show the details in a different place (I'm thinking a panel with 2 columns of labels and text fields, and two rows - one for each attribute, and perhaps a button to Save)
The edit controls would best be encapsulated in a panel that can then hidden when not required, & put in a variety of places, e.g.
Below the list
In the main part of the GUI
displayed in a JOptionPane or a (modal or not) JDialog
Here is an example of placing the 'view/edit panel' (the file details) below the selection component (the table).
I'm trying to create list that shows contacts, each list item shows the name on a line, and the phone number on a second line, and maybe an image or icon. I was thinking of using two labels for that, but i can figure out how to use a custom list model to implement this.
My first attempt was to add a Panel object that contained the info i wanted in the list, then add it to an instance of the defualt list model, but that only turned up the class name in the list.
DefaultListModel Clistmodel = new DefaultListModel();//
Clistmodel.addElement(Contact);//Contact is an JPanel object
GroupList.setModel(Clistmodel);//GroupList is the List object
this didn't work out at all then i learnt that the default list model only knows how to render strings i think, so i have to create a custom list model, or a custom ListCellRenderer, i don't really know which will solve the problem.
Your question asks how to create a custom list model, however, that's not what you need (I don't think) as a DefaultListModel will work nicely for you. Rather you will need to work on the renderer. You need to create a non-GUI class to hold your information that each item will display, probably your Contact class, and then create a JList that holds this in its DefaultListModel.
The key for you will be to then create a custom list cell renderer to display the information on multiple lines -- perhaps a JTextArea, or a JPanel that holds two JLabels in a GridLayout. Please understand that the renderer does not display the actual underlying components, but something more akin to a stamped image of whatever components you're trying to display, so it will not have the full behaviors available to it as the actual component would. It will take work, but the writing a renderer section of the tutorial linked to by user714965 will show you how to do this.
Please give it a try, and then if you still are stuck, come on back with your code, your errors, and your questions, and we'll be better able to give you specific help.