1

i have two jframes in my app, Addnmember.java and Frame1.java Frame1 is the main Jframe,

enter image description here

i want when someone presses the Add button, the Addmember will be shown, i have done this with this code, and every thing is fine:

    AddB.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            frame.dispose();
            AddMember addmem = new AddMember();
            addmem.setVisible(true);
        }
    });

But in the new Jframe, i want to come back again when Done Bt is selected, i have done this whit this code:

btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            //dispose();
            Frame1 addmem = new Frame1();
            addmem.setVisible(true);
        }
    });

but this doesent work! mean the Jframe Loads but there is no content in it! look:

enter image description here

why this happens? here is my codes:

AddMember.java:

public class AddMember extends JFrame {
private JFrame frame;
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
private JTextField textField_6;
private JButton btnNewButton;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                AddMember frame = new AddMember();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public AddMember() {
    setTitle("\u0627\u0641\u0632\u0648\u062F\u0646 \u0639\u0636\u0648 \u062C\u062F\u06CC\u062F");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 331);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel label = new JLabel("\u0646\u0627\u0645:");
    label.setFont(new Font("Tahoma", Font.BOLD, 11));
    label.setBounds(332, 60, 46, 14);
    contentPane.add(label);

    textField = new JTextField();
    textField.setToolTipText("\u0646\u0627\u0645");
    textField.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent arg0) {
            textField.setText("");
        }
    });
    textField.setHorizontalAlignment(SwingConstants.RIGHT);
    textField.setText("\u0646\u0627\u0645");
    textField.setBounds(236, 57, 86, 20);
    contentPane.add(textField);
    textField.setColumns(10);

    JLabel label_1 = new JLabel("\u0634.\u062F\u0627\u0646\u0634\u062C\u0648\u06CC\u06CC:");
    label_1.setFont(new Font("Tahoma", Font.BOLD, 11));
    label_1.setBounds(328, 103, 85, 14);
    contentPane.add(label_1);

    textField_1 = new JTextField();
    textField_1.setToolTipText("\u0634\u0645\u0627\u0631\u0647 \u062F\u0627\u0646\u0634\u062C\u0648\u06CC\u06CC");
    textField_1.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            textField_1.setText("");
        }
    });
    textField_1.setText("\u0634\u0645\u0627\u0631\u0647 \u062F\u0627\u0646\u0634\u062C\u0648\u06CC\u06CC");
    textField_1.setHorizontalAlignment(SwingConstants.RIGHT);
    textField_1.setColumns(10);
    textField_1.setBounds(236, 100, 86, 20);
    contentPane.add(textField_1);

    JLabel label_2 = new JLabel("\u0631\u0634\u062A\u0647:");
    label_2.setFont(new Font("Tahoma", Font.BOLD, 11));
    label_2.setBounds(332, 145, 46, 14);
    contentPane.add(label_2);

    textField_2 = new JTextField();
    textField_2.setToolTipText("\u0631\u0634\u062A\u0647 \u062A\u062D\u0635\u06CC\u0644\u06CC");
    textField_2.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
        textField_2.setText("");
        }
    });
    textField_2.setText("\u0631\u0634\u062A\u0647 \u062A\u062D\u0635\u06CC\u0644\u06CC");
    textField_2.setHorizontalAlignment(SwingConstants.RIGHT);
    textField_2.setColumns(10);
    textField_2.setBounds(236, 142, 86, 20);
    contentPane.add(textField_2);

    JLabel label_3 = new JLabel("\u0646\u0627\u0645 \u062E\u0627\u0646\u0648\u0627\u062F\u06AF\u06CC:");
    label_3.setFont(new Font("Tahoma", Font.BOLD, 11));
    label_3.setBounds(144, 61, 82, 14);
    contentPane.add(label_3);

    textField_3 = new JTextField();
    textField_3.setToolTipText("\u0646\u0627\u0645 \u062E\u0627\u0646\u0648\u0627\u062F\u06AF\u06CC");
    textField_3.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            textField_3.setText("");
        }
    });
    textField_3.setText("\u0646\u0627\u0645 \u062E\u0627\u0646\u0648\u0627\u062F\u06AF\u06CC");
    textField_3.setHorizontalAlignment(SwingConstants.RIGHT);
    textField_3.setColumns(10);
    textField_3.setBounds(48, 58, 86, 20);
    contentPane.add(textField_3);

    JLabel label_4 = new JLabel("\u0634. \u062A\u0645\u0627\u0633:");
    label_4.setFont(new Font("Tahoma", Font.BOLD, 11));
    label_4.setBounds(144, 104, 69, 14);
    contentPane.add(label_4);

    textField_4 = new JTextField();
    textField_4.setToolTipText("\u0634\u0645\u0627\u0631\u0647 \u062A\u0645\u0627\u0633");
    textField_4.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            textField_4.setText("");
        }
    });
    textField_4.setText("\u0634\u0645\u0627\u0631\u0647 \u062A\u0645\u0627\u0633");
    textField_4.setHorizontalAlignment(SwingConstants.RIGHT);
    textField_4.setColumns(10);
    textField_4.setBounds(48, 101, 86, 20);
    contentPane.add(textField_4);

    textField_5 = new JTextField();
    textField_5.setToolTipText("\u0633\u0627\u0644 \u0648\u0631\u0648\u062F");
    textField_5.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
        textField_5.setText("");
        }
    });
    textField_5.setText("\u0633\u0627\u0644 \u0648\u0631\u0648\u062F");
    textField_5.setHorizontalAlignment(SwingConstants.RIGHT);
    textField_5.setColumns(10);
    textField_5.setBounds(48, 140, 86, 20);
    contentPane.add(textField_5);

    JLabel label_5 = new JLabel("\u0648\u0631\u0648\u062F\u06CC:");
    label_5.setFont(new Font("Tahoma", Font.BOLD, 11));
    label_5.setBounds(144, 143, 46, 14);
    contentPane.add(label_5);

    textField_6 = new JTextField();
    textField_6.setToolTipText("\u0636\u0631\u0648\u0631\u06CC \u0646\u06CC\u0633\u062A");
    textField_6.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            textField_6.setText("");
        }
    });
    textField_6.setHorizontalAlignment(SwingConstants.RIGHT);
    textField_6.setText("\u0636\u0631\u0648\u0631\u06CC \u0646\u06CC\u0633\u062A");
    textField_6.setColumns(10);
    textField_6.setBounds(131, 187, 86, 20);
    contentPane.add(textField_6);

    JLabel label_6 = new JLabel("\u0627\u06CC\u0645\u06CC\u0644:");
    label_6.setFont(new Font("Tahoma", Font.BOLD, 11));
    label_6.setBounds(227, 190, 46, 14);
    contentPane.add(label_6);

    btnNewButton = new JButton("Done");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            //dispose();
            Frame1 addmem = new Frame1();
            addmem.setVisible(true);
        }
    });
    btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 11));
    btnNewButton.setBounds(171, 227, 89, 23);
    contentPane.add(btnNewButton);
}
private static void addPopup(Component component, final JPopupMenu popup) {
    component.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            if (e.isPopupTrigger()) {
                showMenu(e);
            }
        }
        public void mouseReleased(MouseEvent e) {
            if (e.isPopupTrigger()) {
                showMenu(e);
            }
        }
        private void showMenu(MouseEvent e) {
            popup.show(e.getComponent(), e.getX(), e.getY());
        }
    });
}
}

And Frame1.java:

public class Frame1 extends JFrame {
Connection connection = null;
private JFrame frame;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Frame1 window = new Frame1();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */

private JTable table;
public Frame1() {
    JOptionPane.showMessageDialog(null, "sazande :|");
    this.initialize();
    connection = DataBConect.dbConnect();
    try {
        String query = "select * from Users";
        PreparedStatement pst = connection.prepareStatement(query);
        ResultSet rs = pst.executeQuery();
        table.setModel(DbUtils.resultSetToTableModel(rs));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    JOptionPane.showMessageDialog(null, "intialize :|");
    frame.setTitle("App");
    frame.setBounds(100, 100, 725, 465);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JButton AddB = new JButton("Add");
    AddB.setBackground(Color.WHITE);
    AddB.setFont(new Font("Tahoma", Font.BOLD, 11));
    AddB.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            frame.dispose();
            AddMember addmem = new AddMember();
            addmem.setVisible(true);
        }
    });
    AddB.setBounds(302, 392, 135, 23);
    frame.getContentPane().add(AddB);

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBounds(22, 26, 658, 359);
    frame.getContentPane().add(scrollPane);

    table = new JTable();
    scrollPane.setViewportView(table);



}

}

thanks.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Alireza Pir
  • 878
  • 1
  • 16
  • 41
  • 1
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Jan 18 '15 at 22:00
  • The `initialize()` method of Frame1 is not adding components to the Frame1 instance. It is adding them to a new JFrame. – VGR Jan 19 '15 at 06:12

1 Answers1

2

As to why your second Frame1 shows no data, I'm not 100% sure, but perhaps it has something to do with your failure to close your database connection. Regardless, it seems wasteful and even potentially dangerous to want to gather the database data twice when you've already obtained it once, and to create your Frame1 GUI twice when you've already created it once.

You should re-design your GUI. The main JFrame GUI should remain visible, and the Frame1 window should be a modal JDialog. As a modal dialog, it will prevent users from being able to interact with the parent JFrame until the dialog is no longer visible.

Note that to get started requires only a few changes:

in Frame1.java

  AddB.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent arg0) {
        // frame.dispose();
        AddMember addmem = new AddMember(Frame1.this); // !!
        addmem.setVisible(true);
     }
  });


in AddMember.java

class AddMember extends JDialog {

public AddMember(JFrame frame) {
   super(frame, "", ModalityType.APPLICATION_MODAL);
   setTitle("\u0627\u0641\u0632\u0648\u062F\u0646 \u0639\u0636\u0648 \u062C\u062F\u06CC\u062F");
   // !! setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  btnNewButton.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent arg0) {
        // dispose();
        dispose(); // !!
        // !! Frame1 addmem = new Frame1();
        // addmem.setVisible(true);
     }
  });

Also, you'll want to avoid null layouts and setBounds(...) as this will lead to very rigid, hard to debug and improve programs.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373