-
Calling objets
Hey, I am trying to call a object from once class to another. For exampel if i have 2 class a Welcome class and a Main class, on the Welcome class i have a button called "close" and on the Main class I have 2 buttons "open welcome" and "close". On the Main class i want to be able to call the close button so i can disable it or enable it. If any one can help that would be greate thanks.
Weclome class
Code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Mainwindow {
private JFrame MFrame;
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
try {
Mainwindow window = new Mainwindow();
window.MFrame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the application
*/
public Mainwindow() {
initialize();
}
/**
* Initialize the contents of the frame
*/
private void initialize() {
MFrame = new JFrame();
MFrame.setAlwaysOnTop(true);
MFrame.getContentPane().setLayout(null);
MFrame.setTitle("Main Window");
MFrame.setBounds(100, 100, 500, 375);
MFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JButton openWelcomeButton = new JButton();
openWelcomeButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent arg0) {
Welcome w = new Welcome();
}
});
openWelcomeButton.setText("Open Welcome");
openWelcomeButton.setBounds(207, 305, 121, 26);
MFrame.getContentPane().add(openWelcomeButton);
final JButton exitButton = new JButton();
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
System.exit(0);
}
});
exitButton.setText("exit");
exitButton.setBounds(352, 305, 106, 26);
MFrame.getContentPane().add(exitButton);
}
}
Main Class
Code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Welcome {
private JFrame WFrame;
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
try {
Welcome window = new Welcome();
window.WFrame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the application
*/
public Welcome() {
initialize();
}
/**
* Initialize the contents of the frame
*/
private void initialize() {
WFrame = new JFrame();
WFrame.setTitle("Welcome");
WFrame.getContentPane().setLayout(null);
WFrame.setBounds(100, 100, 500, 375);
WFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JButton closeButton = new JButton();
closeButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
WFrame.dispose();
}
});
closeButton.setText("Close");
closeButton.setBounds(178, 305, 106, 26);
WFrame.getContentPane().add(closeButton);
}
}
-
Re: Calling objets
make a public function to do that