I am really new to code project and to java.
Here you can see class A (LoginFrame ) which is inside package Automation :
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()
{
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);
loginButton.setBounds(80,330,150,40);
resetButton.setBounds(270,330,150,40);
}
public void addComponentsToContainer()
{
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.
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()
{
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));
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()
{
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) {
}
}
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?