i'm pretty much a newbie to programming so this may seem like a very stupid question, I've been trying to do this for a while but cant get my head around it.
But I was wondering if its possible for the JList object to store more than one value, so when i click on the object in the list, the different values such as price, weight, colour etc. will be then displayed into some JTextField's below the list.
And would it then be possible to then add that JList object into another JList?
I've been researching on how to do this but I cant seem to find it anywhere. Is there an easier approach to doing this?
Thanks in advance!:)
So this is basically what i want it to look like:
But I was wondering if its possible for the JList object to store more than one value, so when i click on the object in the list, the different values such as price, weight, colour etc. ..
Yes & no - but more yes than no. To explain:
What is required here is to define a new class that encapsulates that information. It might be something like a 'menu item'. That (let's roll with that) MenuItem
class then has fields to store the description, price, calories and other attributes of that menu item.
Then the list simply has to be configured to handle a MenuItem
. Configuring it might not be necessary if the toString()
method of the class shows all the information you would want to show in the list. Otherwise look to use a custom renderer to show exactly what is required.
The rest, displaying the attributes in fields and moving from list ( MenuList
) to list ( MealOrder
) is pretty straightforward, and covered in the relevant tutorials.
JList can have any Object as its item members; what shows in the list is the string from that object's toString() method. So you make any subclass of Object that can have any number of fields, and override toString() to return whatever string you want to see in the list..
It is possible of course, what you need is a class to stock the object's states/fields.
Here is a class that show approximatively what you want.
When the item is selected, a dialog shows up showing the characteristics of it.
The same behavior is reproducible within the program you'll be making.
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class Test {
public static class Item {
String name;
double price;
double weight;
Color color;
public Item(String n,double p, double w, Color c){
name = n;
price = p;
weight = w;
color = c;
}
public String extendedToString() {
return name + " " + price + " " + weight + " " + color;
}
@Override
public String toString() {
return this.name;
}
}
public static void main(String[] args) {
JFrame f = new JFrame();
Item i = new Test.Item("myItem", 5d, 2d, Color.black);
JList<Item> l = new JList<Item>(new Item[]{i});
l.addListSelectionListener(new ListSelectionListener(){
@Override
public void valueChanged(ListSelectionEvent e) {
JList l = (JList) e.getSource();
Item item = (Item) l.getSelectedValue();
JOptionPane.showMessageDialog(f, item.extendedToString());
}
});
f.add(l);
f.setSize(500, 500);
f.setVisible(true);
}
}
It is possible.
When your model grows and you start wondering how you should handle all of these listeners and propagate the changes, start looking for an in process EventBus .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.