8

I am making a text-based game where the user inputs text to solve the game. I decided to use java swing to display the text, and I want to have the background of the textPane to be black. I have tried everything I have found (commented out), but non of it seems to work.

    private JTextPane blackJTextPane() {
    //JTextPane area = new JTextPane();
    //area.setBackground(Color.BLACK);
    //area.setForeground(Color.WHITE);
    JEditorPane area = new JEditorPane();

      Color bgColor = Color.BLACK;
      UIDefaults defaults = new UIDefaults();
      defaults.put("EditorPane[Enabled].backgroundPainter", bgColor);
      area.putClientProperty("Nimbus.Overrides", defaults);
      area.putClientProperty("Nimbus.Overrides.InheritDefaults", true);
      area.setBackground(bgColor);

   return area;
  }
public Everything(){
    super("Game");
    try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    } catch (Exception exc) {

        // ignore error
    }
    setSize(600,500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    BorderLayout layout = new BorderLayout(); 
    setLayout(layout);
    setVisible(true);

    text = new JLabel("");
    text.setText("Text:");

    texts = new JTextField(20);
    texts.setBackground(Color.white);
    texts.setText("");

    JPanel panel = new JPanel();

    panel.add(text );
    panel.add(texts);

    texts.addActionListener(this);
    add(panel, BorderLayout.SOUTH);


    UIDefaults defs = UIManager.getDefaults();
    defs.put("TextPane.background", new ColorUIResource(Color.BLACK));
    defs.put("TextPane.inactiveBackground", new ColorUIResource(Color.BLACK));


    area = blackJTextPane();

    area.setEditable(false);

    style = area.addStyle("style", null);
    //StyleConstants.setBackground(style, Color.black);

    JScrollPane pane = new JScrollPane(area);
    add(pane, BorderLayout.CENTER);

    doc = area.getStyledDocument();

    button.addActionListener(this);

    setVisible(true);







}

The imports are not pictured here but there are no errors in the game when I run it with any of those commented out parts.

Slushy_G
  • 137
  • 2
  • 9

1 Answers1

11

You may have an issue with Nimbus not respecting the background color settings. Try this to override the color:

  JEditorPane area = new JEditorPane();

  Color bgColor = Color.BLACK;
  UIDefaults defaults = new UIDefaults();
  defaults.put("EditorPane[Enabled].backgroundPainter", bgColor);
  area.putClientProperty("Nimbus.Overrides", defaults);
  area.putClientProperty("Nimbus.Overrides.InheritDefaults", true);
  area.setBackground(bgColor);
tenorsax
  • 21,123
  • 9
  • 60
  • 107
  • @Slushy_G put it in `blackJTextPane()` instead of `area.setBackground(Color.BLACK);` – tenorsax Mar 26 '14 at 23:44
  • Gives errors, i will update code to show new version – Slushy_G Mar 26 '14 at 23:58
  • @Slushy_G it should be `area` not `areas`. – tenorsax Mar 27 '14 at 00:09
  • Yes, but if I change area over to an JEditorPane it gives errors on the return statement, and when I try to use Style and getStyledDocument() – Slushy_G Mar 27 '14 at 00:14
  • 2
    @Slushy_G I did not notice you're were using `JTextPane`. Change init to `JTextPane`, and use `defaults.put("TextPane[Enabled].backgroundPainter", bgColor);` – tenorsax Mar 27 '14 at 00:21
  • For me it needs this command `defaults.put("TextPane.background", new ColorUIResource(bgColor));` instead of `defaults.put("TextPane[Enabled].backgroundPainter", bgColor);` – Panayotis Jun 26 '19 at 13:48