How to use a Jcombobox of one class in another?

Asked

Viewed 139 times

1

I want to create two screens, one registers the values of Jcombobox and the other I use the values. I can get the Combobox back but there’s no value.

Class of register

public class Combo extends JFrame{

    private JButton ok,proxima;
    private JTextField texto;
    private JComboBox<String> meucombo;


    public JComboBox getMeucombo() {
        return meucombo;
    }

    Combo() {

        super("ComboBox");
        mCombo();
    }

    private void mCombo() {

        setLayout(new BorderLayout());

        acaoBotao acao = new acaoBotao();
        outroBotao outroBotao = new outroBotao();

        meucombo = new JComboBox<String>();

        proxima = new JButton("proxima");
        proxima.addActionListener(outroBotao);

        ok = new JButton("OK");
        ok.addActionListener(acao);

        texto = new JTextField();

        add(meucombo, BorderLayout.NORTH);
        add(texto, BorderLayout.CENTER);
        add(ok, BorderLayout.SOUTH);
        add(proxima,BorderLayout.EAST);


    }

    private class acaoBotao implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {


            meucombo.addItem(texto.getText());
            texto.setText("");

        }
    }

    private class outroBotao implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {

            Teste teste = new Teste();
            teste.setVisible(true);
        }
    }
}

Other class

public class Teste extends JFrame {

    Teste(){

        super("teste");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(600,600);
        setVisible(true);
        Combo combo = new Combo();
        JComboBox mcombo = combo.getMeucombo();
        JFrame frame = new JFrame();
        frame.setLayout(new FlowLayout());
        add(mcombo);

    }

}

1 answer

1


You can pass the combo as argument in the constructor of the new class:

private class outroBotao implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {

        Teste teste = new Teste(meucombo);
        teste.setVisible(true);
    }

And in the Test class:

public class Teste extends JFrame {

    Teste(JComboBox combo){

        super("teste");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(600,600);
        setVisible(true);
        Combo combo = new Combo();
        JFrame frame = new JFrame();
        frame.setLayout(new FlowLayout());
        add(combo);

    }

}

But I believe that this approach is not good. The component should be building on the same screen that it is part of. What would be more appropriate, I believe, is to somehow persist the combo data and use this data on the new screen, so you don’t have to keep creating the component in one place and adding in another.

  • Thank you for the reply friend, it is only a room activity so do not need bank. I thank you

  • @Rodolfoallesson ah yes, in this case this solution meets well :) If the answer served you can mark it as accepted by clicking on v next to her.

Browser other questions tagged

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