0

So, I have a JTextArea in my program which uses Nimbus LAF. I need to swap it for JTextPane because of some functionality issues.

However, JTextArea has a painted border by default. JTextPane does not. I do not know which is the JTextArea's default border to set it to JTextPane.

I tried with getBorder(), but that only returned "javax.swing.plaf.synth.SynthBorder@455e3f91"

How do I get default JTextBoreder to JTextPane?

Karlovsky120
  • 6,212
  • 8
  • 41
  • 94

2 Answers2

2

I guess you are looking for this:

UIManager.getDefaults().getBorder("TextArea.border");
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • This isn't working: someTextPane.setBorder(UIManager.getDefaults().getBorder("TextArea.border")); – Karlovsky120 Oct 11 '12 at 16:47
  • @Karlovsky120 to check NimbusDefaults, and take from my profile Painter for Nimbus (there isn't GradientPainter == endless lazyness), – mKorbel Oct 11 '12 at 16:49
  • UIManager.getDefaults().getBorder("TextArea.border"); returns null. Are you sure that's the right key? – Karlovsky120 Oct 11 '12 at 17:18
  • @Karlovsky120 Actually, yes it is the right key, but in the case of Nimbus, it uses a BorderPainter as explained by mKorbel. Take a look at NimbusDefaults. – Guillaume Polet Oct 11 '12 at 17:44
  • I understand Nimbus has a different way of painting graphics so they work on all resolutions, but I am not sure how to create that border... To what method should I pass the above line as an argument? – Karlovsky120 Oct 11 '12 at 20:13
2

After having Nimbus hand it to me for months with these * Painters, I emerge victorious.

Please note that the keys used can change between operating systems (so no guarantee there, but it does work on mine). So long as you have a valid key for the border of JTextArea you can transfer it to your JTextPane.

import java.awt.Insets;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;


public class NimbusBorderPainting extends Box{

    public NimbusBorderPainting(){
        super(BoxLayout.Y_AXIS);
        try {
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (UnsupportedLookAndFeelException e2) {
            e2.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //Retrieve the TextArea painter from the defaults 
        Object o = UIManager.get("TextArea[Enabled+NotInScrollPane].borderPainter");        

        //Transfer the Painter to a TextPane key
        UIDefaults paneDefaults = new UIDefaults();
        paneDefaults.put("TextPane.borderPainter",o);

        JTextPane pane = new JTextPane();
        pane.setMargin(new Insets(10, 10, 10, 10));

        //Apply the new UI to your text pane
        pane.putClientProperty("Nimbus.Overrides",paneDefaults);
        pane.putClientProperty("Nimbus.Overrides.InheritDefaults",false);
        pane.setText("Lots of Text\nWell, as much as I'm willing to type\n");
        add(pane);

    }

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new NimbusBorderPainting());
        frame.validate();
        frame.pack();
        frame.setVisible(true);
    }

}
Nick Rippe
  • 6,465
  • 14
  • 30