How to add a KeyListener to a JInternalFrame

1). I have Main Window which is an Application window and has a menu. When I click on a menu option,
a user interface which is a JInternalFrame type, with JTextFields in it, is loaded.
2). In the first JTextField(e.g. Employee Number), I have a KeyListener added to the JInternalFrame.
3). When I enter an Employee number in the JTextField and press the Key F10, I have to fetch the details about the Employee
(his first name, middle name, last name etc.) from the database and display these details in the corresponding
JTextFields in the user interface.
PROBLEM:
I am able to add a KeyListener to the JInternalFrame but it does not work the way I need it to.
The moment F10 key is pressed, the JInternalFrame looses focus, and the focus goes to the Main Window and does not execute
the code for the keyPressed event that is there in the listener.
How do I overcome this issue??
The code that I am using is below
public class TD {
public void invoke(){
cFrame = new JInternalFrame("TD Form",
false, //resizable
true, //closable
true, //maximizable
true);//iconifiable
// More code here..................
//Destroy the TD Window.
cFrame.addInternalFrameListener(new InternalFrameAdapter() {
     public void internalFrameClosing(InternalFrameEvent evt){
               cancelSteps();
}); // end of InternalFrameListener.
cFrame.addKeyListener(new KeyTD(this)); // Adding the KeyListener here to the JInternalFrame
cFrame.setVisible(true);
cFrame.setSize(800,450);
cFrame.setLocation(10,10);
} // end of the constructor.
// Inner class that implements the KeyListener
class KeyTD implements KeyListener {
private int msg =JOptionPane.INFORMATION_MESSAGE;
private String title = "KeyPress Event Message";
Container cornbut;
TD objTD;
private int UTNum;
public KeyTD(TD objTD){
          this.objTD = objTD;
public void keyPressed(KeyEvent ke){
int key1 = ke.getKeyCode();
switch(key1){
case KeyEvent.VK_F9:
tD_F9();
break;
case KeyEvent.VK_F10:
UTNum = getNum(); // Reads the content of the JTextField and gets the Integer value
if (UTNum > 0){
                    tD_F10();
}else{
JOptionPane.showMessageDialog(cornbut," Please enter the Number. ",title,msg);
break;
}//end of keyPressed method
public void keyReleased(KeyEvent ke){
public void keyTyped(KeyEvent ke){
public void tD_F9(){
//sets focus to the Text Field where the key listener has been added
}//end of tD_F9 method
public void tD_F10(){
String str = String.valueOf(UTNum);
Objdbdef.fetchTaskDefiner(str,"", "","" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" );
if(//there are records in the database then){                       
// If there are records matching the number from the JTextField the display statements are here.........
}else{
JOptionPane.showMessageDialog(cornbut,"There are no records found for the specified criteria.",title,msg);
}//end of td_F10 method
}// end of KeyTD class

As a rule of thumb, I never use KeyListeners, unless you want to listen to many different key strokes.
Instead, use ActionMap/InputMap. You can attach an action to, say your desktop, that
is activated when F1 is pressed WHEN_ANCESTOR_OF_FOCUSED_COMPONENT or
WHEN_IN_FOCUSED_WINDOW

Similar Messages

  • How to add a KeyListener for the JFrame (when I'm typing in a JTextField)?

    I have some problem with KeyListener..
    I add a KeyListener (I named it "listener") for my JFrame and it works fine. Then I add JTextField to the JFrame. When I'm typing some text in the JTextField - my "listener" does not work. (cause my JTextField doesn't have a KeyListener).
    I just want to make an ability to process hot keys which user presses in my java program..
    Does anyone know how to do it?

    In future, please ask Swing questions in the [Swing Forum.|http://forums.sun.com/forum.jspa?forumID=57]
    Don't use KeyListener. In fact KeyListener is seldom useful. Use key binding: [http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html]
    Or you can have menu items with accelerators (hot keys).

  • How to add background image for a JInternalFrame Container?

    JInternalFrame fr=new JInternalFrame();
    //we added to desktop,
    Container c=fr.getContentPane();
    //how can we set background image for a internal frame??
    thanks,
    waiting,

    You can let the container's border draw the background image: caution: some will find this image disturbing.
    One nice thing about this solution is that you don't have to subclass any components.

  • How to add multiple images in jinternalframe

    Hi all,
    how to add multiple images to the jinternalframe, at specified location of the pane and resizing the images with specified height and width.
    code examples are highly appreciated.
    Thanks & Regards,
    Abel

    Thanks, it works perfectly. It's a really smart way of fixing the problem too :)
    I also found your toggle button icon classes which is something I've also had a problem with.
    Thanks.

  • Add a KeyListener to JTextField

    Hello:
    I got some questions to ask you.
    1) How to add a KeyListener to a JTextField, and I'd like to known how
    to use System.out.println to print out the values which are from keyBoard?
    2) How to put the JTextField in the middle of the JFrame?
    import javax.swing.*;
    import java.awt.*;
    public class TestKeyListener extends JFrame{
         private static TestKeyListener tKL;
         private static JTextField jTF;
         private static Container c;
         private static GridLayout gL;
         public static void main(String [] args){
              tKL=new TestKeyListener();
              tKL.setVisible(true);
         public TestKeyListener(){
              setSize(400,400);
              setTitle("TestKeyListener");
              jTF=new JTextField();
              gL=new GridLayout(10,1);
              c=getContentPane();
              c.setLayout(gL);
              setUp();
         private void setUp(){
         c.add(jTF);

    hello,
    try this
    1) jTF.addKeyListener (new KeyListener ()
    public void keyPressed (KeyEvent keye)
    // your other codes goes here
    // to print out the text in the textfield see code below
    String printout = jTF.getText();
    System.out.println (printout);
    Note: there are more methods of the KeyListener such as keyReleased e.t.c pls bwrosed through the documentation for the jdk version u are using to get more of those methods.
    2) u can simply use borderlayout to add the textfield to the center of the frame
    frame.getContentPane().add (jTF, BorderLayout.CENTER);
    of if it is an awt frame
    frame.add(jTF, BorderLayout.CENTER);
    Note that ActionListener works for JTextFields as well by pressing the enter key on your keyboard.
    I hope this helps
    Cheers

  • How to add an icon in the title bar,next to the maximize and minimize icons

    I need to add an icon ( help icon; ?) in the JInternalFrame� title bar. Anyone could help me????
    Thanks

    have you get the answer ?? on how you add an icon to the title bar?? ...
    if you do please inform me..
    thanks

  • How to add fields to already loaded cube or dso and how to fill records in

    how to add fields to already loaded cube or dso and how to fill  it.can any one tell me the critical issues in data loading process..?

    This is sensitive task with regards to large volumes of data in infoproviders.
    The issue is to reload of data in case of adjusted structures of infoproviders.
    Indeed there are some tricks. See following:
    http://weblogs.sdn.sap.com/cs/blank/view/wlg/19300
    https://service.sap.com/sap/support/notes/1287382

  • How to add a library (libSomeRandomlib.so) to a project....??

    Ok, so I've been trying to get CERN's ROOT program/libraries to work in an xcode project for a while, but I'm running into one really weird error, and I think I may just not know how to add a library to a project correctly.
    Suppose I create a new Xcode C++ command line tool project. I hit Build and go, and it gives me a nice little Hello World! in the console, just as it should. If I then click on the project in Groups & Files, go to Project>Add to Project..., and choose, say, libCint.so, from my root/lib directory, and click ok, then click Build & Go, I get an error in the console:
    dyld: Library not loaded: @rpath/libCint.so
    Referenced from: /Users/paulthompson/Documents/Programming/Build Products/Debug/LibraryIncludeTest
    Reason: image not found
    sharedlibrary apply-load-rules all
    Data Formatters temporarily unavailable, will re-try after a 'continue'. (Cannot call into the loader at present, it is locked.)
    My assumption is that either I'm missing a step in the process of adding a library, or else there is something jacked up with the libraries themselves. Anyone have any advice?
    Thanks,
    Paul
    EDIT* Oh, I should mention that the actuall binary that gets built after adding the library WILL execute properly in the Finder, or from terminal, just not with the Build & Go, or Go commands from within xcode.
    Message was edited by: TraxusIV

    TraxusIV wrote:
    Results of otool:
    LibraryIncludeTest:
    @rpath/libCint.so (compatibility version 0.0.0, current version 0.0.0)
    /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.9.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 123.0.0)
    Macintosh-9:Debug paulthompson$ otool -L /Applications/CERNRoot/root/lib/libCint.so
    /Applications/CERNRoot/root/lib/libCint.so:
    @rpath/libCint.so (compatibility version 0.0.0, current version 0.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.0)
    /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.9.0)
    I honestly don't know much about @rpath. I found this page that explains it a bit more: http://blogs.sun.com/dipol/entry/dynamiclibraries_rpath_andmac
    So, since I don't want to deploy with the environment variable set, I should ask, what IS the correct, MacOS X way to do it?
    The best way to find that is to look at how Apple does it. They put frameworks in /System/Library/Frameworks and regular shared libraries in the standard places. You can do the same. Put your frameworks into /Library/Frameworks and your shared libraries into /usr/local-based paths.
    If you want to create stand-alone executables that can be installed via drag-n-drop, you can put shared libraries inside the application bundle. If your shared libraries/frameworks are elsewhere, you will need some sort of installer to get them installed.
    I've been sifting through Apple's developer documentation but I have yet to find a concise, straightforward explanation of how to add shared libraries. I'm horribly confused at this point. Is there a good tutorial available anywhere for both the general unix case of linking to dylibs and .so's, and the Mac specific case?
    Hopefully someone else knows an answer for this one.
    *EDIT* I do already have the variable defined both in ~/.MacOS/environment.plist and also in Xcode as an additional user variable.
    I'm not sure about the whole RPATH thing. Setting environment.plist will define environment variables for use in the Finder, but that is really a hack. There are better ways to do it, but if you are porting some open-source programs, that may be the only way. You should be able to define those variables in Xcode. You have to selected the debug executable under "Exectuables"

  • How to add A single row at the middle of the table in a Webi report

    Hi,
         I created a Webi report using Universe(Created universe using bex query).Now i have a requirement to display a row at the middle of a report. Can you please tell me ,how to add a sigle row at the middle of a Webi report.
                                                    Thanks in advance
    Regards
    Monika

    Hi Monika,
    It is not really possible to add a row (I assume you mean of unrelated data) to the middle of a table in a report. You can add a new table with a single row between two tables. For instance you could add a new one row table, or even single cells which are positioned relatively between two tables. Possibly a block on top of another. But this gets tricky.
    Can you explain in more detail what you are trying to do?
    Thanks

  • How to create a job card and how to add waranty card in sales order

    I have one scenario for CS.the scenario is realted to automotive industry. Basically its a trading industry of HCV,MCV,LCV apart from that they will do servicing also. First the customer comes for a service.he is having free services. he will have waranty for spare parts of the vehicle. once he comes for servicing first the executive will take complains from the customer after that a Job card will be issued to the customer. there his chasis no ,engine no and registration no will be there. once enter the chasis no entire customer details has to come. how many free services he is having for how many kilo meters.then job card will go to the spare parts dept.that dept will issue the spare parts.then they will invoice the customer. he will pay the payment.and finally the gate pass will be given to the customer to deliver the vehicle.
    painful area is how to create a job card and how to add waranty in sales order.
    Regards,
    Venkat

    Hi,
    Have u resolved it then Please let me know !!! It is a very interesting problem and owuld like to know the solution...
    Regards
    Krishna

  • How to add apache to an exisitng BOXI31 presently running on IIS

    Hi Tim,
    I know its rude that I directly mailed you but i am not able to post on the SAP forum due to some reasons I don't know. So please if you can help me I would be highly indebted.
    I have a multiple server installation.
    Number of Servers : 4
    Number of CMS : 2
    Application tier on 2 servers.
    Web tier on 2 servers.
    The problem is when i login my .NET infoview works fine but my Java infoview doesn't.
    I found the error that the CCM doesn't have Tomcat Apache Server.
    So my guess is thats the reason why the java infoview is not working.
    Please help how to add the Apache to the CCM.
    Regards
    Sid

    Hi,
    You need to run custom installation to install Java Web components on your server box and choose to install
    tomcat app server
    - Subhodeep

  • How to add description of a column of a table in SQL Azure

    Hi
    I have some tables in my application database where there are descriptions added against certain columns. Needless to say they were done by using sp_addextendedproperty.
    Now I am trying to migrate the Database to SQL Azure. SQL Azure does not support sp_addextendedproperty.
    Hence I am not able to figure out how to add descriptions to those columns.
    Any help would be much appreciated.
    Thanks
    Soumyadeb

    Hello,
    Just as Latheesh post above, Windows Azure SQL database are not support extended stored procedures. That’s one of the limitations on SQL database, and I don’t know there is another way to achieve the same on Azure.
    Regards,
    Fanny Liu
    Fanny Liu
    TechNet Community Support

  • How to add more fields at level task for importing to MsProject

    Hi everyone,
    We would like to have the same number of fields either for importing as exporting; or at least, the fields that we need.
    The problem is when we are configurating the fields for exporting from cProjects to MsProject, we found 81 available fields and then we don't find the same amount for importing (34). So, in some cases we don't find fields which were configurated for exporting and it is no posible to upload the information back because the field is no available for importing.
    Does anyone knows how to add more fields for importing at the cProjects side?
    Thanks a lot,
    CAMILO URIBE

    Some fields can not be migrated because of MsProject designe. The above is the reason why cProjects' customizing does not offer the
    mapping of the same nuember of fields.

  • How to ADD field in iview ?

    hi,
    in portal-iview i want to add a field.
    when i go to 'portal-content -> open the iview with right click -> preview' and then
    do ctrl-alt-right-mouse click i got to the customizing of the fields.
    i know how to HIDE fields there, but i don't know how to add fields ? in the list
    of the fields the necessary fields are available, BUT i only have to options:
    hide -> YES or NOT PERSONALIZED
    any ideas ?
    reg, Martin

    Gopal,
    the root element of this fields does NOT have the option 'decorate'.
    it is the quoata overview of ESS : sap.com/ess~quotas
    the top of the tree has option 'decorate', but this is above the necessary table where
    i select the data.
    my adrl is 
    martin.svik(at)denzel.at
    maybe we can 'change' some screenshots where you can send me yours and i can
    send you mine to explain it in a better way
    reg, Martin

  • How to add more drop zones or movies in an idvd menu

    I was building a dvd in idvd and ran out of drop zones to fill but I couldn't figure out how to add more drop zones.
    I tried different templates but I could find none that had more than 12 drop zones.
    Then I was adding movies to the main menu and idvd refused to allow more movies added when I reached 10 movies. It said that the template did not accept more movies.
    I tried a few other templates and could not get it to accept more.
    I don't want to add submenus because the movies being used do not lend themselves to being put into different submenus.
    Is there some way to add more than the default drop zones in an idvd template and to add more movies in a menu without having to use submenus?
    Thanks for any advice.

    I couldn't figure out how to add more drop zones
    Sorry, the templates 'are what they are'.
    I don't want to add submenus because the movies being used do not lend themselves to being put into different submenus.
    You need to figure out how to work with what is available.
    If you want more freedom, buy Apple's Final Cut Studio and learn to use DVD Studio Pro.

Maybe you are looking for