Class ComboItem
public class ComboItems {
private Long key;
private String value;
public ComboItems(Long key, String value) {
this.key = key;
this.value = value;
}
@Override
public String toString() {
return value;
}
public Long getKey() {
return key;
}
public String getValue() {
return value;
}
}
Set Class ComboItem to ComboBox
private void loadSupplier(FormBuy formBuy) {
List<Supplier> suppliers = supplierJdbc.selectSuppliers("%%");
DefaultComboBoxModel defaultComboBoxModel = new DefaultComboBoxModel();
suppliers.forEach(supplier -> {
defaultComboBoxModel.addElement(new ComboItems(supplier.getId(), supplier.getName()));
});
formBuy.getjComboBoxSupplier().setModel(defaultComboBoxModel);
}
Show to DefautTableModel
private void loadTable(DefaultTableModel defaultTableModel, FormBuy formBuy) {
defaultTableModel.getDataVector().removeAllElements();
defaultTableModel.fireTableDataChanged();
List<ResponseListTableBuy> buys = buyJdbc.selectBuys("%" + formBuy.getjTextFieldSearch().getText() + "%");
Object[] objects = new Object[10];
for (ResponseListTableBuy buy : buys) {
objects[0] = buy.getId();
objects[1] = buy.getSupplier().getName();
objects[2] = buy.getCategory().getName();
objects[3] = buy.getItem().getName();
objects[4] = buy.getUnit().getName();
objects[5] = buy.getCountItem();
objects[6] = buy.getBuyPrice();
objects[7] = new BigDecimal(buy.getCountItem() * buy.getBuyPrice().intValue());
objects[8] = buy.getSellPrice();
objects[9] = buy.getDate();
defaultTableModel.addRow(objects);
}
}
so far it's still fine!. I got an error when I put this code
get code from table to combobox
formBuy.getjComboBoxSupplier().getModel().setSelectedItem(defaultTableModel.getValueAt(formBuy.getjTableBuy().getSelectedRow(), 1).toString());
and put this an error
System.out.println(((ComboItems) formBuy.getjComboBoxSupplier().getModel().getSelectedItem()).getKey());
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to aliimron.combobox.ComboItems
at aliimron.controller.ControllerBuy$1.mouseClicked(ControllerBuy.java:97)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
at java.awt.Component.processMouseEvent(Component.java:6542)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6304)
at java.awt.Container.processEvent(Container.java:2239)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2297)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4544)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
at java.awt.Container.dispatchEventImpl(Container.java:2283)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
at java.awt.EventQueue$4.run(EventQueue.java:733)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
so the flow is from comboitem to combobox to table and back from table to combobox. how do you get it from the table back into the combobox
So, you build your model using ...
This is good, you're encapsulating the information into an object, but, then you use
Wait, that's a lot, lets see if we can clean it up a bit...
So, basically, you're trying to use a
Stringto set the value of the model, but the model is expecting an instance ofComboItems...So, you have a couple of choices...
#1. You could...
Supply the
TableModelwith aComboItemsand then use a custom cell renderer to render it, then you could use something like...to set the selected combo box item.
For my money, this is the more "correct" approach, see
for more details
#2. You could...
Look up the item in the model directly, based on the
Stringfrom the table.Something maybe like this...
But to be honest, you're better off with option #1
Extensions...
One of things I might be focusing on, is trying to tie your data together, in a more meaningful way, so you don't need to do these "side lookups", for example, your data seems to be expressible as a key/value pair, lets start there...
Okay, but how's that going to help you? Well, now you can narrow the constraints to actual data types, for example...
The neat thing is, this is still usable anywhere you need
KeyValueExpressible, but can provide a finer level constraint when you need theSupplier, sweetNow you could construct you combobox using something like...
nb:
DefaultComboBoxModel<KeyValueExpressible>could also beDefaultComboBoxModel<Supplier>, butDefaultComboBoxModel<KeyValueExpressible>allows us to do cool magic like...which can then be applied using something like...
Which is nice a reusable, sweet.
Now, we can start extending the concept, for example, assuming we have something like...
Then we can encapsulate the
TableModel, for example...And then....
more magic !
You apply it using something like...
And suddenly, every column you said was a
KeyValueExpressible.classwill be rendered through this renderer! Sweet!We've encapsulated the data in the model; we've got the view to render the data the way want it to and when needed...we can do...
nb: You could make the
BuyTableModeldo the casting for you, but I think I might have already blown your mindA neat little trick...
You might find yourself in a situation where you have two instances
KeyValueExpressiblerepresenting the same data (you could use a common factory to manage it, but that's another topic).In those cases, you need some way to tell that those two instances are the same, this is where
equalsandhashCodecome in, for example...Now, any two instances with the same
keyandvaluewill be equal, sweet!Now, if you prefer to tighten the constraint a little more, you could also do something like...
Now only two
Suppliers with the same key/value will be equal, sweet!