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 9215123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:08:00+00:00 2026-06-18T02:08:00+00:00

I am a beginner in Java & I am learning the swing api. I

  • 0

I am a beginner in Java & I am learning the swing api. I am having trouble with my code, and I can’t figure out how to fix it. Please help.

Below is an example of the code I am using:

import javax.swing.JFrame;
import javax.swing.JLabel;

public class HelloWorldApp extends JFrame {


  public static void main(String args[]) {

    CustomWindow w = new CustomWindow();
    w.setVisible(true);

  }

  public CustomWindow() {

    setSize(500, 500);
    setTitle("Jacob Perkins");
    JLabel lbl = new JLabel("Hello World!");
    add(lbl);

  }

}

My problem is that I get the following error:

invalid method declaration; return type required
  • 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-06-18T02:08:01+00:00Added an answer on June 18, 2026 at 2:08 am
    public CustomWindow() {
        setSize(500, 500);
        setTitle("Jacob Perkins");
        JLabel lbl = new JLabel("Hello World!");
        add(lbl);
    }
    

    should be:

    public void CustomWindow() {
        setSize(500, 500);
        setTitle("Jacob Perkins");
        JLabel lbl = new JLabel("Hello World!");
        add(lbl);
    }
    

    Or instead of void (for returning nothing) we would put the data type we wanted to return. The method would now have to include a return statement which returns an appropriate data type to match the method signature, or you will get the error you have:

    invalid method declaration; return type required

    See Returning a Value from a Method.

    Unless you are attempting to create a constructor for the HelloWorldApp class than the method must be given the class name (have a read on Providing Constructors for Your Classes):

    public HelloWorldApp() {
        setSize(500, 500);
        setTitle("Jacob Perkins");
        JLabel lbl = new JLabel("Hello World!");
        add(lbl);
     }
    

    Other suggestions

    • Dont extend JFrame unnecessarily.

    • Dont call setSize rather use correct LayoutManager and/or override getPreferredSize and return Dimensions which fit its contents. Than call pack() on JFrame before setting it visible but after adding components.

    • Create and manipulate Swing components on Event Dispatch Thread via SwingUtilities.invokeLater(Runnable r) block:

      SwingUtilities.invokeLater(new Runnable() {
          @Override
          public void run() {
              //create and show ui here
              CustomWindow w = new CustomWindow();
              w.setVisible(true);
          }
      });
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello :) I am beginner in Java Swing and I can't google solution for
I'm a beginner in Java. The question might be silly, but please help! I've
Ok, i wrote this pice of code as an beginner in java learning from
I am a Java beginner and I would appreciate it if someone here can
I have another beginner's question that hopefully someone can help with. I'm trying to
Hello guys I'am beginner of the Java and i've got some problems with array&arraylist.
I am a beginner in Java and is learning to use array. I understand
I'm a java beginner. I'm in trouble to configure a persistance unit using JTA
I am beginner to jsp & servlet. I am learning session handling in it.
Im a beginner on Java and I'm having problem using Comparable to sort Employee

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.