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

  • Home
  • SEARCH
  • 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 8529155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:58:02+00:00 2026-06-11T08:58:02+00:00

I have got a Java-Swing-GUI with a structure like this: JFrame — JPanel1 —–jButton1

  • 0

I have got a Java-Swing-GUI with a structure like this:
JFrame
— JPanel1
—–jButton1
—–jLabel1

My target is that when jButton1 is pressed I want to change the color of jLabel1 or set some text onto it, but this should be done in an external class (let’s call it externalClass).

So in the constructor of the GUI-class I pass itself to my external class:

public class GUIclass extends javax.swing.JFrame {  
    private externalClass e;
    public GUIclass() {
        initComponents();
        e = new externalClass(GUIclass.this);
}

In my external class I am not able to access e.g. the label:

private GUIclass g;
public externalClass(GUIclass g) {
    this.g = g;
    System.out.println(g.getComponentCount());
        // --> only 1, is this the JFrame (?)
    System.out.println(g.getComponent(0).getName());
        // always "null"
}

Could anybody please explain to me how I could get access to the label?
Also I am worried why the name of the component is always “null”.

Thanks in advance!

  • 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-11T08:58:03+00:00Added an answer on June 11, 2026 at 8:58 am

    You make the instance of the JLabel global in your JPanel, and you provide a getter in the JPanel for your JLabel.

    You pass the instance of the JPanel to your external class through the constructor.

    Somewhere in your external class, you use the JPanel instance to get the JLabel instance.

    JLabel label = panel.getJLabel();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Java (Swing), say I've got a 2D game where I have various types
I have got a project that is using jni to connect java wrapper and
I am new in Java and have got as a task to find out,
I'm a beginner in Java and I have got some problems with the static
I recently got into Java. I have a background in dynamic languages and I'm
I have got this code: function init(){ if (typeof window.jQuery !== 'function') { var
i have got <div><a class='link' href='index.php' data-container='target'>Load</a></div> <div class='target'></div> i need to write jquery
I have a java application for an ATC. I just got started with the
I have always wondered about this but quite never got to understand why. We
I know that I am missing something simple here. I have got this homework

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.