JSpinner
For some reason I like to make the textfield of a JSpinner component transparent, but
((javax.swing.JSpinner.NumberEditor)jSpinner1.getEditor()).getTextField().setOpaque(false);
doesn't do the job. I can change the background color with
((javax.swing.JSpinner.NumberEditor)jSpinner1.getEditor()).getTextField().setBackground(new Color(150,150,255));
but I don't get why it won't be transparent.
Any help is heighly appreciated.
Stephan
> I'm also working with Linux and Java 5.0, but the
> background is always white. I tried different L&F,
> but same thing.
Here's the test program I have used:
import javax.swing.*;
import java.awt.*;
import static java.awt.BorderLayout.*;
public class SwingTester{
JFrame frame;
Container con;
public SwingTester(Component c1, Object constraints1,
Component c2, Object constraints2,
Component c3, Object constraints3){
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
con = frame.getContentPane();
con.add(c1, constraints1);
if (c2 != null){
con.add(c2, constraints2);
}
if (c3 != null){
con.add(c3, constraints3);
}
frame.setSize(500, 500);
frame.setVisible(true);
}
public SwingTester(Component c1, Object constraints1,
Component c2, Object constraints2){
this(c1, constraints1, c2, constraints2, null, null);
}
public SwingTester(Component c1, Object constraints1){
this(c1, constraints1, null, null, null, null);
}
public static void main(String[] args){
SpinnerNumberModel snm = new SpinnerNumberModel(100, 0, 1000, 1);
JSpinner js = new JSpinner(snm);
((JSpinner.NumberEditor)js.getEditor()).getTextField().setOpaque(false);
JTextField jtf = new JTextField("Akaraka Sanbara");
jtf.setOpaque(false);
new SwingTester(js, NORTH, jtf, SOUTH);
}
}
hiwaa at 2007-7-16 1:50:12 >

> Thanks hiwa,
>
> I have added the following line to your code:
> > con.setBackground(new Color(150,150,255));
>
>
> If you run the code now you will see that the text
> field is transparent, but the Spinner component is
> gray. You actually have the same issue.
>
> Maybe it is a bug in the JSpinner component?!
I remember I have seen the source code of JSpinner and its UI classes a few years ago.
Basically, a JSpinner is a composite component and its editor is a JFormattedTextField on a
JPanel. That is, background of the opaque==false textfield is the JPanel's background, not
the content pane of the JFrame. So, it is not a bug.
The whole JSpinner is a more complex composite component. In order to see the content
pane BG color through the spinner textfield, you should do:
((JSpinner.NumberEditor)js.getEditor()).getTextField().setOpaque(false);
((JComponent)((JSpinner.NumberEditor)js.getEditor()).getTextField().getParent()).setOpaque(false);
((JComponent)((JSpinner.NumberEditor)js.getEditor()).getTextField().getParent().getParent()).setOpaque(false);
hiwaa at 2007-7-16 1:50:12 >
