0

In Class UserInterface I am setting a String (filePath) in JTextField after clicking on the browser button. Now I want to close my window and return the String stored in JTextField to the MainClass that called UserInterface

This is the MainClass

public class MainClass {

public void mainClass(String IDEFilePath) throws IOException
{
    UserInterface userInterface = new UserInterface();
    String filePath = userInterface.createInterface();

    System.out.println(filePath);
 }
 }

This is the UserInterface Class

package com.mycompany.prototype2;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.*;

public class UserInterface {

JButton browseButton;
JTextField pathField;
JFrame frame;
String IdeFilePath;
JPanel panel, topPanel, bottomPanel;

UserInterface()
{
    // Creating instance of JFrame
    frame = new JFrame("Convert IDEJUnit to WebDriver");

    // Setting the width and height of frame
    frame.setSize(350, 200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    panel = new JPanel(new BorderLayout());

    topPanel = new JPanel(new BorderLayout());
    topPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    bottomPanel = new JPanel(new BorderLayout());
    bottomPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

    panel.add(topPanel, BorderLayout.NORTH);
    panel.add(bottomPanel, BorderLayout.SOUTH);
}



public String createInterface() {    

    //Top Panel
    JLabel IDEFilePathLabel = new JLabel("IDE File Path", JLabel.CENTER);
    pathField = new JTextField(10);


    browseButton = new JButton("Browse");
    browseButton.addActionListener(new ActionListener() { 
        @Override
        public void actionPerformed(ActionEvent e)
        {
            String defaultDirectoryPath = "C:\\Users\\Parul\\Desktop";
            JFileChooser chooser = new JFileChooser(defaultDirectoryPath);
            int returnVal = chooser.showOpenDialog(frame);
            chooser.setDialogTitle("Select Location");
            if (returnVal == JFileChooser.APPROVE_OPTION) 
            {
                java.io.File file = chooser.getSelectedFile();
                pathField.setText(chooser.getSelectedFile().toString());
                IdeFilePath = pathField.getText();
            } 
        }
    });



    topPanel.add(IDEFilePathLabel, BorderLayout.WEST);
    topPanel.add(pathField, BorderLayout.CENTER);
    topPanel.add(browseButton, BorderLayout.EAST);


    //Bottom Panel
    JButton okButton = new JButton("OK");
    bottomPanel.add(okButton, BorderLayout.CENTER);

    okButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            frame.dispose();
        }
    });

    frame.add(panel);
    frame.setVisible(true);

    return IdeFilePath ;
}


}
parul71625
  • 193
  • 2
  • 4
  • 14

1 Answers1

0

Add the flollowing code to you MainClass, and make sure to make filePath static,

public static void setString(String text)
{
    filePath = text;
}

and call this method from UserInterface like so:

MainClass.setText(pathField.getText());

before the line:

frame.dispose();
Omar Ayala
  • 154
  • 3
  • 11