How to Throw the dice of a Jlist into another Jlist that is in another Jframe?

Asked

Viewed 852 times

1

I tried it in various ways, but I couldn’t find a solution. When I click the save button. I want him to throw the dice on that Jlist to the other Jlist that’s in the Other Jframe. "I couldn’t 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 help me?

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

2 answers

0

Using two frames, one with a list and a button I will call a 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 instate the second JFrame for a private variable:

private JFrameDestino jFrameDestino;

...

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

Right 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 at the click of the button located on JFrameReferencia I make the following call:

private void jButtonCopiarActionPerformed(java.awt.event.ActionEvent evt) {                                         
  jFrameDestino.copiarItensSelecionados(jListReferencia);
}

0


If the intention is to copy exactly all items of a Jlist from a window to a Jlist of another, just pass the ListModel from first to second, as window parameter. Since your code is not reproducible, see this example of how to pass 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:

inserir a descrição da imagem aqui

  • The code worked, now I’m trying to implement in my program

  • @Mateussilveira tests there, anything warns here.

  • Sorry for the delay. I’m not able to implement. I’ll make an example and send the link to Google Drive for you to see better.

  • https://drive.google.com/open?id=0B3LW2w_eQoq4V3BhV0hiQ1o1dnM

  • @Mateussilveira https://ufile.io/abf14 tests there, but already I say that is not a good solution, you are mixing business logic with canvas, I could hardly find the lists. The editor should be used only to make the window, no logic at all, and then you add it manually, not via drag and drop, so it avoids this scrambling of code that makes it even difficult for yourself.

  • Thanks! It worked, and thanks for the tips

Show 1 more comment

Browser other questions tagged

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