This discussion is archived
5 Replies Latest reply: Mar 26, 2013 10:21 AM by 998435 RSS

Transparent JSpinner

998435 Newbie
Currently Being Moderated
Hi!

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
    StanislavL Pro
    Currently Being Moderated
    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
    998435 Newbie
    Currently Being Moderated
    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();
        spinner.setOpaque(false);
    
        panel.add(spinner);
        getContentPane().add(panel);
      }
    
      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
    StanislavL Pro
    Currently Being Moderated
            spinner.setOpaque(false);
            spinner.getEditor().setOpaque(false);
            ((JSpinner.NumberEditor)spinner.getEditor()).getTextField().setOpaque(false);
    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
    Maxideon Explorer
    Currently Being Moderated
    These two methods together will make a component transparent under most look and feels.
    setOpaque(false);
    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())
                makeTransparent(child);
        }
        if(c instanceof JComponent) {
            JComponent c2 = (JComponent) c;
            c2.setOpaque(false);
            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
    998435 Newbie
    Currently Being Moderated
    I realised that the JSpinner consists of other components but I didn't think about the Editor.

    Thanks guys!

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points