2

I have a program with quite a few JDialogs, JFileChoosers and other components containing JButtons.

I know I could go from JButton to JButton and set .focusPainted(false) to make focus invisible, but the problem is that I have a bunch of "automatically" created components (Predefined JDialogs, JFileChoosers), and I don't have access to their JButton. I'd have to create whole components manually to be able to control their sub-components (and creating a fileChooser from scratch has to be difficult)...

However, I have an idea. Everytime a JButon is created, focusPainted is set to true by default. Is there a way I could find the Java library containtng original JButton constructor(s) and change the method to be false there, making that the default?

EDIT: I found src.zip and extracted AbstractButton.java. How do I edit and recompile it?

Actually, the question is, how do I edit Java standard libraries?

P.S. All you folks saying it's not a good practise, what could go wrong with this specific example (I don't think that anyting in JSL depends on whether focus is painted on a Jbutton or not)? And I could always work on two project simultaniously, one with original libaries, and one with edited, and when I get error in one, but not in the other, I know where's the problem lies.

Karlovsky120
  • 6,212
  • 8
  • 41
  • 94
  • You could replace java core classes using `-Xbootclasspath` option, but that is not a good practice. More info on it [here](http://blogs.sourceallies.com/2010/02/replacing-and-patching-java-application-and-core-classes/) or [here](http://media.techtarget.com/tss/static/articles/content/CovertJava/Sams-CovertJava-15.pdf) – cubanacan Sep 12 '12 at 11:37
  • 1
    why do you want to confuse your users? – kleopatra Sep 12 '12 at 11:45
  • 2
    I often see questions about fighting the L&F and default behaviour: IT IS A BAD PRACTICE! Platform L&F are made to mock as close as possible the default L&F of the platform so that the user enjoys an experiment similar, if not exactly, as any other application. Ask yourself: "why do I want to go against what the user expects?" – Guillaume Polet Sep 12 '12 at 11:58
  • Is your application multi-platform, or more precisely, does it use different Look-and-feels (L&Fs) or only one? In the latter case, you seemingly need to subclass that L&F and disable the rendering of the focus rectangle; then, let the application load your customized L&F on start. Never done it myself, though. – Goblin Alchemist Sep 12 '12 at 12:59
  • Can I disable focus only on default Java L&F? I must admit it looks rather ugly. This was mostly aestetical question. Focus may look better on other L&Fs, though (but I'd like to make possible for user to choose between L&Fs)... – Karlovsky120 Sep 12 '12 at 16:58

2 Answers2

0

What about to inherit from JButton and user your own Button class where you set focusPainted to false in the constructor you use. You do NOT want to change standard Java code.

public class MyButton extends JButton{

  public MyButton() {
    super();
    setFocusPainted(false);
  }

}

But I see, this will not change the focus on your FileChooser Buttons.

Simulant
  • 19,190
  • 8
  • 63
  • 98
0

Create a custom look-and-feel by subclassing the look-and-feel you are currently using. It's enough to define two classes: the look-and-feel itself and the button UI delegate. The look-and-feel should put the button delegate into the common table, replacing the original button delegate. The delegate should override the method drawing the focus rectangle witn an empty method so that focus rectangles are not drawn. Also, it's necessary to override the static method in the delegate which creates a delegate instance.

public class MyLookAndFeel extends WindowsLookAndFeel {

    protected void initClassDefaults(UIDefaults table){
        super.initClassDefaults(table);
        table.put("ButtonUI","package1.package2.MyButtonUI");
    }

}

public class MyButtonUI extends WindowsButtonUI {

    private static MyButtonUI thiz;

    public static ComponentUI createUI(JComponent c) {
        if(thiz==null) thiz=new MyButtonUI();
        return thiz;
    }

    protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect, Rectangle textRect, Rectangle iconRect){}

}

When your application starts, activate your customized look-and-feel:

UIManager.setLookAndFeel("package1.package2.MyLookAndFeel");

This removes the focus rectangle not only from buttons you create manually, but also from buttons in JOptionPane, in file dialogs etc.

Goblin Alchemist
  • 829
  • 5
  • 11