so basically I'm making Swing app in Java and i have list where user can add item by typing into text field, and button(when user click that button, window dispose and open's up new window). On new window i have just button that will drive me back into first window, but data inserted into list delete. Is there any way to avoid that(i know there is, but if someone can explain me how it would be great :) ).
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class first extends JFrame {
public static final long serialVersionUID = 1L;
private JFrame frame;
private JButton button, button2;
private JComboBox box;
private JScrollPane scroll;
private DefaultListModel defListModel = new DefaultListModel();
private JList list;
private JPanel panel;
private JTextField field;
public First() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception e) {
e.printStackTrace();
}
frame = new JFrame("Hello Eclipse");
panel = new JPanel();
field = new JTextField("", 25);
panel.add(field);
button = new JButton("Click to add to the list");
button.setFocusPainted(false);
button.setToolTipText("Hovered Over ME");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button){
String text = field.getText();
defListModel.addElement(text);
JOptionPane.showMessageDialog(frame, "Added to the list", "Done", JOptionPane.INFORMATION_MESSAGE);
}
}
});
panel.add(button);
button2 = new JButton("New Window");
button2.setFocusPainted(false);
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button2){
NewWindow window = new NewWindow();
frame.setVisible(false);
}
}
});
panel.add(button2);
String[] movies = {};
for(String movie : movies){
defListModel.addElement(movie);
}
list = new JList(defListModel);
list.setFixedCellWidth(150);
list.setFixedCellHeight(30);
scroll = new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
panel.add(scroll);
frame.add(panel);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.setLocationRelativeTo(null);
}
public static void main(String[] args) {
new First();
}
}
Thanks in advance!