Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am really new to code project and to java.

Here you can see class A (LoginFrame ) which is inside package Automation :

Java
package Automation;

import javax.swing.*;

import java.awt.*; 

import java.awt.event.*; 

import java.lang.Exception; 

import Automation.FileSelector;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

      

       public class LoginFrame extends JFrame implements ActionListener {


             Container container=getContentPane();

           JLabel appName= new JLabel("ONDEMAND Automation Tool");

           JLabel userLabel=new JLabel("Username");

           JLabel passwordLabel=new JLabel("Password");

           JTextField userTextField=new JTextField();

           JPasswordField passwordField=new JPasswordField();

           JButton loginButton=new JButton("LOGIN");

           JButton resetButton=new JButton("RESET");

          

           LoginFrame()

           {

              //Calling methods inside constructor.

               setLayoutManager();

               setLocationAndSize();

               addComponentsToContainer();

           }

          public void setLayoutManager()

          {

              container.setLayout(null);

          }

          public void setLocationAndSize()

          {

                appName.setBounds(110,30,300,100);

                appName.setFont(new Font("Calibri", Font.CENTER_BASELINE, 20));

              userLabel.setBounds(100,160,100,30);

              userLabel.setFont(new Font("Tahoma", Font.BOLD, 15));

              passwordLabel.setBounds(100,230,100,30);

              passwordLabel.setFont(new Font("Tahoma", Font.BOLD, 15));

              userTextField.setBounds(200,160,175,30);

              passwordField.setBounds(200,230,175,30);

              //showPassword.setBounds(150,250,150,30);

              loginButton.setBounds(80,330,150,40);

              resetButton.setBounds(270,330,150,40);

          }

          public void addComponentsToContainer()

          {

             //Adding each components to the Container

                container.add(appName);

              container.add(userLabel);

              container.add(passwordLabel);

              container.add(userTextField);

              userTextField.addActionListener(this);

              container.add(passwordField);

              userTextField.addActionListener(this);

              container.add(loginButton);

              loginButton.addActionListener(this);

              container.add(resetButton);

              resetButton.addActionListener(this);

          }


           public void actionPerformed(ActionEvent e) {

             

              if (e.getSource() == loginButton) {

                    String username = userTextField.getText();

                     char[] password = passwordField.getPassword();

                     System.out.println(username);

                     System.out.println(password);

                    

                     `FileSelector.f1.setVisible(true);`

                      `f.setVisible(false);`

                    

              }

                    

                    if (e.getSource() == resetButton) {

                          userTextField.setText("");

                            passwordField.setText("");

                     }

                    

              }     
     

             public static void main(String[] a){

 
               LoginFrame f = new LoginFrame();

                 f.setTitle("OnDemand Automation Tool");

               f.setVisible(true);

               f.setBounds(10,10,600,500);

               f.getContentPane().setBackground(new Color(255, 246, 240));

               f.setResizable(false);

           } 

       }


1. f is an object of LoginFrame and it is called inside main() method of LoginFrame. How can I access it inside actionPerformed event inside LoginFrame?


Also, Below, you can see class B (FileSelector), also inside package Automation.

Java
package Automation;

import javax.swing.*;

import java.awt.*; 

import java.awt.event.*; 

import java.lang.Exception; 
 

       import java.awt.event.ActionEvent;

       import java.awt.event.ActionListener;

      

       @SuppressWarnings("serial")

       public class FileSelector extends JFrame implements ActionListener {

      
             Container container=getContentPane();

           JLabel appName= new JLabel("ONDEMAND Automation Tool");

           JLabel fileLocation=new JLabel("Select File Location");

           JButton openFile=new JButton("Open");

           JLabel downloadLocation=new JLabel("Select Download Location");

           JButton opendwnFile=new JButton("Open");

            

           FileSelector()

           {

              //Calling methods inside constructor.

               setLayoutManager();

               setLocationAndSize();

               addComponentsToContainer();

      

           }

          public void setLayoutManager()

          {

              container.setLayout(null);

          }

          public void setLocationAndSize()

          {

              //Setting location and Size of each components using setBounds() method.

                appName.setBounds(110,30,300,100);

                appName.setFont(new Font("Calibri", Font.CENTER_BASELINE, 20));

            

                    fileLocation.setBounds(100,160,100,30);

                    fileLocation.setFont(new Font("Tahoma", Font.BOLD, 15));

                    openFile.setBounds(100,230,100,30);

                   

                    downloadLocation.setBounds(100,160,100,30);

                    downloadLocation.setFont(new Font("Tahoma", Font.BOLD, 15));

                    opendwnFile.setBounds(100,230,100,30);

          }

          public void addComponentsToContainer()

          {

             //Adding each components to the Container

                container.add(appName);

                add(fileLocation);

               add(openFile);

               openFile.addActionListener(this);

               add(downloadLocation);

               add(opendwnFile);

               opendwnFile.addActionListener(this);

          }

             public static void main(String[] a){

 

                    FileSelector f1=new FileSelector();

                     f1.setTitle("OnDemand Automation Tool");

                     f1.setVisible(true);

                     f1.setBounds(10,10,800,800);

                     f1.getContentPane().setBackground(new Color(255, 246, 240));

                     f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                     f1.setResizable(false);

           }

             @Override

             public void actionPerformed(ActionEvent arg0) {

                    // TODO Auto-generated method stub
       

             }


       } 


2. How can I access f1 called inside the main method of FileSelector in actionPerfprmed method of class LoginFrame ?

Here my goal is to close LoginFrame page and open FileSelector page once the user clicks on loginButton. Please help me out.

What I have tried:

I tried to take the actionPerformed method inside main method but since loginButton is called inside LoginFrame it is showing error. How can I proceed?
Posted
Updated 25-Oct-22 2:52am
v2

1 solution

Your code has 2 main methods, one in LoginFrame and one in FileSelector. A program should have only one main method.

You need to rethink your logic. What I believe you might want is some sort of controller class that has the one and only main method in it. It will create and call methods on various components depending upon user input. Components like LoginFrame and FileSelector and perhaps others once a file or files have been selected.

So, as in your example above, at startup the main method of the controller class creates a LoginFrame for the user to log in. If successful, the LoginFrame returns success to the main method. If the login is unsuccessful the user can reset and retry or should probably have a way to cancel and return failure to login to main. Upon successful login, main then creates a FileSelector object and the user can interact with it. Neither the LoginFrame nor the FileSelector objects need to know anything about the other.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900