(original) (raw)

Hi Sanjaya,
Hi all,
I want to add a label(D3) on right top corner of a button as below,Can anyone help please?


Inline image 1

There are many ways to do that. For example you can override the JButton#paintComponent method. Or you can use JLayer, see below:

import javax.swing.\*;
import java.awt.\*;

public class JLayerSample {
public static void main(String\[\] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();

JButton button = new JButton("Some button");

JLayer layer = new JLayer<>(button);

JPanel glassPane = new JPanel(new GridBagLayout());

glassPane.add(new JLabel("D3"), new GridBagConstraints(0, 0, 1, 1, 1, 1,
GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0),
0, 0));

layer.setGlassPane(glassPane);
glassPane.setOpaque(false);
glassPane.setVisible(true);

frame.add(layer);

frame.setDefaultCloseOperation(JFrame.EXIT\_ON\_CLOSE);
frame.setSize(600, 400);
frame.setVisible(true);

}
});
}
}

See more about JLayer here: http://docs.oracle.com/javase/tutorial/uiswing/misc/jlayer.html

Regards, Pavel