Setting focus to textfield when program is first launched

I'm getting really mad. Everything is working fine. But, one little thing and its is suppose to be really simple. I must be some kind of an idiot if I can't see what the problem is for this. Anyway, I'm trying to set the cursor/focus on my text field when the program is launch. But, it seems that everyting that I try appears not to be working. I now have some junk code in my program. But, I was hoping that my attempts were getting me close to what I want. Below is my code. I'm hoping someone can tell what I'm doing wrong.
import java.io.*;
import java.awt.*;
import java.awt.event.*;
class ChatClient extends Frame
    private TextField tf1 = new TextField("", 50);
    private TextArea ta;
    private Button sendButton;
    private Button quitButton;
    private MenuItem exitMT, aboutMT;
    private Choice choice;
    private String user;
    public ChatClient()
      super("ChatClient");
     addWindowListener(new WindowAdapter()
      {public void windowClosing(WindowEvent e) {System.exit(0); }});
      setTitle("Chat Room");
      Panel p = new Panel(new BorderLayout());
      Panel p1 = new Panel(new BorderLayout());
      Panel p2 = new Panel(new GridLayout(3,1));
      MenuBar mb = new MenuBar ();
      setMenuBar (mb);
      Menu fileMenu = new Menu ("File");
      mb.add(fileMenu);
      Menu helpMenu = new Menu ("Help");
      mb.add(helpMenu);
      fileMenu.add (exitMT= new MenuItem ("Quit"));
     helpMenu.add (aboutMT = new MenuItem ("About"));
      exitMT.addActionListener(new ExitCommand());
      aboutMT.addActionListener(new AboutCommand());
  //creating main panel
     add(p, BorderLayout.NORTH);
      ta = new TextArea(10, 50);
      add(ta, BorderLayout.CENTER);
      ta.setSize(500,300);
      ta.setEditable(false);
      ta.transferFocus();
    //adding buttons  and choice box to the right side of the window
      sendButton = new Button("Send");
      Panel p3 = new Panel();
      sendButton.setSize(15,15);
      sendButton.setBackground(Color.green);
      p3.add(sendButton);
      p2.add(p3);
     sendButton.addActionListener(new CopyCommand());
     quitButton = new Button("Quit");
      Panel p4 = new Panel();
     quitButton.setSize(15,15);
     quitButton.setBackground(Color.red);
     p4.add(quitButton);
     p2.add(p4);
     quitButton.addActionListener(new ExitCommand());
     Panel p5 = new Panel();
     choice = new Choice();
     choice.addItem("John");
     choice.addItem("Jena");
     choice.addItem("Colleen");
     choice.addItem("Steve");
     user=choice.getItem(0);
     p5.add(choice);
    choice.addItemListener(new CopyCommand());
    p2.add(p5);
    add(p2, BorderLayout.EAST);
   //adding the textfield to the south end of the window
      p1.add(tf1);
      tf1.isDisplayable();
      tf1.setFocusable(true);
      tf1.setVisible(true);
      tf1.requestFocus();
      add(p1, BorderLayout.SOUTH);
      tf1.addActionListener(new CopyCommand());
//handling ActionEvent from buttons and menu items
  class ExitCommand implements ActionListener
    public void actionPerformed (ActionEvent e)
           tf1.setText("");
           ta.setText("");
          choice.select(0);
          System.exit(0);
  class AboutCommand implements ActionListener
     public void actionPerformed (ActionEvent e)
           System.out.print("About menu was pressed!");
  class CopyCommand implements ActionListener, ItemListener
    public void itemStateChanged(ItemEvent e)
     {  if (e.getSource() instanceof Choice)
               user = (String)e.getItem();
    public void actionPerformed (ActionEvent e)
            ta.append(user + ": " + tf1.getText() + "\n");
            tf1.setText("");
            tf1.requestFocus();
     public void keyPressed (KeyEvent ke)
            //figure out if the enter key was pressed
             if (ke.getKeyCode() == KeyEvent.VK_ENTER)
     ta.append(user + ": " + tf1.getText() + "\n");
          tf1.setText("");
            tf1.requestFocus();
     public void keyTyped (KeyEvent ke) { }
     public void keyReleased (KeyEvent ke) { }
   public static void main(String[] args)
   {  Frame f = new ChatClient();
      f.setSize(600, 400);
      f.show();
}

Bingo!
Hi , I am trying to make a chat application too I dont know its exactly like yours or not......I need to make it for the chat support of a website (like in some sites you have online chat with chat center guy's). For that reason I had to make it an applet , although i want to use frame but i do then how i will embed it in the web page? I encountered with the same problem you are facing but didnt solved it yet cause i have bigger problems than that. tell me you have solution.
Plz visit the following threads:
http://forum.java.sun.com/thread.jspa?threadID=611865
and
http://forum.java.sun.com/thread.jspa?threadID=607639
Thankyou
Regards
Manu

Similar Messages

  • How can I set focus on TextField?

    Hi,
    I can't find a method or solution to set focus on TextField,I'd like to set the cursor into a special TextField when user enter my form.how can i implement this functionality?
    Any suggestion?
    Hanlin Li.
    Edited by: noob on Apr 13, 2012 4:28 PM

    textField.requestFocus();

  • Set focus on TextField

    Hi guys,
    I want to focus the textfield with a cursor on it without
    clicking on the same and without using Ifocusmanager as I have
    taken the textField.
    I have tried
    stage.focus but it doesn't seem to work the way I want.
    please help me..

    It works.. But without clicking stage won't it happen?
    I have one more query related to it..
    I have n number of tfs on the stage and when I enter into the
    the first tf and if it reaches the maxchar no. it
    shifts its focus to the next tf. Now If I want to reenter the
    text into these tfs from the start it won't replace the text
    (for this i wrote tf .text="", cause of this it takes only
    single char irrespective of the maxchar limit! ) So is there any
    method to do it? Cause in focusmanager class it happens..and
    I want to make it work without one..

  • Mail accounts offline when Mail is first launched

    After upgrading to 10.6.2 (both computers), when I launch Mail, all of my accounts are offline with the little triangle next to them. If I click on "Get Mail" I can connect and download my mail. Quit and re-launch, and I'm back offline. Both of my Mac's are connect wirelessly, if that matters. This is getting old.....

    Here's what i did, I had the same problem. Go to the finder/go/computer/yourname/library/preferences. move the file com.apple.mail.plist to the trash bin. But before you do this copy all the info on your account for your isp. Such as user name,password , incomming server, and outgoing server info. This worked for me. Good luck
    John

  • Set Focus target when Add New Instance

    [LC Designer ES2 9.0] Using Action Builder to add a new instance of subform SITE, where first field is called SiteName.
    If I set action to Set Focus to SiteName, the focus goes to the *original* SiteName field instead of to the new instance.
    Users will typically add a number of additional sites and, each time, I want the focus to be set on the first field of the newest instance, not the original instance. How do I accomplish that? Thanks!           ~Carol

    I understand the concept (I think...), but am not clear on where to place the code.
    I used Action Builder to create an AddSite action: when check box chkAddSite is checked | Add a new instance of subformSiteInfo | and set visibility of subformSiteInfo to visible. Does your code go in that same action sequence? If so, what is the next Action Builder result selection? If not, where do I insert it?
    I never know whether the new instance of the subform will begin on the following page, so the user might keep clicking the chkAddSite box, thinking it didn't work. Although, thinking this through, maybe I could force the new subform instance to always appear at the top of the following page, and somehow direct the focus there, rather than to the specific first field???
    Thanks!         ~Carol

  • Cannot set focus when reset data using datasets

    Hi All,
    When I click reset form button. I would like to reset data then focus to the first field. I can clear information I provided but setFocus to the first name field is not working. Please help.
    I use datasets to reset data. 
    Here is my coding:
    event: click
    xfa.datasets.data.loadXML("<form1/>", true, true);
      xfa.host.setFocus("xfa.form1.contactInformation.firstname")
    Thanks,
    Cindy

    I tested the clear button. But it is not set Focus to the first field. It is not successful.

  • How to set focus on a custom PO Item screen field when in error?

    Hi All,
    I have an interesting situation that i'm wondering if others have solved.  We have extended the PO item table (EKPO) by adding two new fields.  We then have implemented two BAdI's:  ME_GUI_PO_CUST and ME_PROCESS_PO_CUST to add them to the ME21N/ME22N/ME23N screens and logic to do some validation, via these respective BAdI's mentioned.  Everything works perfectly - with one small issue.  When we are doing some validation via a method on the ME_PROCESS_PO_CUST - and "invalidate" the field (and throw an error) when it is in error - I also want to be able to "set focus" on the field in question (basically: go to the particular tab on the ME* screen and highlight the field).  I have tried using SET CURSOR FIELD *****  within this BAdI (ME_PROCESS_PO_CUST) - but doesn't seem to work.  Has anyone tried to do this and have come up with a solution?  Would be much appreciative if you shared it!!!  Thanks much.
    Cheers,
    Matt
    ERP version that we have is:  ECC 6.0

    Just have a look at oss note 310154 - ME21N/ME51N: Customer-specific check, generating error log.
    In short:
    Add your error messages in EXIT_SAPMM06E_012 (using specific macros).
    Sample code (provided in Oss note) :
      loop at tekpo where knttp eq 'X'.
        loop at tekkn where ebeln eq tekpo-ebeln and
                            ebelp eq tekpo-ebelp and
                            kostl eq space.
          if not tekkn-id is initial.
            mmpur_business_obj_id tekkn-id.
            mmpur_metafield MMMFD_ACCOUNTINGS.
          endif.
          mmpur_message_forced 'E' 'ZE' '777' '' '' '' ''.
        endloop.
      endloop.

  • I get "application not found" when clicking on a http link in a mail, even after having set firefox as the default program for http links.

    After installing firefox 15.0, I can not open links form email or other office 2010 applications. I get "application not found" error. I tried setting firefox as the default program both manually (in the Windows default program panel) and automatically (having Firefox to do it automatically on startup (tools->options->advanced->General). I still have the error.

    That sounds odd. Is this error coming from a particular e-mail program you're using? It could be that that program has its own settings for which web browser to launch, ignoring the "default browser" setting in Windows.
    Does the same thing happen if you click on a web link from any other external (non-browser) application on your computer? If it happens with other apps too, I would suggest to re-download and install Firefox (www.mozilla.org) and see if that takes care of it.

  • Hi, i am looking to set cells so that when one number decreases the other increases. The catch is that when the first number goes up the other stays the same. And when it starts decreasing again the other builds up. so forth

    Hi, i am looking to set cells so that when one number decreases the other increases. So say cell A is at 5, and cell B is at 0. When Cell A goes to 4, Cell B goe to 1. The catch is that when Cell A gets down to 1, and i change it to 5 again, Cell B will be on 4 and start incresing from that point on when cell A decreses. This is to show an amount of sales within a company when stock is sold.
    Example:
    A = 3
    B = 0
    A = 2
    B = 1
    A = 1
    B = 2
    A = 3
    B = 2
    A = 2
    B = 3
    A = 1
    B = 4
    A = 3
    B = 4
    A = 2
    B = 5
    Sorry if this sounds confusing.
    Many Thanks,
    Oliver

    Can't be done, in a single cell, for the reasons given by Jerry.
    But not difficult in a column:
    I've moved the columns one to the right to put them in the body cells of the default Numbers table.
    Current stock is listed in column B, with results of increases and decreases entered manually. Any increase  is assumed to be a restocking, and does not affect the sales to date figure in column C. Any decrease in stock is regarded as a sale and increases the current total in column C by the amount of the decrease.
    Until an entry is made in B, the corresponding cell in B will show a hyphen. This can be changed by changing the text between the quotation marks in the formula.
    Enter the formula into C3 and Fill it down the rest of column C.
    Regards,
    Barry

  • Setting focus to new instance of a field...need help.

    Hi there,
    I have a flowed form which consists of multiple subforms. I have one subform which is basically one textfield that is set to a min of 1 and a max of 5.
    These are to correspond to fields in our system of record that have 5 fields for company long name with a 36 character field limit.
    I've got the limits set up just fine and I have added simple script to add a new instance when the user exits the field.
    The focus goes to the next field set in the tab order but what I want is the focus set on the new instance of field created but I have not been able to find an example of how to do that.
    Any suggestions would be greatly appreciated. Thanks!

    Well,
    I've been reading and trying various things but cant get anything to work like I want it to.
    The sample provided works for 2 iterations but mine has to do this for up to 5 instances. I have not been able to figure out how to get this to work.
    Anyone have any additional suggestions?
    btw...here is my modified version. ES2 is whay I'm using.
    Long_Title.occur.max = "5";
    var oSubform = xfa.resolveNode("Long_Title");
    var oNewInstance = oSubform.instanceManager.addInstance(1); // I think you probably managed this first part better than here
    xfa.form.recalculate(1); // I THINK THIS IS IMPORTANT
    var count = (this.Long_Title.nodes.length)
    var testIndex = oNewInstance.Account_Long_Title.index;
    xfa.host.messageBox("Text Field Index in new Subform: " + count); // TextField1 will conserve index 0 because it is the only textfield in the new Sub1
    var NEW_TEXTFIELD = xfa.resolveNode("Long_Title[1].Account_Long_Title[0]"); // However, Sub1 gets index 1 because it is not alone any more
    xfa.host.setFocus(NEW_TEXTFIELD); // This actually sets focus on the newly created instance of TextField1 (actually Sub1 instance)

  • Setting Focus In Textarea

    Ok,
    I have a java program I've bundled into a jar and then packaged into a Win32 executable. I have windows associate a particular filetype (.wri) to my program.
    When I click a .wri file and open it, it loads data to a textarea. Now when I scroll down the textarea and finally click in the textarea, it resets me to the top of the textarea. Now, if I open the executable first, then go through the program to open a .wri file, I can scroll down to the bottom of the textarea, click in the textarea and I do not get reset to the beginning. I need a way to prevent the resetting of the textarea. I've tried using some of the Window methods for setting Focus, but none seem to work. Any ideas why this is happening via the one way and not the other. I'm open to any ideas. This known bug frustrates many because the .wri files being opened have over 1000 lines of text. Thanks in advance!

    i think yu can use setFocus by using lostFocus event.
    use Focuslost()

  • Problem using Setting Focus

    Hello there, can you help me on how to use the set Focus.
    For instance I have a 3 JTextField, when I run it, the focus is on the first textField, what I want is that, when I pressed the enter key on the keyBoard, the focus will be on the 2nd textField, and when I pressed again the enter key, the focus will be on the 3rd textField and so on.
    can you give me a sample code on this one, thanks.

    the last post in this thread might give you some ideas
    http://forum.java.sun.com/thread.jspa?threadID=713697&tstart=0
    note: will only work if the textfields follow each other in the focus traversal policy

  • How to retain focus in Textfield

    I have one panel which contains three textfiled and one ToolBar. on clicking tool bar button certain dialog get opened.
    intially foucus remained on first textfield but when i do open the dialog with tool bar button. focus doesn't get return to first textfield even focus doesn't get back to screen.
    And my requirement is that to get focus on first textfield after closing the dialog.
    Please let me know if you have any suggestions.
    Thanks,

    yeah I need to set focus again on textfield and that is my question.you can do that using something like :jTextField.requestFocus(); I am seeking the event on which i can set the focus again please suggest.If you are using JOptionPane just add the above statement after showing dialog like JOptionPane.showInputDialog("Hello");
    jTextField.requestFocus();If you are using your own dialog, have a refrence to the textfield in the dialog and at 'windowClosing' Event use the mentioned statement.
    I think that should work....
    Thanks!

  • How to set focus on a input field in a selected row of a table?

    In a previous discussion (http://scn.sap.com/thread/3564789) I asked how to access an input (sap.m.Input) field of a selected row in a table. In the answer that was supplied I was shown how to get the items of the table. Then using the selected index to get the selected item get the cells. Then I could set editable on the proper cell(s). This worked fine.
    Now I need to set the focus on one of the fields. I tried something like this:
                var oNewLink = table.getSelectedItem();
                var oNewLinkName = oNewLink.getCells()[1];
                oNewLinkName.focus();
    But this doesn't seem to work.
    I have searched through other discussions and have seen this technique for putting focus on a field if you have its ID:
    sap.ui.getCore().byId(id_of_the_input_field).$().focus();
    In my case though I do not have an ID since the row and its cells are generated. How can I set focus on the cell of a certain row in a table?

    Hello Venkatesh. Yes that code does work. First I tried it on a table cell that was already rendered and it did work. The next time I tried it on a table row that was being added and it did not work there. So I added an on after rendering function for the table and added that code there. That did not work until I added a delay (timeout) to do a context switch before calling the focus and that worked.
    Once last thing though sometimes when I call focus on an input field (actually in a table row cell) if the field has text in it already the flashing cursor is at the beginning of the text and other times it is at the end of the text (which is the desired way). It depends on where I click in the row. Is there anyway to make sure the flashing cursor is at the end of the text when the focus is applied to a field that contains text?

  • How to set focus on the title of JTabbedPane

    I have created a JTabbedPane and added three JPanels to it. They are titled, say "Panel 1", "Panel 2" and "Panel 3". And each of them contains buttons and text areas. Since this app is for physically disabled users, it must provide navigation through these three tabs with keyboard operations only (i.e. without mouse clicks).
    When the title "Panel 1" gets focused, users can go to "Panel 2" by the right arrow key. When Panel 2 is brought up, however, the title "Panel 2" does not get focused. Instead the first button inside panel 2 is focused. In order for users to navigate to 'Panel 3" by the arrow key, the title "Panel 2" has to be focused. How do I set focus on the tab title?
    I have tried 'requestFocus()' on Panel 2, but it does not work. Please help me with this issue. Thanks in advance.

    I'd be quite interested to know if this can be doen as well so I thought I'd post this message to move it to the top of the board.
    Thanks.

Maybe you are looking for

  • Problem on CachedRowset with RMI

    Hello, I wanted to transfer some database tables to another machine, thus I used RMI. I found there was a class called CachedRowset which could be serialized. So, I decided to use this. However, when I wrote the following code in an rmi method and ra

  • Server does not support setting more than 5 shared access policy identifiers on a single container

    Hi, I upload a video file to a new Asset. I then attempt to create a streaming URL by creating an Access Policy and then a Locator, which I use to generate the URL used for streaming.This works great. Until the 6th time you execute that code against

  • 'Audiobooks' menu on ipod displays chapters (allmixed up) and not Titles

    I have imported many Audiobooks, from CD's I own, to iTunes. I have transferred them from the 'Music' Library to the 'Audiobooks' Library by clicking 'get info', then 'options' and then setting 'Media Kind' to 'Audiobook' They are of course stored li

  • ACS external database question

    Hi, our customer need a security solution that enable the users to login to the wireless network with their usernames & the passwords which stored in the active directory not using the preshared key of the wireless. if we are using ACS server and int

  • Trivia

    Explain: class test      public static void main(String args[])           String file = "-134343";           System.out.println(file.substring(0,2));           if(file.substring(0,2) == "-1")           System.out.println("yes");           String file