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