Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to:pass an Object reference to another class?Close one GUI window only?

843789Sep 11 2010 — edited Sep 18 2010
I have a class that looks like:
public class Expression Builder extends JFrame {
    .....
    JTextField textField;

    public ExpressionBuilder() {
           textField = new JTextField(10);
            .....
            .....
            setSize(200,200);
            setVisible(true):
    }

    public void enableTextField() {
             textField.setEnabled(true);
    }

    public static void main(String args[]){
         ExpressionBuilder app = new ExpressionBuilder();
         app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
I then have another class that looks like:
public class BaseProtocolWindow extends JFrame {
    .......

    JButton button = new JButton("Done");
    
ExpressionBuilder expressionBuilder = new ExpressionBuilder();

    public BaseProtocolWindow() {
            .....
            .....
            setSize(200,200);
            setVisible(true):
    }
    
    .......
    .......
    public void createXPanel() {
    
            button.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    expressionBuilder.enableTextField();
                   setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
            }
        });

}
When the button is pressed, I would like to enable a textfield in the ExpressionBuilder class and close the BaseProtocolWindow.

The problem is that because I have created two objects of the ExpressionBuilder class (one in ExpressionBuilder itself and one in BaseProtocolWindow(), two ExpressionBuilder windows are opened; first one when I run the code and the second one gets created later in the BaseProtocolWindow.

1) How can I change this code to only create one window? I think I need to somehow send a reference of the ExpressionBuilder Object created(called app in this example) to BaseProtocolWindow instead of creating a whole new object but I am not sure how.

2) setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE); only hides the window and does not closes it. If I set it to setDefaultCloseOperation(WindowConstants.CLOSE_ON_CLOSE); both the ExpressionBuilder window as well as the BaseProtocolWindow closes but I only want to close the BaseProtocolWindow. (to be honest the hiding part has even stopped working for some reason).

I would very much appreciate any help.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 16 2010
Added on Sep 11 2010
29 comments
740 views