Fix JLabel

I am writing an application that performs some task and informs the user about the successful completion of the task. To tell the user, I use jlabel. I want this jlabel to display a message and disappear after a while. I use netbeans as my IDE.

Here is the architecture of my classes.

Annotation, GUI Code

abstract class Admin extends JFrame{
  protected static jlabel lbl_message= new jlabel("some text");
  // other functions and variables

  abstarct protected void performButtonClickAction();

}

A class for implementing abstract functions and providing other functions.

final class AdminActionPerformer extends Admin{
  final public void performButtonClickAction(){
     // code for doin the task
     if(task is successful){
         new Thread(new Fader(Admin.lbl_message)).start();
     }
  }

 public static void main(String[] args) {
    new AdminActionPerformer().setVisible(true);
 }

}

Thread to create jlabel slack

class Fader implements Runnable{
  javax.swing.JLabel label;
  Color c;

  Fader(javax.swing.JLabel label){
    this.label=label;
    c=label.getBackground();
  }

  public void run() {
    int alpha=label.getGraphics().getColor().getAlpha()-5;
    while(alpha>0){
        System.out.println(alpha);
        alpha-=25;
        label.getGraphics().setColor(new Color(c.getRed(), c.getGreen(), c.getBlue(), alpha));
        label.repaint();
        try {
            Thread.sleep(50);
        } catch (InterruptedException ex) {
            Logger.getLogger(Fader.class.getName()).log(Level.SEVERE, null, ex);
        }
     }
  }
}

But the shortcut does not disappear. What am I doing wrong here? Thank:)

PS I set JLabel opaque. This is problem? I want to first display the shortcut with its background color, and then make it disappear.

+3
source share
5 answers

Johannes , , , . SwingWorker SwingUtilities.invokeLater, .

, , , JLabel , . JLabel , , , . ", , ".

: ? , . , , update() repaint(), , .

, , . . ! , , , , , -. , , , , . , .

+3

, swing, ​​ Trident. , .

   JLabel label = ....;
   Timeline timeline = new Timeline(label);
   timeline.addPropertyToInterpolate("background", label.getBackground(), 
     new Color(label.getBackground().getRGB(), true));
   timeline.addPropertyToInterpolate("foreground", label.getForeground(), 
      new Color(label.getForeground().getRGB(), true));
   timeline.play();

EDIT: /, . , , , / . , ...

+5

, -, .

+4

, API-...

com.sun.awt.AWTUtilities !: D

+1

enter a description of the link here I was also looking for a good way to change the contents of JLabel, fading out again. The following code works pretty well. It has no code to fade the icon. But it should not be too difficult. Note that this task requires the current version of TimingFramework (timeframework-swing-4.1).

I hope this can be helpful.

import org.jdesktop.core.animation.timing.Animator;
import org.jdesktop.core.animation.timing.TimingSource;
import org.jdesktop.core.animation.timing.TimingTargetAdapter;
import org.jdesktop.swing.animation.timing.sources.SwingTimerTimingSource;

import javax.swing.*;
import java.awt.*;
import java.util.concurrent.TimeUnit;

public class FadingLabel extends JLabel {
    protected Animator animator;
    protected String newLabelText;

    public FadingLabel(String s, Icon icon, int i) {
        super(s, icon, i);
        initAnimator();
    }

    public FadingLabel(String s, int i) {
        super(s, i);
        initAnimator();
    }

    public FadingLabel(String s) {
        super(s);
        initAnimator();
    }

    public FadingLabel(Icon icon, int i) {
        super(icon, i);
        initAnimator();
    }

    public FadingLabel(Icon icon) {
        super(icon);
        initAnimator();
    }

    public FadingLabel() {
        super();
        initAnimator();
    }

    protected void initAnimator() {
        final TimingSource ts = new SwingTimerTimingSource();
        Animator.setDefaultTimingSource(ts);
        ts.init();
        animator = new Animator.Builder().setDuration(250, TimeUnit.MILLISECONDS).setRepeatCount(2).setRepeatBehavior(Animator.RepeatBehavior.REVERSE).setStartDirection(Animator.Direction.BACKWARD).addTarget(new TimingTargetAdapter() {

            @Override
            public void repeat(Animator source) {
                setText(newLabelText);
            }

            @Override
            public void timingEvent(Animator animator, double fraction) {
                int alpha = new Double(255 * fraction).intValue();
                setForeground(new Color(getForeground().getRed(), getForeground().getGreen(), getForeground().getBlue(), alpha));
                repaint();
            }

        }).build();
    }


    @Override
    public void setText(String s) {
        if (animator != null && !animator.isRunning()) {
            newLabelText = s;
            animator.start();
        } else {
            super.setText(s);
        }
    }
}
+1
source

Source: https://habr.com/ru/post/1755098/


All Articles