-1

I'm new in Java, just want to ask a simple question I have my first form and a text field, I input a text there the once I click the button a new form will come out and the Text in the text Field will be come a Label the the new form

I try this code but it doesn't work

public class Data extends javax.swing.JFrame {

  public Data() {
    initComponents();

    FrmLogIn f = new FrmLogIn();
    User.setText(f.UName.getText());
    User.setVisible(true);
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
kelvzy
  • 913
  • 2
  • 12
  • 19
  • 1
    have you included event listeners? – exexzian Feb 12 '13 at 04:42
  • 1
    Swing? For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Feb 12 '13 at 04:45
  • I just need to get the text in the other form in the textbox – kelvzy Feb 12 '13 at 04:51
  • 1) Again, post an SSCCE. That code would not compile. 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) 3) *"doesn't work"* Is as useful in helping us to diagnose the problem as a screen door is on the ISS. What did you expect to happen? What happened instead? – Andrew Thompson Feb 12 '13 at 05:00
  • for example.. Log In form when I log-in a new form will open and say, "Welcome, Username" – kelvzy Feb 12 '13 at 05:02

2 Answers2

2

There are many ways you could tackle this problem. You could create your own JDialog to handle the input requirements (and I probably would in most cases), but if you're after something a little simpler, JOptionPane provides a ready made dialog which is highly configurable.

enter image description hereenter image description hereenter image description here

There's, of course, nothing stopping you from mixing the two concpets (customized JDialog and JOptionPane)

public class TestLogin01 {

    public static void main(String[] args) {
        new TestLogin01();
    }

    public TestLogin01() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception ex) {
                }


                LoginPane loginPane = new LoginPane();

                String userName = null;
                boolean validUser = false;
                int result = JOptionPane.CANCEL_OPTION;
                do {
                    result = JOptionPane.showConfirmDialog(null, loginPane, "Login", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
                    switch (result) {
                        case JOptionPane.OK_OPTION:
                            // Verify user details..
                            userName = loginPane.getUserName();
                            char[] password = loginPane.getPassword();

                            // Simple random test...
                            validUser = ((int) (Math.round(Math.random() * 1))) == 0 ? true : false;
                            if (!validUser) {
                                JOptionPane.showMessageDialog(null, "Inavlid username/password", "Error", JOptionPane.ERROR_MESSAGE);
                            }
                            break;
                    }
                } while (!validUser && result != JOptionPane.CANCEL_OPTION);

                if (result == JOptionPane.OK_OPTION) {
                    if (validUser) {
                        JOptionPane.showMessageDialog(null, "Welcome back valued user " + userName, "Welcome", JOptionPane.INFORMATION_MESSAGE);
                    }
                }
            }
        });
    }

    public class LoginPane extends JPanel {

        private JTextField userName;
        private JPasswordField passwordField;

        public LoginPane() {
            userName = new JTextField(10);
            passwordField = new JPasswordField(10);

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.WEST;

            add(new JLabel("User name: "), gbc);
            gbc.gridy++;
            add(new JLabel("Password: "), gbc);

            gbc.gridy = 0;
            gbc.gridx++;
            gbc.fill = GridBagConstraints.HORIZONTAL;

            add(userName, gbc);
            gbc.gridy++;
            add(passwordField, gbc);
        }

        public String getUserName() {
            return userName.getText();
        }

        public char[] getPassword() {
            return passwordField.getPassword();
        }
    }
}

You might like to have a read through How to create GUIs with Swing and How to use dialogs for more information.

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

seeing your code, it seems that you have not included event handling part(and you need to include that to get your work done)

as an brief introduction you need to do this in your code:

button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                FrmLogIn f = new FrmLogIn();
                            User.setText(f.UName.getText());
                            f.setVisible(true);
                }
 }

Note: assuming User is an JLabel and UName an JTextField
and FrmLogIn has extended JFrame and you have set required fields like layout and size of the JFrame

for more info about event-handling look up here

Edit 2:

sample code snippet - you want something like this ( its just a rough one to give you an idea how to move ahead wtih)

edit 3
as commented by @madProgrammer - replaced null layout by FlowLayout

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.*;
import javax.swing.JTextField;


class FrmLogIn extends JFrame{
    JLabel User;

    public FrmLogIn()  {
        setLayout(new FlowLayout());
        setSize(200,200);
        User = new JLabel("");
       // User.setBounds(20,30,100,40);
        add(User);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

}

class ForTest extends JFrame{
   JButton enter;
   JTextField UName;

    public ForTest()  {
     setLayout(new FlowLayout());
     setSize(300,300);

     enter = new JButton("enter");
     //enter.setBounds(20,20,100,30);
     UName = new JTextField();
     //UName.setBounds(40,80,60,30);
     add(UName);
     add(enter);
     setVisible(true);
     enter.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    FrmLogIn f = new FrmLogIn();
                                f.User.setText(UName.getText());
                                f.setVisible(true);
                                setVisible(false);
                    }
     }); 

     setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
      new ForTest();
    }
}
exexzian
  • 7,782
  • 6
  • 41
  • 52