Adding a label on top of a button (original) (raw)

Pavel Porvatov pavel.porvatov at oracle.com
Wed Aug 1 15:03:41 UTC 2012


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<JButton> 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 -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.openjdk.java.net/pipermail/swing-dev/attachments/20120801/c8aa0369/attachment.html> -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: image/png Size: 3936 bytes Desc: not available URL: <http://mail.openjdk.java.net/pipermail/swing-dev/attachments/20120801/c8aa0369/attachment.png>



More information about the swing-dev mailing list