0

I have two frames in my java program. One is for the credential window and another for the progress bar. When I click the login button the progress bar should also start working. This is my code

package Javapkg;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.beans.*;
import java.io.IOException;
import java.util.Map;
import java.util.Random;

public class ProgressBarDemo extends JPanel implements ActionListener,
        PropertyChangeListener {

    private static final long serialVersionUID = 1L;
    JFrame frame;
    JPanel panel;
    JTextField userText;
    JPasswordField passwordText;
    JButton loginButton;
    JLabel userLabel;
    JLabel passwordLabel;
    JButton cancelButton;
    JButton startButton;
    private JProgressBar progressBar;
    // private JButton startButton;
    private JTextArea taskOutput;
    private Task task;

    class Task extends SwingWorker<Void, Void> {
        /*
         * Main task. Executed in background thread.
         */
        public void Credential() {
            JFrame frame = new JFrame();
            frame.setUndecorated(true);
            frame.setBackground(new Color(0, 0, 0, 0));
            frame.setSize(new Dimension(400, 300));
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            panel = new JPanel() {


                private static final long serialVersionUID = 1L;

                @Override
                protected void paintComponent(Graphics g) {
                    super.paintComponent(g);
                    if (g instanceof Graphics2D) {
                        final int R = 220;
                        final int G = 220;
                        final int B = 250;
                        Paint p = new GradientPaint(0.0f, 0.0f, new Color(R, G,
                                B, 0), 0.0f, getHeight(), new Color(R, G, B,
                                255), true);
                        Graphics2D g2d = (Graphics2D) g;
                        g2d.setPaint(p);
                        g2d.fillRect(0, 0, getWidth(), getHeight());
                        Font font = new Font("Serif", Font.PLAIN, 45);
                        g2d.setFont(font);
                        g2d.setColor(Color.lightGray);
                        g2d.drawString("Get Credential", 60, 80);
                    }
                }

            };

            frame.setContentPane(panel);
            frame.setLayout(new FlowLayout());
            frame.placeComponents(panel);

        }

        public void placeComponents(JPanel panel) {

            panel.setLayout(null);

            userLabel = new JLabel("User");
            userLabel.setBounds(40, 100, 80, 25);
            panel.add(userLabel);

            userText = new JTextField(20);
            userText.setBounds(130, 100, 160, 25);
            userText.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                }
            });

            panel.add(userText);

            passwordLabel = new JLabel("Password");
            passwordLabel.setBounds(40, 140, 80, 25);
            panel.add(passwordLabel);

            passwordText = new JPasswordField(20);
            passwordText.setBounds(130, 140, 160, 25);
            panel.add(passwordText);

            loginButton = new JButton("login");
            loginButton.setBounds(100, 180, 80, 25);
            loginButton.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {

                    ProcessBuilder pb = new ProcessBuilder("powershell.exe",
                            "-File", "D:\\MyPowershell\\stage1.ps1");
                    Map<String, String> env = pb.environment();
                    env.put("USER", userText.getText());
                    env.put("PASS", passwordText.getText());
                    System.out.println(userText.getText());
                    System.out.println(passwordText.getText());
                    try {
                        Process process = pb.start();

                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }

                }
            });
            panel.add(loginButton);

            cancelButton = new JButton("cancel");
            cancelButton.setBounds(220, 180, 80, 25);
            cancelButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
            panel.add(cancelButton);

        }
    }

    public Void doInBackground() {
        Random random = new Random();
        int progress = 0;
        // Initialize progress property.
        setProgress(0);
        while (progress < 100) {
            // Sleep for up to one second.
            try {
                Thread.sleep(random.nextInt(1000));
            } catch (InterruptedException ignore) {
            }
            // Make random progress.
            progress += random.nextInt(10);
            setProgress(Math.min(progress, 100));
        }
        return null;
    }

    private void setProgress(int min) {
        // TODO Auto-generated method stub

    }

    /*
     * Executed in event dispatching thread
     */
    public void done() {
        Toolkit.getDefaultToolkit().beep();

        startButton.setEnabled(true);
        // turn off the wait cursor
        taskOutput.append("Done!\n");
    }

    public ProgressBarDemo() {
        super(new BorderLayout());

        // Create the demo's UI.
        //startButton = new JButton("Start");
        //startButton.setActionCommand("start");
        //startButton.addActionListener(this);

        progressBar = new JProgressBar(0, 100);
        progressBar.setValue(0);
        progressBar.setStringPainted(true);

        taskOutput = new JTextArea(5, 20);
        taskOutput.setMargin(new Insets(5, 5, 5, 5));
        taskOutput.setEditable(false);

        JPanel panel = new JPanel();
        //panel.add(startButton);
        panel.add(progressBar);

        add(panel, BorderLayout.PAGE_START);
        add(new JScrollPane(taskOutput), BorderLayout.CENTER);
        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

    }

    /**
     * Invoked when the user presses the start button.
     */
    public void actionPerformed(ActionEvent evt) {
        // startButton.setEnabled(false);
        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        // Instances of javax.swing.SwingWorker are not reusuable, so
        // we create new instances as needed.
        task = new Task();
        task.addPropertyChangeListener(this);
        task.execute();
    }

    /**
     * Invoked when task's progress property changes.
     */
    public void propertyChange(PropertyChangeEvent evt) {
        if ("progress" == evt.getPropertyName()) {
            int progress = (Integer) evt.getNewValue();
            progressBar.setValue(progress);
            taskOutput.append(String.format("Completed %d%% of task.\n",
                    task.getProgress()));
        }
    }

    /**
     * Create the GUI and show it. As with all GUI code, this must run on the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        // Create and set up the window.
        JFrame frame = new JFrame("ProgressBarDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Create and set up the content pane.
        JComponent newContentPane = new ProgressBarDemo();
        newContentPane.setOpaque(true); // content panes must be opaque
        frame.setContentPane(newContentPane);

        // Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        // Schedule a job for the event-dispatching thread:
        // creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();

                Credential gtw = new Credential();

                gtw.setVisible(true);
            }

        });
    }
}

I am getting both frames being displayed. But I am not getting the progressbar running when I press the login button.. Also I am not able to get the values of username and password fields into the powershell script.. My powershell code is given below stage1.ps1:

$username = $env:USER
$password = $env:PASS
$url = "https://www.jabong.com/customer/account/login/"
$ie = New-Object -com internetexplorer.application; 
$ie.visible = $true; 
$ie.navigate($url); 
while ($ie.Busy -eq $true) 
{ 
    Start-Sleep 1; 

} 
$ie.Document.getElementById("LoginForm_email").value = $username
$ie.Document.getElementByID("LoginForm_password").value=$password
$ie.Document.getElementByID("qa-login-button").Click();

The web page is invoked when I click the login button.But I am not getting the values of credentials being passed in the swing window into it

sangeeta
  • 80
  • 1
  • 2
  • 12
  • 3
    1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) Swing GUIs might have to work on different platforms, using different PLAFs, on different screen sizes and resolutions with different default settings for font size. As such, they are not conducive to exact placement of components. Instead use layout managers, or [combinations of layout managers](http://stackoverflow.com/a/5630271/418556) as well as [layout padding and borders](http://stackoverflow.com/q/17874717/418556) for white space. – Andrew Thompson Aug 21 '14 at 09:53
  • 2
    A working example is shown in this possible [duplicate](http://stackoverflow.com/q/20600721/230513). – trashgod Aug 21 '14 at 09:59
  • No the issues I have said are not working – sangeeta Aug 21 '14 at 10:34
  • 1
    Tip: Be sure to add @trashgod (or whoever, the `@` is important) to *notify* a person of a new comment. Exactly one person can be notified per comment. BTW - who was that comment directed to? – Andrew Thompson Aug 22 '14 at 09:20
  • 1
    @AndrewThompson is correct; as a guide to your debugging efforts, the accepted answer in the [example cited](http://stackoverflow.com/a/20603012/230513) shows how to examine the output of your `Process`, which you presently ignore; you should also study his helpful links. – trashgod Aug 22 '14 at 09:46

0 Answers0