-2
I am completely lost, I am trying to create a combo box, where I can display "a value/text", more, that when giving a getValue on it, I can get another value. For example, I wanted to display, "Item 01", but when I took the value, "item 01", I wanted it to correspond to "A" for example. Is that I want to show a way for the user, and save only one CHAR in the database.
Is it possible ? I have no idea what to do.
package testes;
import java.awt.Dimension;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Exibir extends JFrame {
private final Combo cb = new Combo(100, 22);
public static void main(String[] args) {
Exibir e = new Exibir();
e.setVisible(true);
}
public Exibir() {
add(painel());
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
vericaKey();
}
private JPanel painel() {
JPanel painel = new JPanel();
painel.add(cb);
cb.addItem("Item 01");
cb.addItem("Item 02");
cb.addItem("Item 03");
return painel;
}
private void vericaKey() {
cb.addItemListener(e -> {
System.out.println("Selecionou: " + cb.getSelectedItem());
});
}
}
class Combo extends JComboBox {
public Combo(int largura, int altura) {
setPreferredSize(new Dimension(100, 22));
}
public Object getValor() {
return getSelectedItem();
}
public Object addItensMap(Map<String, String> map) {
Set<String> keys = map.keySet();
Iterator<String> it = keys.iterator();
while (it.hasNext()) {
String key = it.next();
addItem(key);
}
String key = (String) getSelectedItem();
String customer = map.get(key);
return customer;
}
}
Need to be letter? Naoo can be numeric? If it is just use
getSelectedIndex()
– user28595
Had to be letter in this case
– AlunoOracle
Then you have to create a POJO class to represent the items
– user28595
Please read these links and resolve your problem: https://answall.com/a/204149/28595 and https://answall.com/a/105753/28595
– user28595
Very good example you created, I just don’t think it would be necessary to communicate with the bank. Because there will only be three items, each of which corresponds to a different CHAR, I just wanted to pass this static. When I’m saving something, I just wanted to get a "get" on that char.
– AlunoOracle
There is no other way less complicated. If you want something simple, you can do it according to the links. If you want something complicated, then I don’t know how to help you anymore.
– user28595
By the way, I got an idea here, but you did not show how to fill out this combo, where the data will come from and how will come, without this information it is difficult to post what I thought. Edit the question and provide these details to see if you can really suggest something.
– user28595
@Article the data comes from addItem(); which is already in the example above
– AlunoOracle
In your example there is nothing of CHAR. It is a very superficial example that does not reflect the problem of the question. I suggest trying to edit the example to at least reflect the problem.
– user28595
I expressed myself bad then, the idea, is that in addItem, I pass what I want to show ("Item 01") and another String/Char ("A"), but it does not let
– AlunoOracle
Now I didn’t understand it was nothing what you want to do. You can’t use char as combo dice. It’s only integer. If you want to do with char, you have to follow the links I sent or lock entirely with maps, as you started doing, but the combo will continue with entire Dice.
– user28595