Reputation: 353
Edit (it's not the opaque attribute that's causing the problem, it's updating the JLabel's background attribute): I am using a MouseMotionListener to setText() for a JLabel to whatever the mouse's current position is. The JLabel starts out with the correct background color/transparency at first running of the program. Whenever the text/mouseMotion is updated the JLabel is no longer transparent.
Updated runnable code:
For example:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.TextArea;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class MouseTester extends JFrame {
public static void main(String[] args) {
try {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MouseTester.createMouseTester();
}
});
} catch (Throwable t) {
System.exit(1);
}
}
private static MouseTester mt = null;
private JLabel mouseLocation = null;
private static Color labelBackgroundColor = new Color(0, 0, 0, 127);
private static Color labelForegroundColor = Color.WHITE;
public static void createMouseTester() {
if (mt != null)
return;
mt = new MouseTester();
mt.setVisible(true);
}
private MouseTester() {
super();
mt = this;
setResizable(true);
Dimension dScreen = Toolkit.getDefaultToolkit().getScreenSize();
setMinimumSize(new Dimension(Math.min(800, dScreen.width), Math.min(590,
dScreen.height)));
setSize(getMinimumSize());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mouseLocation = new JLabel(" Lat/Long ");
mouseLocation.setOpaque(true);
mouseLocation.setBackground(labelBackgroundColor);
mouseLocation.setForeground(labelForegroundColor);
mouseLocation.setToolTipText("The MGRS coordinates.");
Component textArea = new TextArea("Move mouse here to see mouse motion info...");
// Add a mouse motion listener to capture mouse motion events
textArea.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent evt) {
TextArea source = (TextArea) evt.getSource();
// Process current position of cursor while all mouse buttons are up.
mouseLocation.setText(source.getText() + "\nMouse moved [" +
evt.getPoint().x + "," + evt.getPoint().y + "]");
mouseLocation.setBackground(labelBackgroundColor);
mouseLocation.setForeground(labelForegroundColor);
mouseLocation.setOpaque(true);
mouseLocation.repaint();
}
public void mouseDragged(MouseEvent evt) {
}
});
// Add the components to the frame; by default, the frame has a border layout
mt.add(textArea, BorderLayout.NORTH);
mouseLocation.setOpaque(true);
mouseLocation.setBackground(labelBackgroundColor);
mouseLocation.setForeground(labelForegroundColor);
mt.add(mouseLocation, BorderLayout.SOUTH);
int width = 300;
int height = 300;
mt.setSize(width, height);
}
}
The JLabel starts out transparent/slightly grey then changes with the mouse motion to not transparent and entirely black. The transparency is determined in the background color.
I've pretty much tried changing the background color everywhere I could think of, but it's not working..
I would like it to remain the color the entire time (the color that it has at startup).
Upvotes: 1
Views: 3054
Reputation: 205785
I'm not sure why you're changing the label's transparency. Making the label opaque and adjusting it's background saturation may be sufficient, as shown below.
A few notes on your implmentation:
setSize()
sparingly.import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class MouseTester extends JFrame {
private static MouseTester mt;
private static Color labelBackgroundColor = Color.gray;
private static Color labelForegroundColor = Color.white;
private JLabel mouseLocation;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
MouseTester.createMouseTester();
}
});
}
public static void createMouseTester() {
mt = new MouseTester();
mt.setVisible(true);
}
private MouseTester() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mouseLocation = new JLabel("Lat/Long", JLabel.CENTER);
mouseLocation.setOpaque(true);
mouseLocation.setBackground(labelBackgroundColor);
mouseLocation.setForeground(labelForegroundColor);
mouseLocation.setToolTipText("The MGRS coordinates.");
JTextArea textArea = new JTextArea(
"Move mouse here to see mouse motion info...");
textArea.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent me) {
mouseLocation.setText("Mouse moved ["
+ me.getX() + ", " + me.getY() + "]");
}
});
this.add(textArea, BorderLayout.CENTER);
this.add(mouseLocation, BorderLayout.SOUTH);
this.pack();
this.setSize(320, 240); // content placeholder
this.setLocationRelativeTo(null);
}
}
Upvotes: 3
Reputation: 1593
You have declared that your JLabel is opaque, which means that it is fully responsible for drawing its own background. Yet you have set it's background color to a semi-transparent color. That is a contradiction and is the cause of your problem.
You can fix the appearance of your JLabel by using mt.repaint();
instead of mouseLocation.repaint();
thus forcing a re-draw of the area of the entire panel behind the JLabel (in gray) followed by a re-draw of the JLabel in your semi-transparent color.
If you want to avoid the cost of re-painting your entire mt
object, then you need to nest your JLabel inside some smaller component which you can redraw quickly.
Upvotes: 4
Reputation: 109813
You have to call JLabel#repaint()
for switching from Opaque(true)
to Opaque(false)
and vice versa, for every of MouseEvents
that fired from (Xxx)MouseListeners
, because this method missed in the API, rest is here with description by @kleapatra
Upvotes: 3