Question

In JSpinner class, how would I change the foreground color of two BasicArrowButton(up and down) components?
the component.setForeground(<<a_color>>) doesn't work.

Thanks in advance.
EDIT

private void set_colors(JSpinner spinner){
    int n = spinner.getComponentCount();
    for (int i=0; i<n; i++)
    {
        Component c = spinner.getComponent(i);
        System.out.println(c);
        if (c instanceof BasicArrowButton)
        {
            c.setForeground(ds_conn_text.getForeground());//doesn't work, doesn't change arrow color
            c.setBackground(ds_conn_text.getBackground());
            BasicArrowButton c0=(BasicArrowButton) c;c0.setBorder(ok_button.getBorder());
        }
    }
}
Was it helpful?

Solution

This is a follow-up question of Swing change the JSpinner back and fore colors

It is not possible to only set the color of the arrows without overriding the paint method. The reason is simply that the color for the arrows is the same color as the color that is used for the "shadows" of the buttons. So you could put the line

UIManager.getDefaults().put("controlDkShadow", Color.MAGENTA);

somewhere into your main, but this would not only change the arrow colors, but also other elements' colors which actually should not be changed.

An ugly/hacky way to set this color only for a particular instance would be

private static void hackilySetColor(JSpinner spinner, Color color)
{
    int n = spinner.getComponentCount();
    for (int i=0; i<n; i++)
    {
        Component c = spinner.getComponent(i);
        if (c instanceof BasicArrowButton)
        {
            try
            {
                Field field = BasicArrowButton.class.getDeclaredField("darkShadow");
                field.setAccessible(true);
                field.set(c, color);
                field.setAccessible(false);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
}

but this uses reflection, is really an ugly hack, and still replaces the color of the "dark shadow" border of the buttons.

You'll be better off with an own UI for things like this (or ... just don't change the color at all - this seems rather useless for me anyhow...)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top