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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:15:11+00:00 2026-06-02T03:15:11+00:00

I have a Java class and I want one of its properties to be

  • 0

I have a Java class and I want one of its properties to be displayed by a JLabel in a Swing desktop application:

class Item {
    private String name;
    private Integer quantity;

    // getters, setters...
}

class Frame {
    Item item = new Item();
    ...

    JLabel label = new JLabel();
    label.setText(item.getQuantity().toString());
    ...
}

How do I get the label to update its text whenever the quantity property changes on the item?

  • 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-02T03:15:13+00:00Added an answer on June 2, 2026 at 3:15 am

    Something will have to update the text of your label (with the setText method you already know). Probably the easiest thing is to let the Item class fire PropertyChangeEvents when its properties are changed, and attach a listener to the item which updates the label.

    final JLabel label = new JLabel();
    label.setText(item.getQuantity().toString());
    item.addPropertyChangeListener( new PropertyChangeListener(){
       @Override
       public void propertyChange( PropertyChangeEvent event ){
         if ( "quantity".equals( event.getPropertyName ) ){
            //I assume this happens on the EDT, otherwise use SwingUtilities.invoke*
            label.setText( (String)event.getNewValue() );
         }
       }
    });
    

    The PropertyChangeSupport class makes it easy to manage the listeners and fire the events in your Item class

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Java. I want to have a setter method of one class that
Let's say I have a class and I want one of its methods to
I want to have immutable Java objects like this (strongly simplified): class Immutable {
If I have a java class which is package-private (declared with class, not public
In Java i have abstract class named Operation and three its subclasses called OperationActivation,
I have this class: private static class ClassA{ int id; String name; public ClassA(int
I have a java class and I need to debug it (put breakpoints and
I have a java class containing all the columns of a database table as
I have a Java class MyPojo that I am interested in deserializing from JSON.
I have a Java class, Node as follows : class Node { public ArrayList<Node>

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.