Giving focus to a component

Hi all,
How does one give focus back or just plain giving focus to a JComponent
e.g. JTextField, JButton, etc.
This puzzles, me please help?

That method was introduced in 1.4, but the API recommends you use requestFocusInWindow()

Similar Messages

  • Giving focus to the program

    I need to make a Java program give focus to itself.
    I'm thinking of a thread that would give focus to the program every second.
    But I dont know how to make it "give focus to this program on the computer". With my searches, I can only find about giving focus to a component of the program, not the program in the computer/os
    The program will run in Windows, so something platform specific would be ok.
    Or any other way to make this Java program regain focus automatically about every second.

    In brief, I need to do it for a barcode system. It's somewhat complicated, the commerce system uses php and is online. In this system, I added an applet which recieves events from a local program (the one I want to auto-focus).
    This local program recieves events from the barcode scanner. But a barcode scanner is stupid, and considered like a keyboard for the computer, so a program must be in focus to recieve the events. The best solution I could think of is to make this local program always takes the focus. The interface of this program would be small (or no interface at al).
    All of this system is done and works correctly. The only piece of this puzzle that is missing is having the local program always recieve events from the scanner.
    The computer that will run it is the "cash regitster" computer for the store, so having this local program always gaining focus is correct. And if needed, this "auto-focus" could be deactivated while the computer is needed for other things.
    So the real point is maning sure the local program gains focus about every second so it recieves the events of the scanner, since the scanner is considered like a keyboard.

  • How to programmatically bring focus to a component

    How can i programmatically bring focus to a component

    [url http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Component.html#requestFocus()]myComponent.requestFocus()

  • Can someone explain how to set the focus to a component

    Hello,
    What is the best way to ensure that a component gets the focus with code. I have tried requestFocus and requestFocusInWindow and none of them seems to do there work. The frame containing the components that should get the focus is displayed, the component is focusable so that is not the problem. What I found out is that if the component is deep in a nesting of panes (where the top pane is in the frame) is that calling requestFocus or requestFocusInWindow does not give the component where this focus is called on the focus but either the pane that contains it, one of the parent panes of this pane or one of the components in these panes, in almost all cases the component requesting the focus does not get the focus at all. I always have to write a work around in defining a focus listener for all these panes that transfer the focus back to the component that requested the focus originally, I can't believe that this is the way to ensure that a component has the focus via code. I have searched for documentation but found nothing on what requestFocus(inWindow) is actually dooing (a lot about focus traversibility but I don't want to control this, just make sure that a component gets the focus).
    Again I'm pretty sure that the toplevel window is activated and that the component that requested the focus is focusable (I had created a derived version of the DefaultFocusManager that showed who has focus). Personally I think (and I'm not alone if you do a search in this forum on focus management) that there is either a big problem in the documentation of focus management or that it is still not possible to even try to set the focus on a component in a easy way.
    Marc

    Hello,
    thanks for your reply, here is some example code that illustrates the problem I have (the actual code is to big to show here, which is the raison why I did not posted it here). The code wil create a frame with a JPanel with a JTextField, a 'New' Jbutton and a 'Delete' Jbutton, a tabbed pane in the center ('New' will create a new JPanel, add it to the tabbed pane and set the focus on it, either by calling RequestFocusInWindow() directly or by calling it via InvokeLater (someone on this forum suggested to use this). I changed also the focusmanager to show the component that has the focus when you type something. To test compile and run the program (either with a call to requesteFocusInWindow directly or indirectly (by changing the comments), on my system I get the following behaviour:
    1. requestFocusInWindow called directly
    Start program en type, the tabbed pane has the focus
    Press New and type something, a button has the focus not the created panel
    Press New again and type something, a button has the focus not the created panel
    2. requestFocusInWindow be called indirectly
    Start program and type, the tabbed pane has the focus
    Press New and type, the created panel has the focus (I thought at one moment that I had found the solution there, but alas ...)
    Press New again and type something, a button has again the focus.
    Note that with my real program it is not always a button that will have the focus, sometimes it is a combobox or a TextField (I tried to get this with the example code (the JTextField) but did not succeed.
    Hopes this clarify the problem a little bid, what I want is when the window is visible to set focus on newly created panes (or in the real program on the panel that is selected via the tabbed pane or via a key combination to shift the focus between the panels) but requestFocusInWindow seems to behave unpredicatable. I can't imagine that what I see is a bug so I must make some assumptions which are not valid but I don't see them, btw listening on the WindowListener is I think not the solution because the frame is already displayed when I want to set the focus and I do not use a modal dialog box.
    The example code follows here
    Marc
    * Main.java
    * Created on December 21, 2004, 8:58 AM
    package testfocus;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    * @author marc
    public class Main
    static int counter;
    /** Creates a new instance of Main */
    public Main()
    * @param args the command line arguments
    public static void main(String[] args)
    JFrame frm=new JFrame();
    JPanel content=new JPanel();
    frm.setContentPane(content);
    final JTabbedPane paneContainer=new JTabbedPane();
    JPanel bar=new JPanel();
    JButton create=new JButton(new AbstractAction("New")
    public void actionPerformed(ActionEvent ev)
    counter++;
    final JPanel pnl=new JPanel();
    pnl.setName("test"+counter);
    paneContainer.add(pnl);
    pnl.setFocusable(true);
    pnl.requestFocusInWindow();
    /* Either use this or the previous line
    SwingUtilities.invokeLater(new Runnable()
    public void run()
    pnl.requestFocusInWindow();
    JButton delete=new JButton(new AbstractAction("Delete")
    public void actionPerformed(ActionEvent ev)
    int i=paneContainer.getComponentCount()-1;
    if (i>=0)
    paneContainer.remove(i);
    JTextField dummy=new JTextField("Test");
    bar.add(dummy);
    bar.add(create);
    bar.add(delete);
    content.setLayout(new BorderLayout());
    content.add(bar,BorderLayout.SOUTH);
    content.add(paneContainer,BorderLayout.CENTER);
    frm.setSize(300,400);
    FocusManager.setCurrentManager(new DefaultFocusManager()
    public void processKeyEvent(Component component,KeyEvent ev)
    System.out.println("Focus owner"+getFocusOwner());
    super.processKeyEvent(component,ev);
    frm.setVisible(true);
    }

  • How to set Focus for one component on Dialog

    Hi all.
    I have a problem and need a help
    I have a JDialog
    I add some components on it. Includes button, textfield...
    after i set
         _textField.requestFocus()When Dialog show, it always focus at first component, not at the component i set.
    Please help me.
    Thanks in advance
    Diego

    Perhaps Swings threading ruins your focus request, and trying invokeLater may be worthwhile...
    Note that [requestFocusInwindow()|http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.html#requestFocusInWindow()] is to be preferred over requestFocus according to the API docs.
    Edited by: isocdev_mb on Mar 4, 2010 6:05 AM

  • Setting focus on a component from backing bean

    Is there an easy way to set focus in a specific component on the jspx from a method in the backing bean? (other than javaScript coding...)

    Hi,
    did you take a look at
    About control the cursor position in a JSF web page
    It contains some links to other weblogs. The links are helpfull. Maybe you'll be able to find your solution there......
    Good luck
    Luc Bors

  • Focus cell editor component of JTable when starting editing by typing

    Hello, everybody.
    We all know that the JTable component doesn't actually focus the actual cell editor component when editing is started by typing. But I need this functionality in an application of mine. So, I ask: is there a way to transfer focus to the editor component when editing is started by typing?
    Thank you.
    Marcos

    Well, I think that I've found it: JTable#setSurrendersFocusOnKeystroke.
    Marcos

  • Focus the third component after the current focus owner

    Hi,
    I have a component which consists of a textfield with two buttons right of it. I have several of these components placed on a panel. I want, when enter pressed, the focus moved from a textfield, to the second textfield in line. I tried this by calling
    KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent();three times but after going to 1 focusNextComponent it stops. Visually, it goes to the next but when I call
    KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
    it is 1 step behind. Do I have to update anything when calling focusNextComponent()?
    Much thanks,
    Hugo

    Will this help?
    Execute the programme and press enter and the focus is gone to the third component.
    import java.awt.*;
    import java.util.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class FocusTest extends JFrame implements KeyListener
         JTextField jtf1,jtf2,jtf3;
         JPanel p;
         public FocusTest()
              jtf1 = new JTextField(10);
              jtf2 = new JTextField(10);
              jtf3 = new JTextField(10);
              jtf1.addKeyListener(this);
              p       = new JPanel();
              p.add(jtf1);
              p.add(jtf2);
              p.add(jtf3);
              getContentPane().add(p);
         public static void main(String dd[])
              FocusTest ft = new FocusTest();
              ft.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              ft.setSize(200,200);
              ft.setVisible(true);
         public void keyPressed(KeyEvent ke)
              if(ke.getKeyCode() == KeyEvent.VK_ENTER)
                   System.out.println("ll");
                   jtf3.requestFocus();
         public void keyReleased(KeyEvent ke)
         public void keyTyped(KeyEvent ke)
    }

  • Give focus to a component

    Hi,
    In my Backing bean code, i change the selected line on my table in a function.
    The line is selected but there is no column "focused".
    I want the focus to be on the 3rd column of the table...
    How can i do this ?
    Regards,
    Julien

    chk
    How to focus on an ADF component using a javascript
    http://blogs.oracle.com/jdevotnharvest/entry/how_to_set_the_initial_component_focus

  • How to set focus to required component in the application

    Hi all,
    Based on user events, I want to set focus dynamically to required component/panel in the application.
    Is this possible? is yes, pls give me api for that!

    [http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JComponent.html#requestFocusInWindow()]
    A panel is not focusable by default.
    If you want a panel to get the focus you have to use panel.setFocusable(true) first.

  • How to manage focus in custom component

    Hallo. As you know some flex components have native focus, like buttons, textinput etc.. I have created a custom component which is a borderContainer.. As you know, by default, if user press TAB my BorderContainer will never receive the focus.. I tryied to set some property like setFocus() and tabFobusEnabled to true but i still have some focus problems with my borderContainer .. It is like my App knows that my Custom component is not a native component.. Is there a way for my component to receive focus without problems??.. Which is the best way for doing that??
    Thx a lot
    Max

    Components must implement IFocusManagerComponent in order to receive focus.

  • How to set focus on a Component?

    I'm doing a simple DATA ENTRY program. My web apps contains the ff:
    - 4- buttons (add, save, undo, del)
    - table
    To avoid mistakes on the data entry by a user. I make it a point to limit possible areas of risk. Like:
    ie: when addButton is clicked.
    - we set addButton.setDISABLED(true)
    - also we set delButton.setDISABLED(true)
    Problem: On the web browser, we get an error that saysn "Can't move to the focus control ... blah, blah blah"
    How do I set the focus on other control?

    I suppose this is a javascript error.
    If you are using firefox, could you please add what the error is that you are seeing.
    How are you trying to set the focus on the other button?
    You could try adding javascript code to onclick functionality of the button you are disabling.
    something like:
    onclick = "<client id of the button to have focus>.focus()";

  • Giving "focus" to a folder in Mail: only with the keyboard?

    Hello everybody,
    I've found that in Mail, you can choose a folder with the mouse, but that you cannot activate it, you cannot give it the focus.
    When you click on a folder, it becomes pale blue and the arrow keys do not allow to go from one folder to the other. The focus is in the mails pane.
    The only way to give the focus to a folder is through the keyboard, using Shift-Tab (or Tab several times): the folder then becomes dark blue, it "has the focus", and the arrow keys allow to go from one folder to the other, or to open and close folders.
    This is an interface issue, and is highly unusual. Am I the only one to observe this?
    If I am the only one, then I have a bug somewhere. If not, Mail has an interface problem that I will report to Apple, hoping for the best.
    Thanks for your input, and Cheers!
    Roger the Photographer
    G5 1.8Dual, 2GB RAM   Mac OS X (10.4.8)   20" App Dsplay, ext. Lacie, iPod, Bach on Al-Lansing, Canon 5D, Canon L glass

    Hello Roger.
    What you’ve described is exactly how it works.
    Although I agree that this is non-standard behavior,
    I have the feeling that it’s intentional, not a bug.
    There are other situations in Mail where the view
    that handles the keystrokes is what the user is most
    likely to want rather than what has focus.
    If you have the preview pane visible, for example,
    and the view that has focus is the message list, the
    arrow keys let you select another message, but the
    Page Up/Down keys are handled by the preview instead,
    regardless of whether it has focus or not. Only if
    the preview pane is hidden does the message list
    respond to Page Up/Down keystrokes.
    Hi David,
    I've just discussed the thing rather extensively with an ex-senior scientist at Apple. I think that he is right - as you are -, this is intended behaviour.
    Whether it is a good idea or not is a different matter however. Apple's keyboard commands have never been as complete, or precise, as Microsoft's. Remark that this is not a compliment to Microsoft whose interface is at times simply terrible! But here, it's not the keyboard command policy that is not systematic, it's the response of the mouse. And this I find very surprising. I have 51 mailboxes in Mail and the more control I have, the happier I am, that's for sure!
    On the other hand, what surprised me is the fact that the mouse does not respond in the same way in, say, Mail and iTunes. In iTunes, if you click on a "folder" (they call them playlists, which is fine, but this is about the same as a folder is it not?), not only you select it, but you activate it: you give it the focus plain and simple.
    Look at Font Book: here also, you have what a power user is waiting for: clicking on a "folder", or a list or group, you give it the focus plain and simple. Idem in Address Book. iPhoto also, but then it's the tab key behaviour that's strange.
    I can see from here programming teams clashing over points like this one. I would greatly prefer that Apple's applications adopt a predictable behaviour from one app to the other.
    "Behaviour by design", all right. I will nonetheless send to Apple what we could call an "enhancement request".
    Cheers!
    Roger the Photographer
    G5 1.8Dual, 2GB RAM   Mac OS X (10.4.8)   20" App Dsplay, ext. Lacie, iPod, Bach on Al-Lansing, Canon 5D, Canon L glass

  • Give sole focus to a component

    I would like to have a JDialog always maintain focus and prevent any action to other components (eg. menu items, and mouse listener in other components).
    I tried adding a WindowFocusListener to the JDialog and having it call requestFocus() in windowFocusLost(), but that did not seem to solve the problem.
    I also have a JPanel (the is underneath the JDialog) that also calls requestFocus() whenever there is mouse motion (because to it has a keyboard listener). But even without the requestFocus(), the mouse listener in the JPanel is still reponding to actions.

    You want to use a [url http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html#overview]modal dialog

  • Requesting focus to a component

    Hi All,
    I got some JTextFields in a JDialog derived class to enter user information and one button. If the button is clicked without entering information in the JTextFields, a message box is displayed using JOptionPane.showMessageDialog.
    I want to change the focus to the first JTextField once the messagedialog box is disposed. How can I do it?
    Thanks
    roger

    Hi aswath,
    I called the message box from my JDialog derived class like this.
    javax.swing.JOptionPane.showMessageDialog(this,
    "", "", javax.swing.JOptionPane.ERROR_MESSAGE);
    this.m_jTextField.requestFocus();
    After that I called requestFocus().
    When I called the requestFocus the messagebox showed is the active window.
    But it is not working. Can anybody help?
    roger

Maybe you are looking for

  • Need help, Photo appear sync in itunes but there are no photos on my ipad 2

    Hi, I need some advise, recently upgraded my itunes to iTunes 10 and at the same time I added new photos to my laptop( running on windows vista) pictures file. I wanted to sync the photos from my laptop to my ipad, but in the end, on my itunes, it ap

  • Execute BI Query in Web Dynpro Java

    Dear All, I have a requirement where i have to display of traffic light that sets its color based on the outcome of a SAP BEx query using Web Dynpro Java. Please explain me the steps how to retrieve and execute the SAP BEx query  using WDJ. Thanks &

  • Merging PDF Files in Preview 5 in OS X v10.7 Lion

    I recently updated to the OSX v10.7 Lion operating system.  In the previous version I had, I was able to merge two or more PDF files in Preview just by dragging the thumbnail.  However, in the Lion version, it is not allowing to drag one PDF file int

  • Sending mail to  SAP Inbox thru actions

    Hi Friends, I am having a requirement as below: Need to send one mail to SAP Inbox to the BP in one specific partner functions in an activity based upon the below conditions: 1) Status of the transaction is open 2) The Todays' Date -Created date =45

  • Forcing events to happen

    Hello, I created a JFileChooser (meCreateChooser) which shows up when the user clicks on a JMenuItem. The user has to type a name for his file and the program checks to see if that name already exists. If the typed file name already exists, the progr