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).