aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes how to control components of another window Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "how to control components of another window" Watch "how to control components of another window" New topic
Author

how to control components of another window

Chandhrasekar Saravanan
Ranch Hand

Joined: Jun 16, 2002
Posts: 57
Hi,
I have created two awt windows, window1 and window2. both window1 and window2 have TextAreas
and control buttons and window1 has an menubar.
when i click control buttons in window1 and window2 ,i am able to switch between windows.
what i am trying to make is when i clicked the menubar in window1 i should be able to change
the text in window2. also window2 should be visible only when i clicked the button in window1

I tried by creating two constructors but i am getting event dispatch error in runtime
The problem i think is how to get access to components of an other window, and how
to change the values of the components.

Thanks
Regards
Chandhrasekar Saravanan
Rene Liebmann
Ranch Hand

Joined: May 02, 2002
Posts: 196
The question is, where does create your windows? Does window1 create window2 or are they created by a different class?
Anyway, there are lot's of possibilities, which you can use.
- window1 could call functions of window2
- window2 can be a listener of window1 (PropertyChangeListener)
- window2 can be a observer of window1 (Observable)
If you need a bit more help, you should post a bit code.
Chandhrasekar Saravanan
Ranch Hand

Joined: Jun 16, 2002
Posts: 57
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
}
}
}


Ed Tidwell
Greenhorn

Joined: Oct 22, 2002
Posts: 18
You need to look at what is called the 'Mediator' pattern.
A mediator is basically the middle man in a large application.
So say when I open a certain type of file my mediator has an openAction. The mediator is responisble for putting the file name in the title bar, enabling the save actionItem which enables the Save in the toolbar and menubar.
In my swing applications I have a way to ask the JFrame for the mediator via an inteface.
Here is a code clip to show you what I mean...
<main class>
protected void registerMediator()
{
// register our main frame with the mediator
// for mouse events and messages
_mediator.registerFrame(this);
// register main panel so we can easily resize on updates
_mediator.registerMainPanel(_mainPanel); // _mainPanel is a TFrame object
// register panels that have actions for the mediator
_mediator.registerNetworkPanel(_networkPanel);
_mediator.registerLinkPanel(_linkPanel);
<mediator class>
//------------------------------------
//-- register controls to be mediated
//------------------------------------
public void registerFrame(NetAssign frame)
{
_frame=frame;
}

public void registerMainPanel(JPanel mainPanel)
{
_mainPanel = mainPanel;
}
<event in mediator that needs to be synched across multiple controls...>
public void action(OpenFileAction openFileAction)
{
// is this a Network file they have opened?
if (openFileAction.getFile().getName().endsWith(".Network"))
{
// get network map element
_networkMap.openNetworkFile(openFileAction.getFile());
} // Network file opened
// is this a Link file for a shelf that they have opened?
if (openFileAction.getFile().getName().endsWith(".shelf"))
{
_shelfLink.openLinkFile(openFileAction.getFile());
// init LIC dialog used on right click of MapNode
_assignLicDialog.setShelf(_shelfLink.getShelf());
} // Shelf file opened
// update layout managers
_mainPanel.validate();
// temp
_saveItem.setEnabled(true); // allow them to save the dummy file
// now set the tooltip for the Save action button
_saveItem.putValue(AbstractAction.SHORT_DESCRIPTION, "Save ");
// change this to be the .Assign file name _openNetworkFile.getName());
setMouseCursorToNormal(_frame);
_frame.setStatusBar(" "); // look at clearing messages in a back ground thread down the road
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to control components of another window