0

Here is what I've tried in my attempt to change a vertical JSeparator's color from Nimbus default black to red based on the answer in How to change the color of a JSeparator?:

public class TestFrame extends JFrame {

    public static void main(String[] args) {

        TestFrame frame = new TestFrame();
        frame.setSize(200, 200);
        frame.setLayout(new GridBagLayout());

        for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                try {
                    UIManager.setLookAndFeel(info.getClassName());
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(TestFrame.class.getName()).log(Level.SEVERE, null, ex);
                } catch (InstantiationException ex) {
                    Logger.getLogger(TestFrame.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(TestFrame.class.getName()).log(Level.SEVERE, null, ex);
                } catch (UnsupportedLookAndFeelException ex) {
                    Logger.getLogger(TestFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
                break;
            }
        }
        UIManager.put("Separator.background", Color.red);
        UIManager.put("Separator.foreground", Color.red);

        JSeparator separator = new JSeparator(JSeparator.VERTICAL);
        separator.setPreferredSize(new Dimension(2, 100));
        separator.setForeground(Color.red);
        separator.setBackground(Color.red);

        frame.add(separator, new GridBagConstraints());
        frame.setVisible(true);

    }

}

Yet the vertical separator remains black. What should I be doing?

Note: I know Nimbus is the issue, because I tried without setting the L&F to Nimbus and this worked fine. Also to note that setting the Separator[Enabled].backgroundPainter property seems to have affected the JSeperator but not in the way I intended (just changed the background color vs the separating line color)

Community
  • 1
  • 1
Sammy Guergachi
  • 1,986
  • 4
  • 26
  • 52
  • 1
    Possible duplicate of [How to change the color of a JSeparator?](http://stackoverflow.com/questions/13083876/how-to-change-the-color-of-a-jseparator) – Jonny Henly Aug 19 '16 at 19:27
  • It's not a duplicate @JonnyHenly since the answer there only works for Metal L&F not Nimbus – Sammy Guergachi Aug 19 '16 at 19:28
  • @SammyGuergachi That post does not work with the UI, but rather with component directly. It should work regardless of installed Look and Feel. – Mordechai Aug 19 '16 at 19:34
  • @SammyGuergachi if you read the comments to the answer of the question I linked, one of them says that you have to set opaque to true via `separator.setOpaque(true)`. If you do that then your separator will be red. – Jonny Henly Aug 19 '16 at 19:40
  • @JonnyHenly If I set the width of the JSeperator big enough, you will see red and the default nimbusBlueGrey as two vertical stripes on the JSeperator because setting the background color doesn't handle the JSeperator's shadow which would be handled by 'setForeground()'. So far I've had success changing the nimbusBlueGrey color, but the problem is that color is used in many other components, so I'm not sure how to contain it to just the specific component. – Sammy Guergachi Aug 19 '16 at 21:05

2 Answers2

1

I resolved this by changing the nimbusBlueGrey color that Nimbus uses to derive other colors. Setting the separator to opaque will only help change the background color, but JSeperator's have 2 colors, a foreground and a background, so setting to opaque and changing the background color fixed half the problem. nimbusBlueGrey seems to handle the foreground color, which doesn't seem to be overridable with setForegroundcolor() or the Separator.foreground property.

The problem is that changing nimbusBlueGrey will affect the color of many other components. I'm not sure how to contain the color change to just the JSeperator.

Sammy Guergachi
  • 1,986
  • 4
  • 26
  • 52
0
  /**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Windows look and feel instead of NIMBUS*/
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
      /*Change This Line To Make Your TextField Transparent */      if ("WINDOWS".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(Furious.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Furious.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Furious.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Furious.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Furious().setVisible(true);
        }
    });
}

Just Change Your Look and feel from NIMBUS to WINDOWS,it worked fine for me.

Here is Snapshot Of My UI:

Here is Snapshot Of My UI

mkl
  • 90,588
  • 15
  • 125
  • 265