This discussion is archived
8 Replies Latest reply: Oct 10, 2012 2:37 PM by anicer RSS

JTextField is not focused in my LAF during TAB pressing

anicer Newbie
Currently Being Moderated
Hi,
I have created my own LAF - see code below - and I do not know, why JTextField () is not focused while I am pressing TAB key (Windows 7). Please, write me, what I have missed to implement in my LAF. Thank you.

Camron
import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import javax.swing.plaf.basic.BasicLookAndFeel;
import java.awt.*;

public class Example extends JFrame {

    public Example() {
        setTitle("Simple example");

        JPanel panel = new JPanel();

        JButton RightButton = new JButton("Right");
        JButton LeftButton = new JButton("Left");

        JTextField myField = new JTextField("Some Text");

        add(BorderLayout.NORTH, myField);
        add(BorderLayout.EAST, RightButton);
        add(BorderLayout.WEST, LeftButton);

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    class MyLookAndFeel extends BasicLookAndFeel {
        public String getID() {
            return "MyFAF";
        }

        public String getName() {
            return "My Look and Feel";
        }

        public String getDescription() {
            return "Sample look and feel from Swing";
        }

        public boolean isNativeLookAndFeel() {
            return false;
        }

        public boolean isSupportedLookAndFeel() {
            return true;
        }

        protected void initSystemColorDefaults(UIDefaults table) {
            String[] defaultSystemColors = {
                    "white", "#FFFFFF",
            };

            loadSystemColors(table, defaultSystemColors, false);
        }

        protected void initComponentDefaults(UIDefaults table) {
            super.initComponentDefaults(table);

            FontUIResource commonFont = new FontUIResource("Tahoma", Font.PLAIN, 11);

            Object[] defaults = {
                    "Button.font", commonFont,
            };

            table.putDefaults(defaults);
        }

        protected void initClassDefaults(UIDefaults table) {
            super.initClassDefaults(table);

        }

    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel(
                            "MyLookAndFeel");

                } catch (Exception exp) {
                    System.out.println("Couldn't load My look and feel " + exp);
                }

                Example ex = new Example();
                ex.setMinimumSize(new Dimension(400, 300));
                ex.pack();
                ex.setLocationRelativeTo(null);
                ex.setVisible(true);
            }
        });
    }
}
  • 1. Re: JTextField is not focused in my LAF during TAB pressing
    Jörg Explorer
    Currently Being Moderated
    Just ran your code and got the exception in main(...). Maybe that's the reason why afterwards the focus behaved as usual.

    java 1.7.0_07, Win7.
  • 2. Re: JTextField is not focused in my LAF during TAB pressing
    anicer Newbie
    Currently Being Moderated
    Which type of exception should I implement in main() ?? Pls, send me some code (example). Tahnk you.
  • 3. Re: JTextField is not focused in my LAF during TAB pressing
    Jörg Explorer
    Currently Being Moderated
    anicer wrote:
    Which type of exception should I implement in main()
    I meant an exception in the main method was thrown: java.lang.ClassNotFoundException,
    so your LAF was not loaded. Just run your own example.
  • 4. Re: JTextField is not focused in my LAF during TAB pressing
    anicer Newbie
    Currently Being Moderated
    Ohh...Grrr. I Am sorry. There is a right compiled code:

    Example.java
    import javax.swing.*;
    import java.awt.*;
    
    public class Example extends JFrame {
        public Example() {
            setTitle("Simple example");
    
            JPanel panel = new JPanel();
    
            JButton RightButton = new JButton("Right");
            JButton LeftButton = new JButton("Left");
    
            JTextField myField = new JTextField("Some Text");
    
            add(BorderLayout.NORTH, myField);
            add(BorderLayout.EAST, RightButton);
            add(BorderLayout.WEST, LeftButton);
    
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    try {
                        UIManager.setLookAndFeel("MyLookAndFeel");
    
                    } catch (Exception exp) {
                        System.out.println("Couldn't load My look and feel " + exp);
                    }
    
                    Example ex = new Example();
                    ex.setMinimumSize(new Dimension(400, 300));
                    ex.pack();
                    ex.setLocationRelativeTo(null);
                    ex.setVisible(true);
                }
            });
        }
    }
    MyLookAndFeel.java
    import javax.swing.*;
    import javax.swing.plaf.FontUIResource;
    import javax.swing.plaf.basic.BasicLookAndFeel;
    import java.awt.*;
    
    public class MyLookAndFeel extends BasicLookAndFeel {
        public String getID() {
            return "MyFAF";
        }
    
        public String getName() {
            return "My Look and Feel";
        }
    
        public String getDescription() {
            return "Sample look and feel from Swing";
        }
    
        public boolean isNativeLookAndFeel() {
            return false;
        }
    
        public boolean isSupportedLookAndFeel() {
            return true;
        }
    
        protected void initSystemColorDefaults(UIDefaults table) {
            String[] defaultSystemColors = {
                    "white", "#FFFFFF",
            };
    
            loadSystemColors(table, defaultSystemColors, false);
        }
    
        protected void initComponentDefaults(UIDefaults table) {
            super.initComponentDefaults(table);
    
            FontUIResource commonFont = new FontUIResource("Tahoma", Font.PLAIN, 11);
    
            Object[] defaults = {
                    "Button.font", commonFont,
            };
    
            table.putDefaults(defaults);
        }
    
        protected void initClassDefaults(UIDefaults table) {
            super.initClassDefaults(table);
    
        }
    
    }
  • 5. Re: JTextField is not focused in my LAF during TAB pressing
    DrClap Expert
    Currently Being Moderated
    The UIManager.setLookAndFeel() method requires the name of the L&F class. That would be the full name including the package name, not just the simple name of the class.

    And if you didn't put your L&F class into a package, you should do that.
  • 6. Re: JTextField is not focused in my LAF during TAB pressing
    anicer Newbie
    Currently Being Moderated
    I have MyLookAndFeel in my Own Package. In this formum a put an example in CCSSE format for understanding my problem. Please, help me with prime problem: Why, JTextField is not focused during tab pressing???? I did not found any mistake in many of LAF. I have searched in this forum an on internet too few days and I am not able to fix it or find any adea how to find any approach.

    Thank you. Camron
  • 7. Re: JTextField is not focused in my LAF during TAB pressing
    DrClap Expert
    Currently Being Moderated
    That's supposed to be your SSCCE? Then your L&F isn't being loaded, because you didn't specify its class name properly.

    At any rate I don't see anything in that example which attempts to control focus, so I would expect the normal Swing focus rules to apply.
  • 8. Re: JTextField is not focused in my LAF during TAB pressing
    anicer Newbie
    Currently Being Moderated
    hm... And why I can not implement any fcocus listener if I press TAB and focus works for JButton instances? It is not clear for me. Focus for JButtons works well - without any implementation of focus control, but for JTextFilds I have to implement focus controling - why? Does not it dengerous swing functionality? Should I do that inside extended JTextFieldUI class or somewhere else?

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points