0

In this code, I am trying to get the window to hide away when I click the hide button. Clicking the button activates the makeHidden() method I have, but it's made on the same level as public static void main(String[] args), and that's where the window visibility is set. I can't seem to reference myWindow from the makeHidden method, and I don't know what put put before myWindow.setVisability(); to make it reference it.)

I can't figure this one out, and need help.

import java.awt.*;
import java.awt.event.*;

public class Main extends Frame implements WindowListener,ActionListener {
    /**
 * 
 */
private static final long serialVersionUID = 1L;
    Label text = new Label("not Posted");
    Button authentweet, hide;

    final static String newline = "\n";

    public static void main(String[] args) {
            Main myWindow = new Main("TwitterBot");
            myWindow.setSize(350,100);
            myWindow.setVisible(true);
    }

    public void makeHidden() {
        //this is where i want to make the window hide
    }
    public Main(String title) {

            super(title);
            setLayout(new FlowLayout());
            addWindowListener(this);
            authentweet = new Button("Make Post");
            hide = new Button("Hide");
            add(authentweet);
            add(text);
            add(hide);
            authentweet.addActionListener(this);
            hide.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == authentweet)
        {
            TwitterPost.makepost();
            text.setText("Posted");
        }
        if (e.getSource() == hide)
        {
            makeHidden();
        }


    }

    public void windowClosing(WindowEvent e) {System.exit(0);}
    public void windowOpened(WindowEvent e) {}
    public void windowActivated(WindowEvent e) {}
    public void windowIconified(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e) {}
    public void windowDeactivated(WindowEvent e) {}
    public void windowClosed(WindowEvent e) {}

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    Just use `setVisible(false)` without any object reference or use `this` (e.g. `this.setVisible(false)`). – initramfs Dec 26 '14 at 04:11
  • yep.. that worked.. thank you, i didnt expect it to be THAT simple – user1896414 Dec 26 '14 at 04:18
  • @CPUTerminator , That counts as an answer,not a comment – Spikatrix Dec 26 '14 at 04:21
  • 1) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. 2) Words typed in all lower case are hard to read, like trying to listen to someone who is mumbling. Please use an upper case letter at the start of sentences, for the word I, and proper names like `ArrayList` or Oracle. 3) And in future, please leave off the entire *"I'm a poor little street urchin"* paragraph. If a question requires improvement, putting that won't stop people from commenting. – Andrew Thompson Dec 27 '14 at 02:23

1 Answers1

1

Answer included for completeness.

Using methods from within the enclosing class need no object reference. Given that your class inherits from Frame you automatically get all non-private methods from Frame, including setVisible().

In certain cases however it may be required for you to reference the current object in which the this keyword should be used. References to the superclass should be made with the super keyword.

For your case, defining your makeHidden() method should go as:

public void makeHidden() {
    // Either
    setVisible(false);
    // Or
    this.setVisible(false);
    // Given you haven't overridden the superclass method, this too:
    super.setVisible(false);
}
initramfs
  • 8,275
  • 2
  • 36
  • 58