How to play the data of a Jlist in another Jlist that is in another Jframe?

1

I tried several ways, but I could not find a solution.    When I click the save button. I want it to throw the data from this Jlist to the other Jlist that is in the Other Jframe. "I could not activate the code block, sorry."

//Método para Adicionar os itens na Jlist

private void btAdicionarGastoActionPerformed(java.awt.event.ActionEvent evt) {                                                 
        listGasto.setModel(item);
        item.addElement(txtNomeGasto.getText() + ": " + txtValorGasto.getText() + " " + "Data: " +txtData.getText());
        txtNomeGasto.setText("");
        txtValorGasto.setText("R$ ");


    } 
    private void 
    btSalvarGastoActionPerformed(java.awt.event.ActionEvent evt) {                                              
            int k=0;
            k = listGasto.getFirstVisibleIndex();
            listGasto.setModel(item);
            receita.setItems((ArrayList<String>) item.getElementAt(k));
            this.hide();
        }                                             

           //Codigo no outro Jframe
            private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {                                         
            frm = new frmGastos();
            gastos = new DefaultListModel();

            listDadosGasto.setModel(gastos);
            gastos.addElement(frm.receita.getItems());
            listDadosGasto.repaint();
        }                          

Can you help me?

Here is a Google drive link for a program Example: drive.google.com/open?id=0B3LW2w_eQoq4V3BhV0hiQ1o1dnM

    
asked by anonymous 07.11.2016 / 12:27

2 answers

1

If you want to copy exactly all items from one Jlist from one window to one JList of another, just pass ListModel from first to second, as the window parameter. Since your code is not reproducible, look at this example of passing values between windows:

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
import javax.swing.SwingUtilities;

public class JListTest extends JFrame {

    public void createAndShowGUI() {

        String[] selections = { "green", "red", "orange", "dark blue" };
        JList<String> list = new JList(selections);

        setLayout(new BorderLayout());
        add(new JScrollPane(list),BorderLayout.NORTH);

        JButton btn = new JButton("Salvar");
        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                //inicio a segunda janela e passo o
                // model como parametro para a segunda
                SecondList frame = new SecondList();
                frame.createAndShowGUI(list.getModel());

            }
        });
        add(btn, BorderLayout.SOUTH);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new JListTest().createAndShowGUI();

            }
        });
    }
}

class SecondList extends JFrame{

    public void createAndShowGUI(ListModel model) {

        //recebo o model e ja instancio a jlist com ele 
        JList<String> list = new JList(model);
        list.setModel(model);

        setLayout(new FlowLayout());
        add(new JScrollPane(list));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new JListTest().createAndShowGUI();

            }
        });
    }
}

See working:

    
07.11.2016 / 13:33
0

Using two frames, one with a list and a button that I will call reference. First I create the list with the items:

DefaultListModel model = new DefaultListModel();

jListReferencia.setModel(model);
model.addElement("Item 1");
model.addElement("Item 2");
model.addElement("Item 3");

Then I create and instantiate the second JFrame for a private variable:

private JFrameDestino jFrameDestino;

...

jFrameDestino = new JFrameDestino();
jFrameDestino.setVisible(true);

Soon after I create the public method in JFrameDestino :

public void copiarItensSelecionados(JList jListReferencia) {
  DefaultListModel list = (DefaultListModel) jListDestino.getModel();

  for (Object sel : jListReferencia.getSelectedValuesList()) {
    if (list.indexOf(sel) == -1) {
      list.addElement(sel);
    }
  }
}

And in the click of the button located in JFrameReferencia I make the following call:

private void jButtonCopiarActionPerformed(java.awt.event.ActionEvent evt) {                                         
  jFrameDestino.copiarItensSelecionados(jListReferencia);
}
    
07.11.2016 / 13:18