Solved

Accessing properties of window components

Posted on 2001-06-10
4
274 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Fast way to search item into Java Array (Rhino compatible) 2 39
Java class and jar 3 43
learn programming 8 73
Html split(text) 2 32
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

730 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