Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By jim829

Thread: getting 2 errors from demo code?

  1. #1
    innovo is offline Member
    Join Date
    Jul 2015
    Posts
    84
    Rep Power
    0

    Default getting 2 errors from demo code?

    I recently was trying to work on a progressbar, so I began looking at demo code. However, when I added the demo code into netbeans, I had errors. I expected this, but I am unable to solve some of the errors, particularly at lines 110 and 123 of the whole code (listed at the bottom)

    I did not comment all of that stuff out, so idk why Javaforums made everything blue, but okay.

    Java Code:
      public void actionPerformed(ActionEvent evt) {
            startButton.setEnabled(false);
            setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
            //Instances of javax.swing.SwingWorker are not reusuable, so
            //we create new instances as needed.
            task = new Task();
            task.addPropertyChangeListener(this);
            task.execute();
        }
    in this particular instance, netbeans gets an error and tells me to add
    Java Code:
    @Override
    which ends up being an error anyway.



    The other error is the same thing
    Java Code:
    public void propertyChange(PropertyChangeEvent evt) {
            if ("progress" == evt.getPropertyName()) {
                int progress = (Integer) evt.getNewValue();
                progressBar.setValue(progress);
                taskOutput.append(String.format(
                        "Completed %d%% of task.\n", task.getProgress()));
            } 
        }
    Netbeans again suggests
    Java Code:
    @Override
    which gives a bigger error



    what the heck is @Override anyway? I've never used it before.


    For the sake of helpfulness I'll post the whole progressbar demo code below, maybe there's something here I'm not aware of that's causing these errors.

    Java Code:
    package progressbardemo;
    
    import java.awt.BorderLayout;
    import java.awt.Cursor;
    import java.awt.Insets;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.beans.PropertyChangeEvent;
    import java.beans.PropertyChangeListener;
    import java.util.Random;
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JProgressBar;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.SwingWorker;
    
    /**
     *
     * @author natehindman
     */
    public class ProgressBarDemo extends JPanel
                                 implements ActionListener, 
                                            PropertyChangeListener {
     
        private JProgressBar progressBar;
        private JButton startButton;
        private JTextArea taskOutput;
        private Task task;
    
        @Override
        public void actionPerformed(ActionEvent e) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
    
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
     
        class Task extends SwingWorker<Void, Void> {
            /**
             * Main task. Executed in background thread.
             */
            @Override
            public Void doInBackground() {
                Random random = new Random();
                int progress = 0;
                //Initialize progress property.
                setProgress(0);
                while (progress < 100) {
                    //Sleep for up to one second.
                    try {
                        Thread.sleep(random.nextInt(1000));
                    } catch (InterruptedException ignore) {}
                    //Make random progress.
                    progress += random.nextInt(10);
                    setProgress(Math.min(progress, 100));
                }
                return null;
            }
     
            /**
             * Executed in event dispatching thread
             */
            @Override
            public void done() {
                Toolkit.getDefaultToolkit().beep();
                startButton.setEnabled(true);
                setCursor(null); //turn off the wait cursor
                taskOutput.append("Done!\n");
            }
        }
     
        public ProgressBarDemo() {
            super(new BorderLayout());
     
            //Create the demo's UI.
            startButton = new JButton("Start");
            startButton.setActionCommand("start");
            startButton.addActionListener(this);
     
            progressBar = new JProgressBar(0, 100);
            progressBar.setValue(0);
            progressBar.setStringPainted(true);
     
            taskOutput = new JTextArea(5, 20);
            taskOutput.setMargin(new Insets(5,5,5,5));
            taskOutput.setEditable(false);
     
            JPanel panel = new JPanel();
            panel.add(startButton);
            panel.add(progressBar);
     
            add(panel, BorderLayout.PAGE_START);
            add(new JScrollPane(taskOutput), BorderLayout.CENTER);
            setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
     
        }
     
        /**
         * Invoked when the user presses the start button.
         * @param evt
         */
        //@Override
        public void actionPerformed(ActionEvent evt) {
            startButton.setEnabled(false);
            setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
            //Instances of javax.swing.SwingWorker are not reusuable, so
            //we create new instances as needed.
            task = new Task();
            task.addPropertyChangeListener(this);
            task.execute();
        }
     
        /**
         * Invoked when task's progress property changes.
         */
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            if ("progress" == evt.getPropertyName()) {
                int progress = (Integer) evt.getNewValue();
                progressBar.setValue(progress);
                taskOutput.append(String.format(
                        "Completed %d%% of task.\n", task.getProgress()));
            } 
        }
     
     
        /**
         * Create the GUI and show it. As with all GUI code, this must run
         * on the event-dispatching thread.
         */
        private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("ProgressBarDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            //Create and set up the content pane.
            JComponent newContentPane = new ProgressBarDemo();
            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();
                }
            });
        }
    }
    Last edited by Norm; 08-07-2015 at 03:01 PM. Reason: Added second * to start of comment

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: getting 2 errors from demo code?

    Two things.

    1) "what the heck is @Override anyway? I've never used it before. " - that is something you should simply lookup, its not a secret

    2) don't refer to the existence of errors, post the actual errors themselves. What you have now is a very elaborate "it doesn't work".

    In any case when I compile this code, the compiler very correctly tells me that you have methods two times in the same class. Both actionPerformed() and propertyChange() appear twice.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Xen
    Xen is offline Member
    Join Date
    Jan 2015
    Posts
    86
    Rep Power
    0

    Default Re: getting 2 errors from demo code?

    @Override is just an annotation that is meant to clarify to the reader mostly that a certain method is an overloaded method.

    This is supposed to indicate particularly to a reader of the code that the method was not declared there originally, so a new person to the code can know what goes on behind the scenes and quickly understand what it is.

    I personally don't like it, it is often very verbose and don't use it until now.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: getting 2 errors from demo code?

    I personally don't like it, it is often very verbose
    The @Override tells the compiler to check if the designated method exists in a super class. It is easy to misspell a method name or give it the wrong args. With @Override the compiler will tell you about it.
    It can save you lots of debugging trying to find out why your method is not being called.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: getting 2 errors from demo code?

    Quote Originally Posted by gimbal2 View Post
    that is something you should simply lookup, its not a secret
    Well, you tried to let the OP figure it out.

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

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: getting 2 errors from demo code?

    I've deleted three off-topic posts. Xen, if you want to share your philosophies, we have a Forum Lobby where that is acceptable.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    6

    Default Re: getting 2 errors from demo code?

    Quote Originally Posted by Xen View Post
    @Override is just an annotation that is meant to clarify to the reader mostly that a certain method is an overloaded method.
    .
    I don't want to appear picky, but overriding and overloading are two different things.

  8. #8
    Join Date
    Aug 2015
    Location
    Northern California
    Posts
    31
    Rep Power
    0

    Default Re: getting 2 errors from demo code?

    You have declared this method twice

    Java Code:
        /**
         * Invoked when the user presses the start button.
         * @param evt
         */
        //@Override
        public void actionPerformed(ActionEvent evt) {
            startButton.setEnabled(false);
            setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
            //Instances of javax.swing.SwingWorker are not reusuable, so
            //we create new instances as needed.
            task = new Task();
            task.addPropertyChangeListener(this);
            task.execute();
        }
      
        /**
         * Invoked when task's progress property changes.
         */
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            if ("progress" == evt.getPropertyName()) {
                int progress = (Integer) evt.getNewValue();
                progressBar.setValue(progress);
                taskOutput.append(String.format(
                        "Completed %d%% of task.\n", task.getProgress()));
            } 
        }
    Innovo I believe after you remove one set of these methods you will then have another error but this time it will be in the console not the IDE itself. Copy/Paste the stacktrace here if you need help with that error.

  9. #9
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    878
    Rep Power
    12

    Default Re: getting 2 errors from demo code?

    remove the duplicate methods ur code will be fine
    \
    Ramya:cool:

Similar Threads

  1. Why am I getting errors on this code?
    By MW130 in forum New To Java
    Replies: 2
    Last Post: 05-07-2013, 03:57 AM
  2. Why am I getting errors on this code?
    By MW130 in forum New To Java
    Replies: 11
    Last Post: 03-14-2013, 01:58 PM
  3. Errors in code
    By MikeJ39 in forum New To Java
    Replies: 0
    Last Post: 10-23-2011, 10:57 PM
  4. Help with errors in code
    By XodoX in forum New To Java
    Replies: 10
    Last Post: 02-08-2011, 05:14 AM
  5. code for making a java swing program a demo verson
    By fakhruddin in forum AWT / Swing
    Replies: 1
    Last Post: 11-27-2007, 08:54 PM

Posting Permissions

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