Results 1 to 5 of 5
  1. #1
    TigerLvr is offline Member
    Join Date
    Jan 2015
    Posts
    3
    Rep Power
    0

    Question run() cannot implement run() in java.lang.Runnable? + other issues

    IDE: BlueJ

    I'm creating a small program to count the number of mouse clicks the user is inputting into a BlankArea for a school assignment. I just finished with making the program look nice and all so I was putting in a MouseListener to detect the clicks. However, when I tried adding a MouseListener to the code, the error in the title appeared. I searched for possible fixes but I couldn't find any that applied to my situation.
    I'd also appreciate tips/links to sites to tidy up my code.

    Here's the code:

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import javax.swing.border.*;
    import java.awt.event.*;
    
    
    public class CountClick
    {
       JTextArea textArea = new JTextArea();
       JScrollPane scrollPane = new JScrollPane(textArea);
       JLabel labelOne = new JLabel("Click inside the colored box to find out how many times you clicked.", SwingConstants.CENTER);
       BlankArea blankArea = new BlankArea(Color.lightGray);
            
        public void addComponentsToPane(Container pane) 
        {
            pane.setLayout(null);
            
            //Settings for Text Area
            textArea.setEditable(false);
            textArea.setLineWrap(true);
            TitledBorder titleTwo = new TitledBorder("");
            textArea.setBorder(titleTwo);
            //Settings for Scroll Bar
            scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
            //Settings for Instructions
            TitledBorder titleThree = new TitledBorder("Instructions");
            labelOne.setBorder(titleThree);
            //Settings for Blank Area(mouse click listener)
            TitledBorder titleOne = new TitledBorder("");
            blankArea.setBorder(titleOne);
            //Adds a MouseListener to BlankArea
            blankArea.addMouseListener(new CustomMouseListener());
            
            
            pane.add(textArea);
            pane.add(scrollPane);
            pane.add(labelOne);
            pane.add(blankArea);
            
            
            Insets insets = pane.getInsets();
            Dimension size = labelOne.getPreferredSize();
                labelOne.setBounds(10 + insets.left, 7 + insets.top, 86 + size.width, 20 + size.height);
             
            size = textArea.getPreferredSize();
                textArea.setBounds(10 + insets.left, 267 + insets.top, 373 + size.width, 175 + size.height);    
                 
            size = blankArea.getPreferredSize();
                blankArea.setBounds(11 + insets.left, 77 + insets.top, 472 + size.width, 175 + size.height);
                 
    
        }     
        
         private void createAndShowGUI()
        {
            //Creates main window and sets default close operation
            JFrame mainFrame = new JFrame("Buttons and Words");
            mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             
            //Adds all components to main window
            addComponentsToPane(mainFrame.getContentPane());
             
            //Main Window Dimensions
            Insets insets = mainFrame.getInsets();
            mainFrame.setSize(500 + insets.left + insets.right, 500 + insets.top + insets.bottom);
            mainFrame.setResizable(false);
            mainFrame.setVisible(true);
        }
        
        public static void main(String[] args) 
        {
            //Thread safe and GUI display
            javax.swing.SwingUtilities.invokeLater(new Runnable() 
            {
                public static void run() 
                {
                    createAndShowGUI();
                }
            });
        }  
        
        class CustomMouseListener implements MouseListener
        {
            public void mouseClicked(MouseEvent e)
            {
                textArea.setText("Mouse Clicked: " + e.getClickCount() + " times.");
            }
            
            public void mousePressed(MouseEvent e) 
            {
            }
    
            public void mouseReleased(MouseEvent e) 
            {
            }
    
            public void mouseEntered(MouseEvent e) 
            {
            }
    
            public void mouseExited(MouseEvent e) 
            {
            }
        }
    
       
     }
    Here's what's going on:
    • Compiling causes error as described in Title
    • Java Code:
      createAndShowGUI()
      cannot be referenced from a static context appears
    • Java Code:
      public static void main(String [] args)
      contains the thread safe and program previously launched without mouse functionality(program appeared when there were no MouseListener components and thread safe had all static method stuff).


    Please help me.
    Last edited by TigerLvr; 01-22-2015 at 08:44 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: run() cannot implement run() in java.lang.Runnable? + other issues

    You need to learn the difference between a static method and a non-static method.

    The Runnable interface defines a non-static run() method, but you're trying to implement a static run() method. That's your first problem.

    Your second problem is that main() is a static method, but you're trying to call a non-static method from it. You need to create an instance of CountClick before you can access non-static methods of that class.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

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

    Default Re: run() cannot implement run() in java.lang.Runnable? + other issues

    Here's another suggestion to shorten your code. Since you are declaring a class to implement MouseListener, you should just extend MouseAdapter. That way, you don't have to include the dummy methods of the listener interface.

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

  4. #4
    TigerLvr is offline Member
    Join Date
    Jan 2015
    Posts
    3
    Rep Power
    0

    Default Re: run() cannot implement run() in java.lang.Runnable? + other issues

    Quote Originally Posted by KevinWorkman View Post
    You need to learn the difference between a static method and a non-static method.

    The Runnable interface defines a non-static run() method, but you're trying to implement a static run() method. That's your first problem.

    Your second problem is that main() is a static method, but you're trying to call a non-static method from it. You need to create an instance of CountClick before you can access non-static methods of that class.
    Thank you very much! It took a while but I finally figured out where to put the instance :).

  5. #5
    TigerLvr is offline Member
    Join Date
    Jan 2015
    Posts
    3
    Rep Power
    0

    Default Re: run() cannot implement run() in java.lang.Runnable? + other issues

    Quote Originally Posted by jim829 View Post
    Here's another suggestion to shorten your code. Since you are declaring a class to implement MouseListener, you should just extend MouseAdapter. That way, you don't have to include the dummy methods of the listener interface.

    Regards,
    Jim
    Thank you! I noticed MouseAdapter while looking for the syntax for MouseListener but I thought it was for another purpose.

Similar Threads

  1. Replies: 1
    Last Post: 10-23-2014, 01:57 AM
  2. java.lang.String cannot be cast to java.lang.Boolean
    By putapepe in forum New To Java
    Replies: 7
    Last Post: 04-06-2014, 10:34 PM
  3. hi how can i convert this java class to runnable jar
    By RichersooN in forum New To Java
    Replies: 1
    Last Post: 01-31-2012, 03:13 AM
  4. Replies: 5
    Last Post: 03-17-2011, 02:26 PM

Tags for this Thread

Posting Permissions

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