Using a JSlider to change JLabel background

I have set up a JSlider to change the opacity of a JLabel's background. I have found that the only way to make the change take effect, is to toggle the visibility of the JLabel in the stateChanged method. Why is this? Shouldn't just setting the JLabel background to a new color work? Try running this with and without the last couple lines in stateChanged:

package slidertest;

import java.awt.BorderLayout;

import java.awt.Color;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JSlider;

import javax.swing.SwingConstants;

import javax.swing.event.ChangeEvent;

import javax.swing.event.ChangeListener;

publicclass Mainextends JFrame{

privatestatic Main instance =new Main();

private JLabel colorLabel =new JLabel(" ");

public Main(){

colorLabel.setOpaque(true);

colorLabel.setBackground(Color.green);

JPanel mainPanel =new JPanel(new BorderLayout());

mainPanel.add(colorLabel, BorderLayout.NORTH);

mainPanel.add(createOpacityColorSlider(colorLabel), BorderLayout.CENTER);

this.add(mainPanel);

}

publicstaticvoid main(String[] args){

instance.setSize(200,200);

instance.setVisible(true);

}

staticfinalint ALPHA_MIN = 0;

staticfinalint ALPHA_MAX = 255;

/**

* @param style

* @param colorLabel

* @return

*/

private JSlider createOpacityColorSlider(final JLabel colorLabel ){

JSlider opacitySlider =new JSlider(SwingConstants.HORIZONTAL,

ALPHA_MIN, ALPHA_MAX, colorLabel.getBackground().getAlpha() );

//Turn on labels at major tick marks.

opacitySlider.setMajorTickSpacing(20);

opacitySlider.setMinorTickSpacing(10);

opacitySlider.addChangeListener(new ChangeListener(){

publicvoid stateChanged(ChangeEvent e){

JSlider source = (JSlider)e.getSource();

int alpha = source.getValue();

Color newColor =new Color(colorLabel.getBackground().getRed(), colorLabel.getBackground().getGreen(), colorLabel.getBackground().getBlue(), alpha);

colorLabel.setBackground(newColor);

//will not work without doing this:

colorLabel.setVisible(false);

colorLabel.setVisible(true);

}

});

return opacitySlider;

}

}

-Eric

[4258 byte] By [elevinea] at [2007-11-26 16:41:57]
# 1

See the Evaluation of this bug.

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6461996

This is not a bug. Swing does not support setting a translucent background color in combination with setOpaque(true). If you want to set a background color with alpha, you need to setOpaque(false), and take care of filling the component's background yourself.

Rodney_McKaya at 2007-7-8 23:09:01 > top of Java-index,Desktop,Core GUI APIs...
# 2

//will not work without doing this:

colorLabel.setVisible(false);

colorLabel.setVisible(true);

have you tried in stead of those lines putting in this

colorLabel.repaint();

that line tells the button that somthing has changed and needs to re draw its self

Nibura at 2007-7-8 23:09:01 > top of Java-index,Desktop,Core GUI APIs...
# 3

> have you tried in stead of those lines putting in

> this

>

> colorLabel.repaint();

>

> that line tells the button that somthing has changed

> and needs to re draw its self

Did you read my reply?

This is not supported by Swing.

Rodney_McKaya at 2007-7-8 23:09:01 > top of Java-index,Desktop,Core GUI APIs...
# 4
@Rodney_McKayThanks for the response, thats exactly what I wanted to know. I think its easier to just stick with my hack :)-Eric
elevinea at 2007-7-8 23:09:01 > top of Java-index,Desktop,Core GUI APIs...
# 5

> > have you tried in stead of those lines putting in

> > this

> >

> > colorLabel.repaint();

> >

> > that line tells the button that somthing has

> changed

> > and needs to re draw its self

>

> Did you read my reply?

> This is not supported by Swing.

sorry i was typing it while you posted and only once i posted it i saw yours...

Nibura at 2007-7-8 23:09:01 > top of Java-index,Desktop,Core GUI APIs...