Como fazer isso?

6 respostas
ken420

Alguem pode me ajuda em uma coisa creio que simples? eu tenho um combobox que me retorna 3 string, “5%”,“10%” e “15%”,
bom criei um Object e pego o objeto selecionado do meu combo so que ai vem a minha duvida, preciso fazer um if para que SE o objeto selecionado for
5% ele der system.out.println(“5”); se o selecionado for 10% idem, so que da maneira que fis nao entra no laco, sou iniciante e creio estar fazendo algo errado alguem com um pouco de experiencia pode me da uma maozinha? vlw

Object salario = jcb_salario.getSelectedItem(); if(salario.equals("5")){ System.out.println("5"); }

6 Respostas

Fernando_Generoso_da

melhorando:

String salario = String.valueOf(jcb_salario.getSelectedItem());  
                if(salario.substring(0,salario.indexOf("%")).equals("5")){  
                    System.out.println("5");  
             }  else if.....

Não tive tempo de testar…mas essa é idéia…vê aí se funciona…

Fernando

Fernando_Generoso_da

melhorando:

String salario = String.valueOf(jcb_salario.getSelectedItem());  
                if(salario.substring(0,salario.indexOf("%")).equals("5")){  
                    System.out.println("5");  
             }  else if.....

Não tive tempo de testar…mas essa é idéia…vê aí se funciona…

Fernando

Fernando_Generoso_da

resposta repetida… :oops:

fantomas

Faz um teste com este aqui ó:

String salario = (String)jcb_salario.getSelectedItem(); // Alterei aqui if(salario.equals("5")){ System.out.println("5"); }

flws

fantomas

Este código aqui eu testei e funcionou!

public class Application extends JFrame {

    JComboBox cb = new JComboBox(new String[]{"5", "8", "20"});

    public Application() {
        this.initialize();
    }

    public void startUp() {
        super.setVisible(true);
    }

    private void initialize() {
        super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        JButton bt = new JButton("Press me!");
        bt.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                Application.this.showMessage();
            }
        });

        super.getContentPane().add(cb, BorderLayout.CENTER);
        super.getContentPane().add(bt, BorderLayout.SOUTH);
        super.pack();
        super.setLocationRelativeTo(null);

    }

    private void showMessage() {
        // O cast para string resolveu o problema.
        String salario = (String) cb.getSelectedItem();

        if (salario.equals("5")) {
            System.out.println("Ok! valor escolhido é igual a 5");
        } else {
            System.out.println("O valor escolhido é >> "+salario);
        }
    }

    public static void main(String[] args) {
        Application app = new Application();
        app.startUp();
    }
}

flws

ken420

vlw brother!

Criado 21 de agosto de 2009
Ultima resposta 24 de ago. de 2009
Respostas 6
Participantes 3