0

I want to create a JDialog which has a JTextField, this dialog box should start taking input from user no sooner it starts(i.e WITHOUT user having to click on it). I am calling the code using combination of key tap.So if any other app is running and I would want to pop this dialog box, i would just press the required keys.However currently, I have to click on the dialog box when it pops beacuse the present running app has the focus.Anyhelp..Thanks

  //Creating a panel
    JPanel panel = new JPanel( new SpringLayout() );

    //creating a text field
    final JTextField textfield = new JTextField(65);


    // Modifying text field
    Font font = new Font("SansSerif",Font.LAYOUT_LEFT_TO_RIGHT, 19);
    textfield.setFont(font);
    textfield.setForeground(Color.black);
    textfield.setBackground(new Color(200,200,200,70)); //253,245,230
    //border
    textfield.setBorder(new BevelBorder(BevelBorder.LOWERED));
    textfield.requestFocusInWindow();



    //modifying panel

    label.setLabelFor(textfield);
    panel.add(textfield);
    panel.setBackground(new Color(0,0,0,25)); 
    panel.setPreferredSize(new Dimension(750,70));
    panel.setBorder(new EtchedBorder(EtchedBorder.RAISED));


    //Lay out the panel.
    SpringUtilities.makeCompactGrid(panel,
                                    1 , 2, //rows, cols
                                    10, 10,        //initX, initY
                                    10, 10);       //xPad, yPad


    //Create and set up the Window Frame

    final JDialog frame = new JDialog();
    frame.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);


    //Positioning the dialog at the center of screen
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setLocation(dim.width/4-frame.getSize().width/4, dim.height/3-frame.getSize().height/3);

    //Set up the content pane.

    //adding background


    frame.setLayout(new SpringLayout());
    frame.setUndecorated(true);

    frame.add(panel);       
    frame.setSize(750,71); 
    frame.repaint();



    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            frame.toFront();                 
            frame.setAlwaysOnTop(true);
            frame.repaint();

            frame.setVisible(true);
        }
    });

    frame.addWindowFocusListener( new WindowAdapter() {
        public void windowOpened( WindowEvent e ){
            frame.requestFocus();
        }
    }); 
sjcoding
  • 864
  • 6
  • 6

3 Answers3

3

Almost every line of code is not needed or in the wrong order:

frame.pack();
frame.setSize(750,71); 

Well, make up your mind what are you trying to do? The pack() method is used to have the frame automatically calculate the size based on the components added to the frame. The setSize() would override the results of the pack() method. You should only pack() the frame AFTER adding all the components to the frame.

frame.repaint();

No need for the repaint(), the setVisible(true) method will make sure the frame is painted.

frame.requestFocus();

Not needed, when the frame is made visible focus will automatically go to the first component.

So the basic order of your code should be something like:

JTextField textField = new JTextField(20);

JFrame frame = new JFrame(...);
frame.add(textField, BorderLayout.PAGE_START);
frame.pack();
frame.setVisible(true);

There is no need to request focus on the text field. Focus will automatically be placed on the first component.

You may also want to take a look at the Swing tutorial on How to Make Dialogs. You can use a JOptionPane to prompt a user for text input. The tutorial has working examples.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • actually i am calling this frame using window hook by tapping key combination...if i am using some other app the dialog pop's but doesn't get focus.Thats the issue.! – sjcoding Sep 25 '15 at 20:52
  • @sjcoding: well, shouldn't this key information (and more) be part of the original question? – Hovercraft Full Of Eels Sep 25 '15 at 20:57
  • actually its mine first question..i am sorry about that...however the window hook code is written in C# which calls this Jframe by using the jar file in a process. – sjcoding Sep 25 '15 at 21:03
  • Can you please tell me, How can i start this frame with focus and make all other window app Unfocused. – sjcoding Sep 25 '15 at 21:05
  • 2
    @sjcoding, `which calls this Jframe ` - your code is using a JDialog. Be accurate with your descriptions. We are not mind readers. We can't guess what you mean to say. I've never used a window hook so I don't know how they work. However, maybe you can use the `RequestFocusListener` found in [Dialog Focus](https://tips4java.wordpress.com/2010/03/14/dialog-focus/). If the class doesn't work as is, then try changing the `requestFocusInWindow()` statement in the class to `grabFocus()`. – camickr Sep 25 '15 at 21:32
1
field = new JTextField(40);
f.addWindowListener( new WindowAdapter() {
    public void windowOpened( WindowEvent e ){
        field.requestFocus();
    }
}); 
kiko
  • 72
  • 3
  • 2
    Thanks for contributing It's preferred to add some explanation and not just code to answers. This makes them ore generally useful. – DJClayworth Sep 25 '15 at 20:31
-1

What are you doing here?

There is no textfield show in your code. All you have is a dialog... Do you need help making a textfield? How to add components to JDialog

Then after you make a text field select it by using textField.requestFocusInWindow()

or look at this post to help, Java Textfield focus

Community
  • 1
  • 1
Ya Wang
  • 1,758
  • 1
  • 19
  • 41