-1

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!

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Filip785
  • 101
  • 1
  • 3
  • 8
  • 1
    `extends JFrame` and `private JFrame frame;` raises the question of which frame is actually visible and actually doing the work. In general, there is no need to extend from `JFrame`, as you are not adding any additional functionality to the class – MadProgrammer Aug 23 '14 at 00:05

1 Answers1

3

You can use CardLayout to use same space for multiple views.

Hint: Just switch between multiple JPanel when any JButton is clicked.

Read more about How to Use CardLayout


If you want to stick with the current approach then try with WindowListener

frame.addWindowListener(new WindowAdapter() {
    @Override
    public void windowDeactivated(WindowEvent arg0) {
        System.out.println("windowDeactivated");
        // save the data
    }
});

If needed then override windowClosing() method as well.

You can override setVisible() method of the JFrame as well to check for the JFrame's visibility.

See The Use of Multiple JFrames, Good/Bad Practice?


Doubt: Why are using both extending the JFrame class add having an instance of JFrame itself in the class?

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76