Solved

Accessing properties of window components

Posted on 2001-06-10
4
245 Views
Last Modified: 2010-04-16
If I have a Gui window which has a button to open a second window, if the button when first clicked runs a method which creates the Gui swing components, should the window when closed be disposed of or just hidden. I have set it so that the window is hidden, then subsequent clicks of the window open button will unhide the window. But how can I then access the properties of the components in the second window?

In the second window creation method I return the JFrame so that I can access its setvisible property, but how can I access eg a textbox in the window to set its text?
0
Comment
Question by:ncw
  • 2
4 Comments
 
LVL 3

Expert Comment

by:superschlonz
Comment Utility
subclass JFrame and implement the methods you need there, for example:

public class MyFrame extends JFrame
{
  JTextArea text = new JTextArea();

  public MyFrame( String title )
  {
    super(title);
    getContentPane().setLayout( new BorderLayout() );
    getContentPane().add( text, BorderLayout.CENTER );
  }
  public void setText( String s )
  {
    text.setText( s );
  }
  public String getText()
  {
    return text.getText();
  }
}

You can then use it like this:

MyFrame frame = new MyFrame( "window title" );
frame.setText( "some text" );
frame.setVisible( true );

And get the text later:

String enteredText = frame.getText();
0
 
LVL 1

Author Comment

by:ncw
Comment Utility
I can see it would work using a separate class. I'm now testing the secodary window as a separate class where the creation of Gui components is within the constructor. The window has a button which has an ActionListener which I have coded as an inner class which is now within the constructor.

I can call a method eg OpenWindow of this class which will call the JFrame's setVisible method, and I can pass other objects to this method to for instance set the text to the result of a method call on a passed object.

The problem now is that how can the actionPerformed method of the button's ActionListener (coded as an inner class of the constructor) change properties or make a method call on an object, eg one which was passed in the OpenWindow method.

Is it better to create the gui swing components from a method in the main window's class each time the window is required to be opened so that all objects instatiated in the 1st window are accessible by the 2nd, rather than create the 2nd window once and subsequently hide/show it?  
0
 
LVL 3

Accepted Solution

by:
superschlonz earned 50 total points
Comment Utility
You could do it like this:

class FirstWindo extends JFrame
{
  SecondWindow win2 = new SecondWindow();
  AnObject obj = new AnObject();
  aMethod()
  {
    win2.OpenWindow( obj );
  }
}

class SecondWindow extends JFrame
{
  private AnObject obj;
  private JTextArea text = new JTextArea();
  SecondWindow()
  {
    JButton b = new JButton();
    b.addActionListener( new ActionListener() {
      actionPerformed() {
        obj.setText( text.getText() ); // you have access to private members
      }
    });
    ....
  }
  public OpenWindow( AnObject o )
  {
    obj = o;
    setVisible( true );
  }
}

You could also call a private method of SecondWindow from the method
actionPerformed. If you have more than one button you can use their
setActionCommand and use only one instance of ActionListener for all these
buttons. The getActionCommand of the ActionEvent wil the get the action command
of the button which has been pressed.
There is one thing to remember: the  inner class has access only if it is not static!
If it is static it needs a reference to the instance of SecondWindow for accessing
the non-static members.
0
 

Expert Comment

by:rhubarbtwo
Comment Utility
superschlonz, I like to accept your comment as an answer but I see no accept buttons, I shall contact community support.

I tested your suggestions, but in the end I've chosen to construct all windows in one class to reduce complexity and make the sharing of properties easier.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now