Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Trying to add a close button

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trying to add a close button

    HI all,

    I am trying to add a close button to some basic swing code I was playing with, but no matter where I put the code for the Jbutton I get errors.

    Here is the code for the program as is now
    // Import the swing and AWT classes needed
    import java.awt.EventQueue;
    import java.awt.FlowLayout;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    /**
     * Basic Swing example.
     */
    public class SwingExample {
        public static void main(String[] args){
        private JButton quitButton;
     
            // Make sure all Swing/AWT instantiations and accesses are done on the
            // Event Dispatch Thread (EDT)
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    // Create a JFrame, which is a Window with "decorations", i.e.
                    // title, border and close-button
                    JFrame f = new JFrame("Swing Example Window");
     
                    // Set a simple Layout Manager that arranges the contained
                    // Components
                    f.setLayout(new FlowLayout());
     
                    // Add some Components
                    f.add(new JLabel("Hello, world!"));
                    f.add(new JButton("Press me!"));
                    f.add(new JButton("Quit Button"));
     
     
                    // "Pack" the window, making it "just big enough".
                    f.pack();
     
                    // quit button click event listener
    						quitButton.addActionListener(new ActionListener()
    						{
    							public void actionPerformed(ActionEvent e)
    							{
    								if (JOptionPane.showConfirmDialog(null, "Do you really want to quit?", "Quit", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
    								{
    									System.exit(0);
    								}
    							}
    		});
     
                    // Set the visibility as true, thereby displaying it
                    f.setVisible(true);
                }
            });
        }
    }

    and here are the errors I am getting with this.
    C:\Users\welshd.psrc\Documents\gui Java\SwingExample.java:17: illegal start of expression
    private JButton quitButton;
    ^
    1 error
    Where in the code do I put the "private JButton quitButton; code at so it will work right?

    Thanks in advance.
    COY


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Trying to add a close button

    Members of a class have access modifiers, but variables declared within a function (in this case main) aren't defined with access modifiers. If you want the JButton within main only, remove the private and move the variable within scope of where you need it. If you want access associated with a class move it to the appropriate location so the class has access. In this case, you could probably move it into the scope of the run method (remove the private).

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to add a close button

    OK I tried to add it to the run command, but it still gives errors.

    Here is the updated code:
    // Import the swing and AWT classes needed
    import java.awt.EventQueue;
    import java.awt.FlowLayout;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    /**
     * Basic Swing example.
     */
    public class SwingExample {
        public static void main(String[] args){
    		// Make sure all Swing/AWT instantiations and accesses are done on the
            // Event Dispatch Thread (EDT)
            EventQueue.invokeLater(new Runnable() {
                public void run(JButton"quitButton"); {
                    // Create a JFrame, which is a Window with "decorations", i.e.
                    // title, border and close-button
                    JFrame f = new JFrame("Swing Example Window");
     
                    // Set a simple Layout Manager that arranges the contained
                    // Components
                    f.setLayout(new FlowLayout());
     
                    // Add some Components
                    f.add(new JLabel("Hello, world!"));
                    f.add(new JButton("Press me!"));
    				f.add(new JButton("quitButton"));
     
     
                    // "Pack" the window, making it "just big enough".
                    f.pack();
     
                    // quit button click event listener
    						quitButton.addActionListener(new ActionListener()
    						{
    							public void actionPerformed(ActionEvent e)
    							{
    								if (JOptionPane.showConfirmDialog(null, "Do you really want to quit?", "Quit", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
    								{
    									System.exit(0);
    								}
    							}
    		});
     
                    // Set the visibility as true, thereby displaying it
                    f.setVisible(true);
                }
            });
        }
    }

    and here are the errors I am getting
    C:\Users\welshd.psrc\Documents\gui Java\SwingExample.java:20: <identifier> expected
    public void run(JButton"quitButton"); {
    ^
    Ok the first carrot is supposed to be under teh " before the Q
    C:\Users\welshd.psrc\Documents\gui Java\SwingExample.java:20: ';' expected
    public void run(JButton"quitButton"); {
    ^
    the second carrot is supposed to be under the last )
    2 errors

    What does <identifier> expected mean??

    Thanks

    COY
    Last edited by coyboss; February 12th, 2011 at 12:29 PM. Reason: fix placement of carrot

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Trying to add a close button

    public void run(JButton"quitButton");

    This is not a correct format...you want to create your button object as you would any other object within the run method.

  5. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to add a close button

    so are you talking about using the f.add (new JButton(:closeButton; method??
    If so I already tried that and it said it couldn't find the jbutton.

    so where else would I add it??

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Trying to add a close button

    No, because you need a reference to the button to add a listener to it...in pseudocode:
    JButton quit = new JButton("text");
    whatever.add(quit);
    quit.addActionListener(...

Similar Threads

  1. Replies: 1
    Last Post: February 10th, 2012, 10:05 AM
  2. rightway to close a socket
    By balexios in forum Java Networking
    Replies: 1
    Last Post: October 28th, 2010, 07:15 PM
  3. [SOLVED] How to close all tabs in JTabbedPane?
    By LeonLanford in forum AWT / Java Swing
    Replies: 7
    Last Post: June 28th, 2010, 10:58 AM
  4. Do I need to close an audio clip?
    By m2oswald in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 8th, 2010, 01:20 AM
  5. close JDialog on button click
    By Christophe in forum AWT / Java Swing
    Replies: 4
    Last Post: April 4th, 2010, 11:04 PM