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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:05:37+00:00 2026-05-30T17:05:37+00:00

I need a listener that will constantly check if a static boolean value has

  • 0

I need a listener that will constantly check if a static boolean value has been changed so that I can repaint a component on a frame. Can someone please help me I really don’t know much about listeners and haven’t worked with them much? Help will be greatly appreciated.

edit(more clarity): I have two separate classes in which on class is the “main frame” the second class is an extension of JLabel and implements MouseListner for a “clickable photo”. The “main frame” creates instances of the photo and when the photo is clicked the “main frame” is supposed to paint on the panel a description of the photo. This is “main frame”

MenuBar menuBar;
static AnnotationVisual a;
Picture pic;
Picture pic2;

GalleryScreen(int rows, int columns){
    this.setBorder(BorderFactory.createEmptyBorder(500,500,0,0));
    pic = new Picture("pic1", "Z:/My Documents/Downloads/Ball.jpg", new Coordinate(0,0));
    pic2 = new Picture("pic2", "Z:/My Documents/Downloads/hoop.jpg" , new Coordinate(1,0));

    this.add(pic);
    this.add(pic2);
    a = new AnnotationVisual();
}

public void paintComponent(Graphics g){
    super.paintComponent(g);
    if(a.shouldAnnotate()){
        FontMetrics size= g.getFontMetrics(); 
        if(getWidth()>=(a.dispX()+size.stringWidth(a.annotationText()))){
            g.setColor(Color.white);
            g.fillRect(a.dispX()-3,a.dispY()-12,size.stringWidth(a.annotationText())+5,15);
            g.setColor(Color.black);
            g.drawRect(a.dispX()-3,a.dispY()-12,size.stringWidth(a.annotationText())+5,15);
            g.drawString(a.annotationText(), a.dispX(), a.dispY());
        }else{
            String sub="";
            int letters=0;
            g.setColor(Color.white);
            g.fillRect(a.dispX()-3,a.dispY()-12,getWidth(),15);
            g.setColor(Color.black);

            for(int i=0;i<a.annotationText().length();i++){
                if(a.dispX()+letters+16<=getWidth()){
                    sub+=a.annotationText().substring(i,i+1);
                    letters=size.stringWidth(sub);
                }else{
                    sub=sub+"...";
                    i=a.annotationText().length();
                }
            }
            g.drawRect(a.dispX()-3,a.dispY()-12,size.stringWidth(sub)+3,15);
            g.drawString(sub,a.dispX(),a.dispY());
        }
    }
}

public static AnnotationVisual getA()
{
    return a;
}

This is “clickable photo”

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.*;
import javax.swing.*;

public class Picture extends JLabel implements MouseListener
{
    String myAnnotation;
    String filePath;
    Coordinate imageCoord;
    private boolean wasDoubleClick;
    private Timer timer;
    EditAnnotation newEdit; 
    AnnotationVisual newVisual;

    public Picture(String annotation, String filePath, Coordinate coord)
    {
        super(new ImageIcon(filePath));
        this.addMouseListener(this);
        myAnnotation=annotation;
        this.filePath = filePath;
        imageCoord = coord;
        newEdit = new EditAnnotation(annotation);
        newVisual = new AnnotationVisual();
    }
    public Picture(String filePath)
    {
        super(new ImageIcon(filePath));
        this.addMouseListener(this);
        this.filePath = filePath;
        newEdit  = new EditAnnotation();
        newVisual = new AnnotationVisual();
    }
    public String getAnnotation()
    {
        return myAnnotation;
    }
    public AnnotationVisual getAnnotationVisual()
    {
        return newVisual;
    }
    public void setAnnotation(String annotation)
    {
        myAnnotation=annotation;
    }
    public Coordinate getCoordinate()
    {
        return imageCoord;
    }
    public void setCoordinate(Coordinate coord)
    {
        imageCoord = coord;
    }
    public Dimension getSize()
    {
        return new Dimension(super.getIcon().getIconWidth(), super.getIcon().getIconHeight());
    }
    public void mouseClicked(MouseEvent e)
    {
        final int scrLocX = (int)e.getLocationOnScreen().getX();
        final int scrLocY = (int)e.getLocationOnScreen().getY();

        if (e.getClickCount() == 2) 
        {
            wasDoubleClick = true; 
        } 
        else if(e.getClickCount() == 1)
        {
            Integer timerinterval = (Integer) Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval"); 

            timer = new Timer(timerinterval.intValue(), new ActionListener() 
            {
                public void actionPerformed(ActionEvent evt) 
                {
                    if (wasDoubleClick) 
                    {
                        GalleryScreen.getA().deleteAnnotation();
                        myAnnotation = newEdit.getAnnotation();
                        newEdit.show(myAnnotation);
                        wasDoubleClick = false;
                    } 
                    else 
                    {
                        GalleryScreen.getA().deleteAnnotation();
                        GalleryScreen.getA().showAnnotation(scrLocX, scrLocY , myAnnotation);
                    } 
                }
            });
            timer.setRepeats(false);
            timer.start();
        }
    }
    public void mouseEntered(MouseEvent e)
    {

    }
    public void mouseExited(MouseEvent e)
    {

    }
    public void mousePressed(MouseEvent e)
    {

    }
    public void mouseReleased(MouseEvent e)
    {

    }
}

AnnotationVisual is the thing that supposed to pop up when single clicked

  • 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-30T17:05:40+00:00Added an answer on May 30, 2026 at 5:05 pm

    You don’t set a listener on a field in Java, you set it on a property. While properties (according to the JavaBeans spec) can be fields, they’re usually done as pairs of methods (one getter, one setter; the latter being not needed for read-only fields) as that lets you hook extra logic in to be called when the property is accessed. Such as firing a listener callback to say that the value has changed. (You could use a thread to monitor for that sort of thing, but that’s really nasty and error-prone. Wasteful too.)

    One thing to be aware of though: you don’t know what thread the value will have been modified from. Take care when invoking back into Swing…

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

Sidebar

Related Questions

I need to write simple TCP listener that will be able to listen TCP
I'm 10000000% sure that this question has been asked before, however, the majority of
I need to do this: Value Change Listener to JTextField I'm trying the solution
How can I detect that a client has disconnected from my server? I have
I need the context to ApplicationContext.xml ,which I provided in web.xml as <listener> <listener-class>
Need a function that takes a character as a parameter and returns true if
Need to an expression that returns only things with an I followed by either
Need a function like: function isGoogleURL(url) { ... } that returns true iff URL
I am building a logging DLL that will simplify EntLib 5 Logging Application Block.
I'm new to actionscript. I'm trying to make a menusystem that will jump 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.