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

Similar Messages

  • 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);
    }

  • Trying to give the focus at my JComboBox

    Hey does somebody knows what i'm doing wrong,
    i have a JFrame with a JTextField and a JComboBox, i want to give the focus on startup to the JComboBox but the form gives it to my JTextField, here is my code to ask the focus for my JComboBox
    keuzeBox.addComponentListener(new ComponentAdapter(){
    public void ComponentShown(ComponentEvent ex){
    ((JComboBox)ex.getSource()).requestFocusInWindow();
    what am i doing wrong?

    I'm not sure if componentShown is applicable to JFrame class (as a lightweight component). The event seems to not be fired.
    Try this, as you mentioned you are displaying a JFrame, prefer to use a WindowFocusListener.
            frame.addWindowFocusListener(new WindowFocusListener() {
                public void windowGainedFocus(WindowEvent e) {
                    keuzeBox.requestFocus();
                public void windowLostFocus(WindowEvent e) {
            });

  • 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()

  • 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

  • Button.requestFocus(); does not give the focus to a button...

    I have a Frame with a message (textPane) and a button, for accept the message. I want the button to be focused when I open the frame, so this will allow the user to just press one key for accepting the message without use the mouse.
    How can I acomplish this???!!!
    Thanks on advance.

    I still cannot give the focus to the button when I open the JDialog.
    My code is this:
    public class VError extends JDialog
    GridBagLayout gridBagLayout1 = new GridBagLayout();
    JTextPane jTextPane1 = new JTextPane();
    JPanel jPanel1 = new JPanel();
    JButton jButton1 = new JButton();
    GridBagLayout gridBagLayout2 = new GridBagLayout();
    String message;
    ResourceBundle mes = ResourceBundle.getBundle("com.saincotrafico.optimus.client.resources.RError",Locale.getDefault());
    MutableAttributeSet mas;
    public VError(String message)
    this.setSize(250,150);
    this.setResizable(false);
    this.getContentPane().setBackground(UIManager.getColor("Panel.background"));
    this.setTitle(mes.getString("title"));
    this.message = message;
    try
    jbInit();
    catch(Exception e) {
    e.printStackTrace();
    private void jbInit() throws Exception
    mas=new SimpleAttributeSet();
    StyleConstants.setAlignment(mas,StyleConstants.ALIGN_CENTER);
    jTextPane1.setParagraphAttributes(mas,true);
    jTextPane1.setEditable(false);
    jTextPane1.setSelectedTextColor(Color.black);
    jTextPane1.setText("\n"+message);
    jTextPane1.setBackground(this.getBackground());
    jTextPane1.setEditable(false);
    this.getContentPane().setLayout(gridBagLayout1);
    jButton1.setSelected(true);
    jButton1.setText(mes.getString("aceptar"));
    jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(ActionEvent e) {
    jButton1_actionPerformed(e);
    jPanel1.setLayout(gridBagLayout2);
    this.getContentPane().add(jTextPane1, new GridBagConstraints(0, 0, 1, 1, 0.0, 1.0
    ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
    this.getContentPane().add(jPanel1, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0
    ,GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 10, 0), 0, 0));
    jPanel1.add(jButton1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
    ,GridBagConstraints.NORTH, GridBagConstraints.NONE, new Insets(5, 157, 5, 158), 0, 0));
    this.getRootPane().setDefaultButton(jButton1);
    jButton1.requestDefaultFocus();
    jButton1.requestFocus();
    void jButton1_actionPerformed(ActionEvent e)
    this.dispose();
    }

  • 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.

  • 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)
    }

  • 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()

  • 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 can I make firefox give the focus (the cursor) to the address bar when I open a new window - as it does when I open a new tab (to about:blank)?

    When I open a new tab using CTRL-T, the address/awesome bar has the focus.
    When I open a new window using CTRL-N, it does not.
    Can the behaviours be made consistent via setting a configuration option or key?
    Thanks in advance for any help!

    Hello wodow, please check it in [https://support.mozilla.org/en-US/kb/troubleshoot-firefox-issues-using-safe-mode#firefox:linux:fx27 safe mode], do you have the same behaviour ? DO NOT reset, select "Start in Safe Mode".
    thank you

  • 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()";

Maybe you are looking for

  • Kernel panic interrupt many times on my ibook G4

    kernel panic interrupt pull up many times, and the latest log file as follows: Sun Sep 10 11:19:19 2006 panic(cpu 0 caller 0x000A8B98): Uncorrectable machine check: pc = 00000000000ACCA8, msr = 0000000000141030, dsisr = 42000000, dar = 00000000210090

  • Add new administrator to an existing OnPlus site

    Hello, I would like to add myself to an existing OnPlus site (device) as another administrator of the site. My colleague who already has access to the site, did the steps explained in  Adding and Managing Authorized Agents of the Document OnPlus Port

  • How to alter width of navigation region

    Is there a way to alter the width of the Navigation Region Alternative 1? I am using it to display links to other pages, but the descriptions are bumping onto three and four lines, making it cumbersome.

  • ITunes doesn't work (imagine that)

    Ok, I have two G4s with 10.3.9 - except for hard-drive size, pretty much identical. I did a software update on one. I open iTunes. It looks different - this was an older machine so I assumed it was an older version and when it checks it tells me it's

  • ODS Acitvation  Problem..

    Hi Experts: Following is the ABAP dump, while ODS data activation error in BI 7.0...Please help. Runtime Errors         DBIF_RSQL_SQL_ERROR Except.                   CX_SY_OPEN_SQL_DB Date and Time          04.12.2006 22:02:29 Short text      SQL err