-2

Another question for Java... I know it's basic, but I am not pro.

So I have Main.java

public class Main {
public static void main(String[] args) {
    System.setProperty("webdriver.gecko.driver", "C:/geckodriver.exe");
    WebDriver driver = new FirefoxDriver();
    driver.get("https://my.emerchantpay.com/");
    eMerchantPay emp = PageFactory.initElements(driver, eMerchantPay.class);
    SwingUtilities.invokeLater(new Runnable() {
       public void run() {
            JFrame frame = new MainFrame("Please enter your credentials");
            frame.setSize(500, 400);
            //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
       }
    });

    emp.uid(username);
    emp.pwd(password);
    emp.LoginButton.click();

And the string is located in DetailsPanel.java

public class DetailsPanel  extends JPanel{

private static final long serialVersionUID = 1234567891;

private EventListenerList listenerList = new EventListenerList();

public DetailsPanel() {
    Dimension size = getPreferredSize();
    size.width = 250;
    setPreferredSize(size);

    setBorder(BorderFactory.createTitledBorder("Personal Details"));

    JLabel nameLabel = new JLabel("Name: ");
    JLabel passwordLabel = new JLabel("Password: ");

    final JTextField nameField = new JTextField(10);
    final JPasswordField passwordField = new JPasswordField(10);

    final JButton addBtn = new JButton("Submit");

    addBtn.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(java.awt.event.ActionEvent e) {
            String name = nameField.getText();
            String password = passwordField.getText();

            String text = name + ": " + password + "\n";
            JFrame frame = (JFrame) SwingUtilities.getWindowAncestor(addBtn);
            frame.dispose();
            System.out.println (text);
        }
    });

    setLayout(new GridBagLayout());

    GridBagConstraints gc = new GridBagConstraints();

    /// First column ///
    gc.anchor = GridBagConstraints.LINE_END;
    gc.weightx = 0.5;
    gc.weighty = 0.5;

    gc.gridx = 0;
    gc.gridy = 0;
    add(nameLabel, gc);

    gc.gridx = 0;
    gc.gridy = 1;
    add(passwordLabel, gc);

    /// Second column ///
    gc.anchor = GridBagConstraints.LINE_START;

    gc.gridx = 1;
    gc.gridy = 0;
    add(nameField, gc);

    gc.gridx = 1;
    gc.gridy = 1;
    add(passwordField, gc);

    /// Final row ///
    gc.weighty = 10;

    gc.anchor = GridBagConstraints.FIRST_LINE_START;

    gc.gridx = 1;
    gc.gridy = 2;
    add(addBtn, gc);
}

public void fireDetailEvent(DetailEvent event) {
    Object[] listeners = listenerList.getListenerList();

    for (int i=0; i < listeners.length; i += 2) {
        if (listeners[i] == DetailListener.class) {
            ((DetailListener)listeners[i+1]).detailEventOccured(event);
        }
    }
}

public void addDetailsListener(DetailListener listener) {
    listenerList.add(DetailListener.class, listener);
}

public void removeDetailListener(DetailListener listener) {
    listenerList.remove(DetailListener.class, listener);

}

So the strings are located here

String name = nameField.getText();

String password = passwordField.getText();

How do I access these from Main.java? I have to assign the name value to emp.uid(username);

gkubed
  • 1,849
  • 3
  • 32
  • 45
vmvelev
  • 143
  • 14
  • For this name/password part, you may prefer to use a `JDialog`, see the following question : https://stackoverflow.com/questions/6555040/multiple-input-in-joptionpane-showinputdialog – Arnaud Jun 16 '17 at 09:42
  • *"I know it's basic.."* So you should be working on command line apps. till you have the basics sorted. GUIs are an advanced topic. – Andrew Thompson Jun 16 '17 at 10:30
  • @AndrewThompson sure, I know how to use System.out.println(); but I am learning GUI at the moment. Thank you anyways ;) – vmvelev Jun 16 '17 at 10:59
  • 1
    @Berger Thank you mate, will look into it right now! – vmvelev Jun 16 '17 at 10:59

2 Answers2

1

Your fields are package protected, thus they are visible in the same package.

However, you need an instance of your DetailsPanel to access them.

So, where your Main class uses the DetailsPanel, you could use something like this:

DetailsPanel details = new DeatilsPanel();
...
String name = details.nameField.getText();
char[] password = details.passwordField.getPassword();

(For security reasons JPasswordField does have the getText() method to return the password in a String deprecated.)

Usagi Miyamoto
  • 6,196
  • 1
  • 19
  • 33
0

You can create two data members in your DetailsPanel.java file and can can name them as name and password. In your Main.java, you can create an object of DetailsPanel and using the object you can access the values of name and password

Main.java

DetailsPanel obj=new DetailsPanel();
emp.uid(obj.name);
emp.pwd(obj.password);

DetailsPanel.java

class DetailsPanel{
 String name;
 String password;
} 

Hope it helps you to get the basics.

rackdevelop247
  • 86
  • 1
  • 10