Both the windows are created by two different classes.
Only when clicking the button in window1, window2 is setvisible.
i just wanted to know how to change the text in TextArea of the window2 , when i click on the
menubar on window1. but window2 should be visible only when i click on the button in window1.
i am posting the code i used to create two windows
/***********************************************************************************
* Code for Window 1 *
* *
***********************************************************************************/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Window1 extends Frame implements ActionListener{
public Window1(){
TextArea text = new TextArea();
TestMenuBar menus = new TestMenuBar(this,text);
setTitle("Text Viewer");
setSize(200,350);
setMenuBar(menus);
Panel buttonPanel = new Panel();
Button append = new Button("Append");
append.addActionListener(this);
buttonPanel.add(append);
add(text,BorderLayout.CENTER);
add(buttonPanel,BorderLayout.SOUTH);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
exit();
}
});
}
public static void main(
String args[]){
int k =5;
Window1
test = new Window1();
test.setVisible(true);
}
public void exit(){
setVisible(false);
dispose();
System.exit(0);
}
public void actionPerformed(ActionEvent e){
String s = e.getActionCommand();
if(s=="Append"){
/**********************************************
* This is where i am calling the window2 *
*********************************************/
Window2 tester = new Window2();
tester.setVisible(true);
this.setVisible(false);
}
}
}
class TestMenuBar extends MenuBar implements ActionListener{
Window1 mw;
TextArea text;
MenuItem mi;
public TestMenuBar(Window1 m,TextArea mtext){
mw = m;
text = mtext;
Menu fileMenu = new Menu("File");
Menu helpMenu = new Menu("Help");
fileMenu.add(mi = new MenuItem("Open"));
mi.addActionListener(this);
fileMenu.add(mi = new MenuItem("Exit"));
mi.addActionListener(this);
helpMenu.add(mi = new MenuItem("about"));
mi.addActionListener(this);
helpMenu.add(mi = new MenuItem("Contents"));
add(fileMenu);
add(helpMenu);
}
public void actionPerformed(ActionEvent e){
String item = e.getActionCommand();
if(item.equals("Exit")){
mw.exit();
}
else if(item.equals("Open")){
/***************************************************
* This is where i need to get control of text area *
* of window2 and change the contents of window2 *
***************************************************/
}
else
System.out.println("Selected File Menu"+item);
}
}
}
/***********************************************************************************
* Code for Window 2 *
* *
***********************************************************************************/
import java.awt.*;
import java.awt.event.*;
public class Window2 extends Frame implements ActionListener{
public Window2(){
setTitle("Edit Window");
setSize(200,350);
TextArea editText = new TextArea();
FrameMenuBar menu = new FrameMenuBar(this,editText); //Constructor for setting the menubar
setMenuBar(menu);
Button browse = new Button("Browse");
browse.addActionListener(this);
Button save = new Button("Save");
Panel buttonPanel = new Panel();
buttonPanel.add(browse);
buttonPanel.add(save);
add(editText,BorderLayout.CENTER);
add(buttonPanel,BorderLayout.SOUTH);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
exit();
}
});
}
public static void main(String args[]){
Window2 editor = new Window2();
editor.setVisible(true);
}
public void exit(){
setVisible(false);
dispose();
//System.exit(0);
}
public void actionPerformed(ActionEvent e){
String s = e.getActionCommand();
if(s=="Browse"){
/*******************************************
* This is where i call window1 by clicking *
* the button *
*******************************************/
Window1 tester = new Window1();
tester.setVisible(true);
this.setVisible(false);
}
}
}
class FrameMenuBar extends MenuBar implements ActionListener{
Window2 editor;
TextArea editText;
MenuItem mi;
FrameMenuBar(Window2 e,TextArea text){
editor = e;
editText = text;
Menu HelpMenu = new Menu("Help");
HelpMenu.add(mi = new MenuItem("about"));
mi.addActionListener(this);
HelpMenu.add(mi = new MenuItem("Contents"));
mi.addActionListener(this);
add(HelpMenu);
}
public void actionPerformed(ActionEvent e){
String item = e.getActionCommand();
if(item.equals("Help")){
//pop up Help Dialog Window
}
else{
//pop up contents Dialog Window
}
}
}