Results 1 to 5 of 5

Thread: How to write a Focus Listener in Java?

  1. #1
    Join Date
    Aug 2006
    Posts
    201

    How to write a Focus Listener in Java?

    Hi Friends,
    I not have much knowledge about the Java. Now I want to get the focus where the user will type the next character and for that I want to use Focus Listener in my coding.!! But I don't know about that. So please tell me how to write a Focus Listener in Java.?? Also please tell me the methods that are used by the focus listener, if any. Hoping that someone would help me faster.!!
    Don't be afraid of darkness, it's in your heart anyway.

  2. #2
    Join Date
    Jul 2006
    Posts
    289

    Re: How to write a Focus Listener in Java?

    Focus events are fired whenever a component gains or loses the keyboard focus. This is true whether the change in focus occurs through the mouse, the keyboard, or programmatically. From the user's point of view, the component with the keyboard focus is generally prominent — with a dotted or black border, for example. The window containing the component is also more prominent than other windows onscreen.
    Signatures reduce available bandwidth

  3. #3
    Join Date
    Aug 2006
    Posts
    227

    Re: How to write a Focus Listener in Java?

    A component generally gains the focus when the user clicks it, or when the user tabs between components, or otherwise interacts with a component. A component can also be given the focus programmatically, such as when its containing frame or dialog-box is made visible. The window displays a variety of components. A focus listener, registered on each component, reports every focus-gained and focus-lost event.
    I do to dead flowers what people at morgues do to dead people. Suck all the moisture out, dip them in plastic, paint them up pretty and put them in a nice frame.

  4. #4
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to write a Focus Listener in Java?

    The following sample of example may help you :

    Code:
    public class FocusEventDemo ... implements FocusListener ... {
        public FocusEventDemo() {
            ...
            JTextField textField = new JTextField("A TextField");
            textField.addFocusListener(this);
            ...
    	JLabel label = new JLabel("A Label");
    	label.addFocusListener(this);
            ...
            JComboBox comboBox = new JComboBox(vector);
            comboBox.addFocusListener(this);
            ...
            JButton button = new JButton("A Button");
            button.addFocusListener(this);
            ...
            JList list = new JList(listVector);
            list.setSelectedIndex(1); //It's easier to see the focus change
                                      //if an item is selected.
            list.addFocusListener(this);
            JScrollPane listScrollPane = new JScrollPane(list);
            
            ...

  5. #5
    Join Date
    Jul 2006
    Posts
    442

    Re: How to write a Focus Listener in Java?

    The following sample code shows how this operation can be done :
    Code:
        JFrame frame = new JFrame("Trial");
        JPanel panel = new JPanel(new BorderLayout());
    
    
        JButton button = new JButton("This is Demo");
        panel.add(button);
        frame.getContentPane().add(panel);  
    
        frame.pack(); 
        button.requestFocusInWindow(); 
        frame.setVisible(true);
    "When they give you ruled paper, write the other way..." J.R.J.

Similar Threads

  1. How to write a Mouse Listener in Java?
    By PsYcHo 1 in forum Software Development
    Replies: 4
    Last Post: 13-02-2010, 03:47 AM
  2. How to write a Key Listener in Java?
    By N I C K in forum Software Development
    Replies: 5
    Last Post: 13-02-2010, 01:52 AM
  3. How to write an Item Listener in Java?
    By shivendra in forum Software Development
    Replies: 4
    Last Post: 13-02-2010, 01:13 AM
  4. How to write a Container Listener in Java?
    By Pratim in forum Software Development
    Replies: 4
    Last Post: 12-02-2010, 06:43 AM
  5. How to write an Action Listener in Java?
    By Beter 2 Burn Out in forum Software Development
    Replies: 5
    Last Post: 12-02-2010, 05:44 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,713,088.61168 seconds with 16 queries