Results 1 to 4 of 4

Thread: dispose method

  1. #1
    jumpman8947 is offline Member
    Join Date
    Feb 2013
    Posts
    38
    Rep Power
    0

    Default dispose method

    I created a program where when I click a button another screen shows up but the only problem I'm having is the first screen isn't going away. I tried to use the dispose method but I come up with an error don't know why though.
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class MainScreen extends JFrame
    {
    	private  JTextField field1;
    	private JLabel label1;
    	private JLabel label2;
    	private JButton button1;
    	private JButton button2;
    	private JComboBox box1;
    	private JLabel imageIcon;
    
    
    	public MainScreen()
    	{
    		field1 = new JTextField("Welcome");
    		setLayout(null);
    
    		Dimension size1 = field1.getPreferredSize();
    		//Dimension size2 = label1.getPreferredSize();
    
    		//Build field1
    		field1.setBounds(400, 15, 220, 75);
    		field1.setBackground(new Color(0, 102, 102));
    		field1.setFont(new Font("Times New Roman", 1, 24));
    		field1.setEditable(false);
    		add(field1);
    
    		//Build button1
    		button1 = new JButton("EXIT");
    		button1.setBounds(400, 620, 220, 75);
    		button1.setBackground(new Color(0, 0, 0));
    		button1.setForeground(new Color(255, 0, 0));
    		button1.addActionListener(new button1Listener());
    		add(button1);
    
    		//Build button2
    		button2 = new JButton("Click To Enter");
    		button2.setBounds(350, 300, 220, 75);
    		button2.addActionListener(new button2Listener());
    		add(button2);
    
    		//Build label1
    		label1 = new JLabel();
    		label1.setIcon(new ImageIcon ("bible3.gif"));
    		label1.setBounds(0, 15, 600, 600);
    		add(label1);
    
    		//Build label2
    		label2 = new JLabel();
    		label2.setIcon(new ImageIcon ("seed.jpg"));
    		label2.setBounds(707, 100, 400, 400);
    		add(label2);
    
    		//Build ComboBox
    		box1 = new JComboBox();
    		box1.setModel(new DefaultComboBoxModel(new String[]
    		{
    			"Gospel", "Raeggae", "Hip-Hop"
    		}));
    		box1.setBounds(700, 0, 89, 50);
    		add(box1);
    
    		setSize(1100, 770);
    		setTitle("Main Screen");
    		setBackground(new Color(204, 102, 0));
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		getContentPane().setBackground(new Color(204, 102, 0));
    
    
    
    
    
    		setVisible(true);
    
    
    
    	}
    
    	private class button1Listener implements ActionListener
    				{
    					public void actionPerformed(ActionEvent e)
    					{
    						System.exit(0);
    					}
    
    				}
    	private class button2Listener implements ActionListener
    				{
    					public void actionPerformed(ActionEvent e)
    					{
    						MiddleScreen midScr = new MiddleScreen();
    						midScr.setVisible(true);
    						MainScreen.dispose();
    
    					}
    
    				}
    
    
    
    
    
    
    
    
    	public static void main(String[] args)
    	{
    		new MainScreen();
    	}
    
    
    
    }
    The error I'm getting is "non-static method dispose() can't be referenced from a static context MainScreen.dispose();"

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: dispose method

    That's because the dispose method is an instance method and not a class method. So you need to use a reference of MainScreen to call it.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: dispose method

    As per what jim829 states, you need an instance of MainScreen, and in fact you need the current instance which can be obtained by changing MainScreen.dispose() to MainScreen.this.dispose().

    Having said that I must also be blunt and tell you that your current GUI design is not good. Most applications will have only one main window, and that will be the JFrame. Applications will then either change views held by that main window or else show child windows which should be dialogs such as JDialog or JOptionPanes as they are dependent on the main window.

  4. #4
    tamilarasi is offline Member
    Join Date
    Nov 2012
    Location
    India
    Posts
    70
    Rep Power
    0

    Default Re: dispose method

    Hi jumpman8947,

    can you post the code of MiddleScreen(); and MainScreen.dispose(); here?
    Regards
    Android developer at Trinay Technology Solutions,http://www.trinaytech.com,5705750475

Similar Threads

  1. dispose() gives an "invalid method declaration"
    By DennisM in forum New To Java
    Replies: 4
    Last Post: 10-28-2011, 09:25 PM
  2. Help to call a method on dispose()
    By Muskar in forum New To Java
    Replies: 16
    Last Post: 11-21-2010, 12:24 PM
  3. dispose() method not working
    By R&R in forum New To Java
    Replies: 19
    Last Post: 11-15-2010, 02:46 AM
  4. how to dispose?
    By justlynn in forum NetBeans
    Replies: 15
    Last Post: 07-01-2010, 03:17 AM
  5. dispose
    By simontkk2005 in forum AWT / Swing
    Replies: 3
    Last Post: 11-19-2009, 12:42 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
  •