I explain better in the image. But it is as follows. I have a bt to add fruit. whenever a fruit is added, a new screen opens for it. but the problem comes: ALL ADDED FRUITS MUST APPEAR ON ALL SCREENS, BUT ALL FRUITS, ONLY APPEARS ON THE LAST ADDED SCREEN.
publicclassJFrameEnviaextendsJFrame{staticDefaultListModelmodel;staticJListjListReferencia;staticJScrollPanescrolList;privateJButtonbtadicionar;privateJPanelcontentPane;staticJTextFieldareaNomeFruta;intk=0;publicJFrameEnvia(){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);
}
}