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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:54:51+00:00 2026-05-16T08:54:51+00:00

Why cant JLabel in java swing be declared inside the inner class like JMenu

  • 0

Why cant JLabel in java swing be declared inside the inner class like JMenu or JMenuBar

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class Chk extends JFrame 
{

private JLabel lbl ;

public Chk()
{

lbl = new JLabel("StatusBar");  
lbl.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
add(lbl,BorderLayout.SOUTH);


JMenuBar menubar=new JMenuBar();
JMenu file = new JMenu("File");
JMenu view = new JMenu("View");

JCheckBoxMenuItem sbar= new JCheckBoxMenuItem("Status-Bar");
sbar.setState(true);
sbar.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{


if (lbl.isVisible())
{lbl.setVisible(false);}
else
{lbl.setVisible(true);}


}});




menubar.add(file);
view.add(sbar);
menubar.add(view);
setJMenuBar(menubar);

setSize(300,200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}

public static void main(String args[])
{new Chk();}

}

in the above program why do i have to put this line “private JLabel lbl ;”
Why Cant i use JLabel lbl = new JLabel(“Label”);

  • 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-16T08:54:52+00:00Added an answer on May 16, 2026 at 8:54 am

    You can, but variables used in a closure need to be declared final.

        final JLabel lbl = new JLabel("StatusBar");
        lbl.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
        add(lbl, BorderLayout.SOUTH);
    

    This should work.

    In case you are wondering, the closure is the part where you create an instance of an anonymous inner class and refer to a variable declared in an enclosing scope. In this case ‘lbl’ is referenced from within an anonymous ActionListener instance:

        sbar.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                if (lbl.isVisible()) {
                    lbl.setVisible(false);
                } else {
                    lbl.setVisible(true);
                }
            }
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import java.awt.BorderLayout; import java.awt.FlowLayout; import javax.swing.*; public class FileCutter { public static void main(String[]
I have the following code: package example; import java.awt.BorderLayout; import java.awt.FlowLayout; import javax.swing.JFrame; import
I'm a starter at Java. I'd like to create a Form class that extends
I'm trying to hand-code a Java GUI using Swing and AWT. I'm using various
I cant believe fastest solution for a multilined JLabel is the following one (text
i cant access clicked object(this) parent's class.. click same elements and different returns? this
I'm building a Tic Tac Toe game in Java with a Swing GUI, and
I'd like to create a small Java applet that displays a message and a
We have a controller where we have a pre-declared JList and JLabel that we
In our Java Swing application, we're loading a custom font and adding it to

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.