the message is not updated when sent to more than one screen

Asked

Viewed 42 times

0

I explain it better in the image. But here’s the thing. I have a Bt to add fruit. Whenever a fruit is added, a new canvas opens for it. but here’s the problem: ALL FRUITS ADDED MUST APPEAR ON ALL SCREENS, BUT ALL FRUITS ONLY APPEAR ON THE LAST SCREEN ADDED.

inserir a descrição da imagem aqui

public class JFrameEnvia extends JFrame {

static DefaultListModel model;
static JList jListReferencia;
static JScrollPane scrolList;
private JButton btadicionar;
private JPanel contentPane;
static JTextField areaNomeFruta;
int k = 0;

public JFrameEnvia() {
    super("tela add fruta");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 558, 312);
    contentPane = new JPanel();
    contentPane.setLayout(null);
    setContentPane(contentPane);

    btadicionar = new JButton("adiciona");
    btadicionar.setBounds(147, 11, 89, 23);

    btadicionar.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (!areaNomeFruta.getText().equals("")) {
                model.add(k, areaNomeFruta.getText());

                new TelaFruta().setVisible(true);
                k++;
            }
        }
    });

    contentPane.add(btadicionar);

    areaNomeFruta = new JTextField();
    areaNomeFruta.setBounds(10, 12, 103, 20);
    contentPane.add(areaNomeFruta);
    areaNomeFruta.setColumns(10);

    model = new DefaultListModel();
    jListReferencia = new JList(model);

    scrolList = new JScrollPane(jListReferencia);
    scrolList.setBounds(335, 54, 89, 151);
    contentPane.add(scrolList);
}

public static void main(String[] args) {
    new JFrameEnvia().setVisible(true);
    ;
}

public class TelaFruta extends JFrame {

private JPanel contentPane;
static DefaultListModel modelList;
static JList liste;
JScrollPane scrolList;
private JButton btEnviarNomeFruta;

public static void copiarItensSelecionados(JList jListReferencia) {
    modelList = (DefaultListModel) liste.getModel();
    for (Object sel : jListReferencia.getSelectedValuesList()) {
        if (modelList.indexOf(sel) == -1) {
            modelList.addElement(sel);
        }
    }
}

public TelaFruta() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 558, 312);
    contentPane = new JPanel();
    contentPane.setLayout(null);
    setContentPane(contentPane);
    System.out.println("tela");

    modelList = new DefaultListModel();
    liste = new JList(modelList);

    scrolList = new JScrollPane(liste);
    scrolList.setBounds(335, 54, 89, 151);
    contentPane.add(scrolList);

    btEnviarNomeFruta = new JButton("enviar o nome das frutas");
    btEnviarNomeFruta.setBounds(234, 230, 198, 32);

    btEnviarNomeFruta.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            TelaFruta.copiarItensSelecionados(JFrameEnvia.jListReferencia);
        }

    });

    contentPane.add(btEnviarNomeFruta);
}

}

  • 2
  • It is not recommended to have more than one jframe in the application, it is correct to create only one and to create jdialogs subtelas.

  • examples? can you give me

  • Here you have several answers with examples: http://answall.com/questions/tagged/jdialog

  • but if I exchange the following Jframes for Jdialog the information will be dazzled on the screens?

No answers

Browser other questions tagged

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