Solved

Accessing properties of window components

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet  URL Rewriting 1 36
SHA2 certs for IIS AND Java? 2 91
tomcat startup error 5 65
hibernate example using maven 12 42
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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.

777 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