Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7603429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:39:14+00:00 2026-05-30T23:39:14+00:00

I am having difficulty trying to add JButton s to my JFrame . I

  • 0

I am having difficulty trying to add JButtons to my JFrame.

I have created two methods (all within the same class for now). If I make the showGUI method static, then I receive errors:

//Listen for actions on buttons.
next.addActionListener(this); (CANNOT USE THIS IN A STATIC CONTEXT)
previous.addActionListener(this); (CANNOT USE THIS IN A STATIC CONTEXT)
classify.addActionListener(this); (CANNOT USE THIS IN A STATIC CONTEXT)

and when adding the JButton objects to my JFrame, I receive the following errors:

add(next); (Cannot make a static reference to the non-static method add(Component) from the type Container)
add(previous); (Cannot make a static reference to the non-static method add(Component) from the type Container)
add(classify); (Cannot make a static reference to the non-static method add(Component) from the type Container)

How can I overcome this? I have included my method below for reference:

public void showGUI(BufferedImage img){

    next = new JButton("Next Image");
    next.setMnemonic(KeyEvent.VK_N);
    next.setActionCommand("disable");

    previous = new JButton("Previous Image");
    previous.setMnemonic(KeyEvent.VK_P);
    previous.setActionCommand("disable");

    classify = new JButton("Classify");
    classify.setMnemonic(KeyEvent.VK_C);
    classify.setActionCommand("disable");

    //Listen for actions on buttons.
    next.addActionListener(this);
    previous.addActionListener(this);
    classify.addActionListener(this);

    add(next);
    add(previous);
    add(classify);

    //Display image on the screen.
    frame.setTitle("TITLE");
    RMHC newContentPane = new RMHC();
    newContentPane.setOpaque(true);

    frame.setContentPane(newContentPane);
    frame.getContentPane().setLayout(new FlowLayout());
    frame.getContentPane().add(new JLabel(new ImageIcon(img)));
    frame.pack();
    frame.setVisible(true);
    frame.isResizable();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T23:39:16+00:00Added an answer on May 30, 2026 at 11:39 pm

    I think the problem because you call the method showGUI from main method which is static, so the best thing is to initializing GUI Frame in main method (also in EDT), like:

    public class MainApp {
        public static void main(String... args) {
            EventQueue.invokeLater(
                new Runnable() {
                    @Override
                    public void run() {
                        JFrame frame= new YourFrame();
                        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        frame.setVisible(true);
                    }
                }
            );
        }
    }
    
    class YourFrame () extends JFrame implements ActionListener{
        public YourFrame() {
        }
    
        public void showGUI(BufferedImage img){
        }
    
        private void add(JButtob button) {
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to tackle this problem , but I am having difficulty
Having difficulty articulating this correlated subquery. I have two tables fictitious tables, foo and
So, I'm having difficulty trying to append/add a child div to the parent div,
im having some difficulty trying to pull out a specific value from a cookie.
Trying to convert the following but am having difficulty. Can anyone see where I'm
I'm having a great deal of difficulty trying to figure out the logic behind
I'm having difficulty using MooseX::Declare properly when calling BUILDARGS. I'm trying to create an
I'm trying to use this library (which looks very nice) but I'm having difficulty
I'm trying to learn the MapKit with Monotouch and I'm having difficulty figuring out
I'm having difficulty with Javascript instance variables. I'm trying to make an easy way

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.