Using JComboBox to change JLabels from data within same object: - java

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.

Related

Object to ArrayList to JList and back again (A few questions)

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?

How can I create a dynamically expanding array of arrays?

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

Custom component for JList instead of just strings

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).

how to create a Custom list model in Java

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.

GUI: Changing panels based on value of combo box

I have a question about GUI design, specifically with Java Swing and creating clean separation between presentation and model.
It's a bit difficult to describe, but essentially we have lots of reference data in our system (i.e. that would correspond to lookup tables in the DB). We want people to be able to edit them all from one screen.
So, in an ideal world what we'd like is a combo box in the top-left corner with a list of 'types' of reference data (so each corresponding to one table in the DB).
Then, when selected, a list of the data is populated below, also a filter (or search box). When one of these items is selected, the panel to the right is activated which will allow the actual data to be edited.
Now, here's the problem: each type of data we need to edit is different, so it has different fields etc. We could go with a generic solution but I'm not really a fan of them - there are lots of different validation rules for each etc, even for different clients, and it would be a nightmare to manage.
We're using the Presentation Model pattern to achieve some degree of separation between GUI code and the model but I can't think of a clean way of doing this which doesn't somehow blur the line of responsibilities a bit.
What are the ways you have solved problems like this?
[Note: apologies for the long question, hope it's understandable, I can re-phrase if necessary]
You could use the Factory Pattern to create a UI widget for the element that you are selecting. You could also use it to create a validation rule object depending on the type. This would give you some of the flexibility you desire.
So you can have something like:
IWidget widget = UIFactory.createFor(myObject.getType())
That can be invoked on the selection event to create the right widget to edit the selected element.
The IWidget could have methods such as:
validateData()
refreshData()
setDataElement(IDataElement element)
That would allow you to treat all UI widgets generically, but still have a special UI widget for each element type. I am assuming that the elements that you are selecting from the table all implement some IDataElement interface that contains the getType() method.
I used this solution tied together with the Eclipse Extension mechanism to plug-in new UI elements into my "base" solution, to have an extensible core and a high level of reuse. You could achieve something similar by injecting types and widgets into your factory, either manually or with Spring.
If you dont want to go down the generic path, you could have your model hold a mapping of combobox item -> panel name for use with a CardLayout. You could then create custom panels for the editing each of the reference data types. When the combo box selection is changed, you can save the current state in your model, request the panel name of the current selection, prepare your next panel for display and then have your CardLayout show it.

Categories