How to create a combo with 2 values? [closed]

-2

I'm completely lost, I'm trying to create a combo box, where I can display "a value / text" plus, that by giving a getValor in it, I can get another value. For example, I wanted to display, "Item 01", more when picking up the value, "item 01", wanted it to correspond to "A" for example. It's that I want to show a way to the user, and just save a 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;
    }
}
    
asked by anonymous 18.03.2018 / 22:14

1 answer

0

One possibility, in my view, would be to create an array. In a very "simplistic" way, what you are going to 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 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];
    }
}
    
25.03.2018 / 22:48