Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Accessing properties of window components

Posted on 2001-06-10
4
Medium Priority
?
294 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
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 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

783 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