0

I am creating a Java application where I am going to have a button. When that button is pressed I want a form with fields like username, password and various others to be entered there. In that form there is the Save button which when pressed saves everything to a file and switches back to the first JFrame.

This is how I do it currently. This is the button code for the first JFrame:

public class FirstJFrame extends JFrame {    
    private void newJFrameActionPerformed(java.awt.event.ActionEvent evt) {                                          
        SecondJFrame pan = new SecondJFrame();
        this.setVisible(false);
        pan.setVisible(true);
    }
}

and this is the code for the Save button on the second JFrame:

public class SecondJFrame extends javax.swing.JFrame {
    private void goBackActionPerformed(java.awt.event.ActionEvent evt) {                                       
        SecondJFrame pan = new SecondJFrame();
        this.setVisible(false);
        pan.setVisible(true);
    }
}

This works well enough for me but I have a feeling that I am not doing this right at all and I might create a memory leak since I am creating a new object each time. Is what I am doing correct or is there a better way?

Aki K
  • 1,222
  • 1
  • 27
  • 49

1 Answers1

2

It's not the best way indeed!

If you want to collect some data from user input you can use JOptionPane with the input dialog. Here is a code sample that may show you the way :

import javax.swing.*;
import java.awt.event.*;

public class ShowInputDialog{
    public static void main(String[] args){
        JFrame frame = new JFrame("Input Dialog Box Frame");
        JButton button = new JButton("Show Input Dialog Box");
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae){
                JTextField xField = new JTextField(5);
                JTextField yField = new JTextField(5);

                JPanel myPanel = new JPanel();
                myPanel.add(new JLabel("x:"));
                myPanel.add(xField);
                myPanel.add(Box.createHorizontalStrut(15)); // a spacer
                myPanel.add(new JLabel("y:"));
                myPanel.add(yField);

                int result = JOptionPane.showConfirmDialog(null, myPanel, 
                         "Please Enter X and Y Values", JOptionPane.OK_CANCEL_OPTION);
                if (result == JOptionPane.OK_OPTION) {
                   System.out.println("x value: " + xField.getText());
                   System.out.println("y value: " + yField.getText());
                }
            }
        });
        JPanel panel = new JPanel();
        panel.add(button);
        frame.add(panel);
        frame.setSize(400, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
JajaDrinker
  • 652
  • 6
  • 15
  • I think I get it now. Basically I put a new panel in the `Dialog` instead of creating a new `JFrame` alltogether. Thank you. – Aki K Apr 04 '14 at 17:42
  • indeed. JOptionPane has many interesting dialogs that are fully customisable! – JajaDrinker Apr 04 '14 at 17:44
  • I decided to put the whole panel in a different class to make my code look cleaner but the problem is that I can't access the fields from my JFrame when I press the OK button. I could have another button in the panel called `save` but having extra buttons like that would look bad, is there a way to access the stored values in the JPanel after pressing `OK` ? I already tried `myPanel.xField` but netbeans doesn't let me change the member variables from private to public. – Aki K Apr 04 '14 at 18:15
  • You will have to pass the pannel as a parameter to your new class. it is good to make an other class so your code seems more readable. just pass it to the constructor and you will be able to use it – JajaDrinker Apr 04 '14 at 23:25