-1

When I try to add some function to get data from jtext field under public void actionPerformed(ActionEvent arg0) My GUI goes blank I have attached the images below I can't figure out what's wrong with it.

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JLabel;
    import javax.swing.JRadioButton;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;

    public class Register extends JFrame {

        private JPanel contentPane;
        private JTextField textField;
        private JTextField textField_1;
        private JTextField textField_2;
        private JTextField textField_3;
        private JTextField textField_4;
        private JLabel lblNewLabel;

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

        /**
         * Create the frame.
         */
        public Register() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 450, 300);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            setContentPane(contentPane);
            contentPane.setLayout(null);

            JLabel lblName = new JLabel("Name");
            lblName.setBounds(56, 56, 56, 16);
            contentPane.add(lblName);

            JLabel lblUsername = new JLabel("Username");
            lblUsername.setBounds(56, 85, 77, 16);
            contentPane.add(lblUsername);

            JLabel lblPassword = new JLabel("Password");
            lblPassword.setBounds(56, 114, 56, 16);
            contentPane.add(lblPassword);

            JLabel lblAge = new JLabel("Age");
            lblAge.setBounds(56, 143, 56, 16);
            contentPane.add(lblAge);

            JLabel lblGender = new JLabel("Gender");
            lblGender.setBounds(56, 172, 56, 16);
            contentPane.add(lblGender);

            textField = new JTextField();
            textField.setBounds(130, 53, 116, 19);
            contentPane.add(textField);
            textField.setColumns(10);

            textField_1 = new JTextField();
            textField_1.setBounds(130, 82, 116, 22);
            contentPane.add(textField_1);
            textField_1.setColumns(10);

            textField_2 = new JTextField();
            textField_2.setBounds(130, 111, 116, 22);
            contentPane.add(textField_2);
            textField_2.setColumns(10);

            textField_3 = new JTextField();
            textField_3.setBounds(130, 140, 116, 22);
            contentPane.add(textField_3);
            textField_3.setColumns(10);

            textField_4 = new JTextField();
            textField_4.setBounds(130, 169, 116, 22);
            contentPane.add(textField_4);
            textField_4.setColumns(10);

            JButton btnSubmit = new JButton("Submit");
            btnSubmit.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent arg0) {



                }
            });
            btnSubmit.setBounds(169, 215, 97, 25);
            contentPane.add(btnSubmit);

            lblNewLabel = new JLabel("New label");
            lblNewLabel.setBounds(330, 56, 56, 16);
            contentPane.add(lblNewLabel);
        }


    }

https://i.stack.imgur.com/CU3ax.png

EDIT 1: Added String name to action just to get name.

JButton btnSubmit = new JButton("Submit");
        btnSubmit.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {

                String Name;

            }
        });

and This happened

GUI went blank:

https://i.stack.imgur.com/02Mxe.png

camickr
  • 321,443
  • 19
  • 166
  • 288
Saubhagya
  • 155
  • 12
  • 2
    (1-) You don't even have any code in your ActionListener. Post a proper [mcve] that demonstrates the problem. – camickr Jun 04 '17 at 21:14
  • @camickr That's what the question is about if i even define String name; in my action listener it the GUI goes blank. – Saubhagya Jun 04 '17 at 21:17
  • `if i even define String name; in my action listener it the GUI goes blank.` - There is no reason for the GUI to go blank if you add code to the ActionListener to just use the getText() method on a text field. So if you want help then prove it!!! Post the actual code you are using. We can't guess what you are doing. – camickr Jun 04 '17 at 21:21
  • @camickr I just defined the string and it went blanked attached the image. – Saubhagya Jun 04 '17 at 21:31
  • I have no problem. Maybe you have some old class files lying around that are causing a problem. Also try adding something that actually does something to the ActionListener. Something like: `System.out.println( textField.getText() );`. – camickr Jun 04 '17 at 21:39
  • @camickr Same issue – Saubhagya Jun 04 '17 at 21:42
  • 1
    So create a simpler [mcve] that demonstrates the problem. That is create a frame with just a text field and a button. Add the System.out.println() statement from above to your ActionListener. See if this simple class works. If so then delete your old class and start adding back in your other components to this new class. – camickr Jun 04 '17 at 21:47

1 Answers1

1

You're using an absolute layout. Since you can't pack() the window, you need to at least validate() its contents.

Register frame = new Register();
frame.validate();
frame.setVisible(true);

Now you can see the real problem: you picked bounds for lblPassword and lblNewLabel that cut off the text on my computer. You really need to use a layout, maybe like this.

screenshot

Catalina Island
  • 7,027
  • 2
  • 23
  • 42