0

I have two swing Frames. Frame one will contain a button.when we click the button we will get another frame which will have the five lables(which are the varibles of a class.) with the textfields beside, and a submit button. user will enter the values and clicks submit butoon. My question is how can retrieve the values from that Frame two when user clicks submit button .i have the code like blelow.

    public class Form extends JFrame implements ActionListener {
    JPanel panel = new JPanel();
    JFrame frame = new JFrame("New frame");
    JPanel panel2 = new JPanel();
    JButton button = new JButton("add");
    JButton button2 = new JButton("Submit");
    JLabel label;
    JTextField textfield;

    public Form() {
        setLayout(new BorderLayout());
        panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
        panel.setPreferredSize(new Dimension(300, 200));
        button.addActionListener(this);
        add(panel, BorderLayout.CENTER);
        add(button, BorderLayout.SOUTH);

    }

    public static void main(String[] a) {
        Form s = new Form();
        s.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        s.pack();
        s.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        dispose();
        panel2.setLayout(new FlowLayout());
        panel2.setPreferredSize(new Dimension(1000, 1000));
        final Field[] fields = Employee.class.getFields();
        for (Field temp : fields) {
            label = new JLabel(temp.getName());
            label.setBounds(20, 50, 100, 20);
            textfield = new JTextField(20);
            textfield.setBounds(140, 50, 100, 20);
            panel2.add(label);
            panel2.add(textfield);
        }
        frame.add(panel2);
        frame.setSize(290, 300);
        frame.setVisible(true);
        button2.setSize(20, 30);
        frame.add(button2, BorderLayout.SOUTH);

        repaint();
        revalidate();

        button2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {

            }
        });

    }

}
yashwanth
  • 1
  • 1
  • 4
  • Actually i dont know how many textFields and how many labels gets generated ,coz i'm generating them dynamically(depends on the no. of properties in a class). So after clicking the submit button , i want the values to be extracted. – yashwanth Oct 31 '14 at 06:34
  • Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Nov 02 '14 at 01:56

1 Answers1

2

Start by taking a look at The Use of Multiple JFrames, Good/Bad Practice?

Instead of using a second frame, you should use a modal dialog, which, when made visible, will halt your programs execution at that point until it's disposed, at which time it will return and you can extract the values you want from it.

See How to Make Dialogs for more details

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366