Results 1 to 6 of 6

Thread: close a frame..

  1. #1
    Join Date
    Jan 2008
    Posts
    5
    Rep Power
    0

    Red face close a frame..

    hi every body!..
    here i have a prob with my system that using java..when i click a button, it will run a frame..here i want to ask how to close the frame only without close the whole system..i have put down the coding below but it doesnt work
    this.setVisible(false)
    please advise me...

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    frame.dispose();

  3. #3
    Join Date
    Jan 2008
    Posts
    5
    Rep Power
    0

    Thumbs up

    thanks bro..:D

  4. #4
    coolbiker is offline Member
    Join Date
    Apr 2008
    Posts
    2
    Rep Power
    0

    Question frame.dispose does not work for me any sugestions

    I found this post while doing a Yahoo search and tried this with no luck it tells me I cannot set a static reference to a non static method dispose()

    maybe Im setting it up the wrong way but in someway I need to have an exit button for a form for a practice test and my teachers says that dispose should work. belows the code for the button. It is a default frame using Eclipse 3.2.2 through its visual editor and 1.6_5Update of java sdk(the newest one at the time)

    Java Code:
    private JButton getJButtonexit() {
    		if (jButtonexit == null) {
    			jButtonexit = new JButton();
    			jButtonexit.setBounds(new Rectangle(267, 199, 106, 33));
    			jButtonexit.setText("Exit Frame");
    			jButtonexit.addActionListener(new java.awt.event.ActionListener() {
    				public void actionPerformed(java.awt.event.ActionEvent e) {
    					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
    				frame.dispose();
    				}
    			});
    		}
    		return jButtonexit;

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    it tells me I cannot set a static reference to a non static method dispose
    Sounds like there may be trouble with a static modifier somewhere.
    I would want to look at the exact exception wording.
    If I mistakenly capitalized the "frame" reference, viz, Frame.dispose(); then I'd get this compile error:
    Java Code:
    C:\jexp>javac test.java
    test.java:12: non-static method dispose() cannot be referenced from a static context
                    Frame.dispose();
                         ^
    1 error
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Test {
        JFrame frame;
    
        public Test() {
            JButton button = new JButton("exit");
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    frame.dispose();
                }
            });
            JPanel panel = new JPanel();
            panel.add(button);
            frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(panel);
            frame.pack();
            frame.setLocation(200,200);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            new Test();        
        }
    }

  6. #6
    coolbiker is offline Member
    Join Date
    Apr 2008
    Posts
    2
    Rep Power
    0

    Default

    Hardwired I seem to have found my solution for whatever Eclipse won't allow me to do "frame." and I saw some other post that simply did "setVisible(false); and just plain dispose(); I have 1_6_05 so maybe they do it differently.
    Its probably like the new stuff my teacher telling us about generic functions that are supposedly not in earlier versions of Java and supposed to do allot of stuff if you can understand them under your belt.
    I thank you for your response "hardwired"

Similar Threads

  1. Frame close operation
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-21-2007, 08:39 AM
  2. How to close a JFrame
    By valery in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 05:33 PM
  3. How to close JDBC Connection
    By Heather in forum JDBC
    Replies: 2
    Last Post: 07-15-2007, 01:07 PM
  4. Close a JDialog Programmatically
    By Marcus in forum Advanced Java
    Replies: 1
    Last Post: 07-06-2007, 04:06 PM
  5. What could be causing the browser to close?
    By Marcus in forum Java Applets
    Replies: 2
    Last Post: 07-04-2007, 07:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •