How to create a combo with 2 values?

Asked

Viewed 61 times

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

  • Had to be letter in this case

  • Then you have to create a POJO class to represent the items

  • Please read these links and resolve your problem: https://answall.com/a/204149/28595 and https://answall.com/a/105753/28595

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

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

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

  • @Article the data comes from addItem(); which is already in the example above

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

  • 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

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

Show 6 more comments

1 answer

0


One possibility, in my view, would be to create a matrix. In a very "simplistic" way, what you will do is in the first bracket [] store the "key", in your case "A". And in the second bracket [], store the items that will be displayed in the JComboBox, "Item 01".

import java.awt.Dimension;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Exibir extends JFrame {

    private Combo cb = new Combo(new String[][]{{"A", "Item 01"}, {"B", "Item 02"}, {"C", "Item 03"}});

    public static void main(String[] args) {
        Exibir e = new Exibir();
        e.setVisible(true);
    }

    public Exibir() {
        add(painel());
        pack();
        vericaKey();
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private JPanel painel() {
        JPanel painel = new JPanel();
        painel.add(cb);
        return painel;
    }

    private void vericaKey() {
        cb.addItemListener(e -> {
            System.out.println("Selecionou: " + cb.getValor());
        });
    }
}

class Combo extends JComboBox {

    private String[][] dados;

    public Combo(String[][] dados) {
        this.dados = dados;
        setPreferredSize(new Dimension(100, 22));
        preencher();        
    }

    public void preencher() {
        addItem("<Selecione>");
        for (int i = 0; i < dados.length; i++) {
            addItem(dados[i][1]);
        }
    }

    public Object getValor() {
        return dados[getSelectedIndex() - 1][0];
    }
}
  • It’s a solution, but it sounds kind of like you’re going to do it the right way by creating a representation of the object of the bank.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.