I have a java application that somehow has a different behavior when launched in windows by double clicking the jar file compared to launching it using the command prompt.
The behavior that i'm noticing specifically is when I override a JLabel to render better using the following
lblDate = new ATimeLabel(ATimeLabel.DATE_LETTERS) {
private static final long serialVersionUID = 1L;
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
RenderingHints.VALUE_FRACTIONALMETRICS_ON);
super.paintComponent(g2d);
}
};
The effect appears when I launch the app from my IDE or launch the jar from the command prompt but when I double click my app it will show the label without the paintComponent() overriden effects.
would appreciate help to figure out exactly how to be able to have the same effect happen on double click of my app.
EDIT:
I should also mention that I add the following font changes after creating one of 2 JLabels
lblDate.setForeground(Color.gray);
lblDate.setFont(boldFont.deriveFont(Font.PLAIN, timeFontSize));
Here is a screen shot of what it looks like. The one on the left clearly has the anti-aliasing and clean text rendering i'm looking for while the one on the right is fatter and not as sharp. (I also temporary added the red border to show that the paint method is in effect)
EDIT 2:
Seems that when I double click the JVM is 1.7 and my IDE uses JDK 1.6 I don't understand why the RenderingHints for the fonts wouldn't look the same on Java6 and Java7 they are in both APIs and run without exceptions...
Any help would be appreciated.
Seems to work just fine for me...
From left to right, in IDE, from command line and double clicked...

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestPaintLabel {
public static void main(String[] args) {
new TestPaintLabel();
}
public TestPaintLabel() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JLabel lblDate = new JLabel("Hello, is it me you're looking for?") {
private static final long serialVersionUID = 1L;
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
RenderingHints.VALUE_FRACTIONALMETRICS_ON);
super.paintComponent(g2d);
g2d.setColor(Color.RED);
g2d.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
g2d.dispose();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 50);
}
};
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
frame.add(lblDate);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
Try adding some additional painting to determine if the paint method is actually been called.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With