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 2 of 2

Thread: Syntax error with dispose()

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

    Default Syntax error with dispose()

    Hello, I have been having a problem in some code I'm writing.

    I'm doing a project in 8th grade that involves some GUI and I have a problem with my listener and this.dispose().

    I'll post the code with the error here:
    class doneListener implements ActionListener{
    			public void actionPerformed (ActionEvent e){
    				if("doneAdd".equals(e.getActionCommand())){
    					addFrame.dispose();
    Basically I'm getting an error on addFrame, which is the name of my JFrame variable that contains all the stuff in the window.

    I'll post the whole thing here:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
     
    public class primary extends JPanel
                          implements ListSelectionListener {
        private JList list;
        private DefaultListModel listModel;
     
        private static final String addString = "ADD!";
        private static final String removeString = " - ";
        private static final String editString = "edit";
        private JButton removeButton,editButton;
        private static final String doneAdd = "Done!";
     
        public primary() {
            super(new BorderLayout());
     
            listModel = new DefaultListModel();
     
            //Create the list and put it in a scroll pane.
            list = new JList(listModel);
            list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            list.setSelectedIndex(0);
            list.addListSelectionListener(this);
            list.setVisibleRowCount(20);
     
     
     
     
     
            JScrollPane listScrollPane = new JScrollPane(list);
     
            JButton addButton = new JButton(addString);
            addButton.setActionCommand(addString);
            addButton.addActionListener(new AddListener());
            addButton.setEnabled(true);
     
            editButton = new JButton(editString);
            editButton.setActionCommand(editString);
            editButton.addActionListener(new EditListener());
     
            removeButton = new JButton(removeString);
            removeButton.setActionCommand(removeString);
            removeButton.addActionListener(new RemoveListener());
     
     
            //Create a panel that uses BoxLayout.
            JPanel buttonPane = new JPanel();
            buttonPane.setLayout(new BorderLayout());
            JPanel addremovePane = new JPanel();
     
     
            addremovePane.setLayout(new BorderLayout());
            addremovePane.add(addButton, BorderLayout.LINE_START);
            addremovePane.add(removeButton, BorderLayout.LINE_END);
     
            addremovePane.add(Box.createHorizontalStrut(5), BorderLayout.CENTER);
     
            buttonPane.add(addremovePane, BorderLayout.LINE_START);
            buttonPane.add(editButton, BorderLayout.LINE_END);
     
            buttonPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
     
            add(listScrollPane, BorderLayout.CENTER);
            add(buttonPane, BorderLayout.PAGE_END);
     
            if (listModel.getSize()==0){
            	removeButton.setEnabled(false);
            	editButton.setEnabled(false);
            }
        }
     
        class RemoveListener implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                int index = list.getSelectedIndex();
                listModel.remove(index);
     
                int size = listModel.getSize();
     
                if (size == 0) { //Nobody's left, disable remove
                    removeButton.setEnabled(false);
     
                } else { //Select an index.
                    if (index == listModel.getSize()) {
                        //removed item in last position
                        index--;
                    }
     
                    list.setSelectedIndex(index);
                    list.ensureIndexIsVisible(index);
                }
            }
        }
     
     
     
     
        class EditListener implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                int index = list.getSelectedIndex();
     
     
                    list.setSelectedIndex(index);
                    list.ensureIndexIsVisible(index);
                }
            }
     
     
        class addWindow extends JPanel{
     
        	public addWindow(){
        		super(new BorderLayout());
     
        		String [] eqSel = {"Test1","Test2","Test3"};
     
        		JPanel topPanel = new JPanel(new BorderLayout());
        		JPanel buttonPanel = new JPanel(new BorderLayout());
        		JPanel comboPanel = new JPanel(new BorderLayout());
     
     
        		JCheckBox solType = new JCheckBox("Equation as solution?",false);
        		solType.setEnabled(true);
        		topPanel.add(solType,BorderLayout.PAGE_END);
     
        		JButton doneButton = new JButton(doneAdd);
        		doneButton.setEnabled(true);
        		doneButton.setActionCommand(doneAdd);
        		doneButton.addActionListener(new doneListener());
     
        		JComboBox eqType = new JComboBox(eqSel);
        		eqType.setEnabled(true);
        		topPanel.add(eqType,BorderLayout.LINE_START);
        		add(topPanel,BorderLayout.PAGE_START);
     
     
                buttonPanel.setLayout(new BorderLayout());
                buttonPanel.add(doneButton, BorderLayout.PAGE_END);
                add(buttonPanel,BorderLayout.LINE_END);
        	}
    		class doneListener implements ActionListener{
    			public void actionPerformed (ActionEvent e){
    				if("doneAdd".equals(e.getActionCommand())){
    					addFrame.dispose();
    				}
    			}
    		}
        	}
     
     
       public class AddListener implements ActionListener{
            //Required by ActionListener.
            public void actionPerformed(ActionEvent e) {
                int index = list.getSelectedIndex(); //get selected index
     
                if (index == -1) { //no selection, so insert at beginning
                    index = 0;
                } else {           //add after the selected item
                    index++;
                }
                JFrame addFrame = new JFrame("Equation Editor");
                //Select the new item and make it visible.
                list.setSelectedIndex(index);
                list.ensureIndexIsVisible(index); 
                //This creates the window
                //These lines create the frame
     
                addFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
     
                //Create and set up the content pane.
                JComponent newContentPane = new addWindow();
                newContentPane.setOpaque(true); //content panes must be opaque
                addFrame.setContentPane(newContentPane);
     
                //Display the window.
                addFrame.pack();
                addFrame.setSize(300,500);
                addFrame.setVisible(true);
                addFrame.setResizable(false);
            }
     
         }
     
     
        //This method is required by ListSelectionListener.
        public void valueChanged(ListSelectionEvent e) {
            if (e.getValueIsAdjusting() == false) {
     
                if (list.getSelectedIndex() == -1) {
                //No selection, disable remove and edit button
                    removeButton.setEnabled(false);
                    editButton.setEnabled(false);
     
                } else {
                //Selection, enable the remove and edit button
                    removeButton.setEnabled(true);
                    editButton.setEnabled(true);
                }
            }
        }
     
     
        private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("Puzzles!");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            //Create and set up the content pane.
            JComponent newContentPane = new primary();
            newContentPane.setOpaque(true); //content panes must be opaque
            frame.setContentPane(newContentPane);
     
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
     
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }

    I've tried moving around the class that it's in, nesting it within the JFrame class, making the JFrame class public, and changing around the syntax of dispose();. My teacher is out of ideas and I am stuck until I can get this solved.

    Thanks!


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

    Default Re: Syntax error with dispose()

    A few things first: a) I recommend following standard java code conventions (Code Conventions for the Java Programming Language ) b) It always help to post an SSCCE that demonstrates the problem (see my signature) - oftentimes the process of stripping the problem down to an SSCCE reveals the problem and solution.

    Now to your question: where is the variable addFrame defined in this class? It must be defined as a variable somewhere for you to be able to use said variable.

Similar Threads

  1. Syntax in method
    By tarkal in forum Java Theory & Questions
    Replies: 2
    Last Post: September 23rd, 2011, 03:01 PM
  2. do while syntax error problem
    By derekxec in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 1st, 2011, 06:30 PM
  3. Syntax error on token ";", @ expected after this token
    By MagicMojo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 16th, 2011, 07:48 AM
  4. syntax question
    By surfbumb in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 9th, 2011, 04:01 PM
  5. dispose()'ing a JFrame and replacing it
    By musasabi in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 14th, 2010, 02:31 PM