Solved

Accessing properties of window components

Posted on 2001-06-10
4
279 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 3

Expert Comment

by:superschlonz
ID: 6173201
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
ID: 6173848
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
ID: 6173960
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
ID: 6197756
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

705 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