5 Replies Latest reply on Mar 26, 2013 5:21 PM by 998435

    Transparent JSpinner


      I'm trying to create a transparent JSpinner on top of a JPanel with an image as background. Apparently setOpaque(false) isn't enough to acheive this and so I'm wondering if there's another way to do it. Can I override some paint method for the JFormattedTextField or customize some UI class?

      Any help appreciated!
        • 1. Re: Transparent JSpinner
          For me setOpaque(false) is enough. Could you provide SSCCE to illustrate your problem?

          I has something similar and the problem was L&F related when it was filled with background without opacity check.
          • 2. Re: Transparent JSpinner
            Thanks for your reply!

            Here's a short example:
            import java.awt.Graphics;
            import javax.swing.ImageIcon;
            import javax.swing.JFrame;
            import javax.swing.JPanel;
            import javax.swing.JSpinner;
            public class SpinnerTest extends JFrame {
              String background = "bkg.jpg";
              public SpinnerTest() {
                ImageIcon imageIcon = new ImageIcon(background);
                setSize(imageIcon.getIconWidth(), imageIcon.getIconHeight());
                JPanel panel = new JPanel() {
                  public void paintComponent(Graphics g) {
                    g.drawImage(new ImageIcon(background).getImage(), 0, 0, null);
                panel.setBounds(0, 0, imageIcon.getIconWidth(), imageIcon.getIconHeight());
                JSpinner spinner = new JSpinner();
              public static void main(String[] args) {
                new SpinnerTest().setVisible(true);
            What I would like to see is the JPanel background bkg.jpg through the JSpinner.
            • 3. Re: Transparent JSpinner
              Just remember spinner is in fact set of components. It has editor (a panel) which in turn holds a editorfield (JFormattedTextField) and 2 buttons.
              • 4. Re: Transparent JSpinner
                These two methods together will make a component transparent under most look and feels.
                setBackground(new Color(0,0,0,0);
                Some components, like the JSpinner, are composed of several sub-components that would also need to be made transparent. So just use a helper method that recursively calls the above two methods on each subcomponent.
                public static void makeTransparent(Component c) {
                    if(c instanceof Container) {
                        for(Component child : ((Container) c).getComponents())
                    if(c instanceof JComponent) {
                        JComponent c2 = (JComponent) c;
                        c2.setBackground(new Color(0,0,0,0));
                Pass the JSpinner or any JComponent to the above method and it should be made 'transparent' under most LAFs (some of them ignore the set background color).
                • 5. Re: Transparent JSpinner
                  I realised that the JSpinner consists of other components but I didn't think about the Editor.

                  Thanks guys!