Can't get reuuestFocus to work


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Can't get reuuestFocus to work

  1. #1
    Join Date
    Jan 2004
    Posts
    1

    Can't get reuuestFocus to work

    I'm new to Java and having trouble getting requestFocus to place the cursor in a JtextField. I've written a little program to play with but can't get it to work. What am I'm missing?

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class testreplyfocus extends JApplet implements ActionListener
    {
    JLabel prompt = new JLabel ("Test Request Focus");
    JButton but = new JButton("Button");
    JTextField text = new JTextField("",10);
    FlowLayout flow = new FlowLayout();


    public void init()
    {
    Container con = getContentPane();
    con.add(prompt);
    con.add(but);
    con.add(text);
    con.setLayout(flow);
    but.addActionListener(this);
    text.requestFocus()
    }
    public void actionPerformed(ActionEvent thisEvent)
    {
    Object source = thisEvent.getSource();
    text.requestFocus();

    if (source == text)
    {

    System.out.println ("Worked");
    }
    }
    }

    Thanks!

  2. #2
    Join Date
    Feb 2004
    Posts
    808
    it does work.. the only thing i did with your source code was put a semicolon on the end of the line with requestFocus().. it was missing, so maybe it didnt compile and you didnt notice...

    here, as proof, try this simple code i knocked up:

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class testreplyfocus extends JApplet implements ActionListener
    {
      int whoGets=0;
    
      JLabel prompt = new JLabel ("Test Request Focus");
      JButton but = new JButton("Button");
      JTextField text[] = new JTextField[5];
      FlowLayout flow = new FlowLayout();
    
    
      public void init()
      {
        Container con = getContentPane();
        con.add(prompt);
        con.add(but);
    
        for(int i=0; i<text.length; i++){
          text[i]=new JTextField("",10);
          con.add(text[i]);
        }
        con.setLayout(flow);
        but.addActionListener(this);
        text[0].requestFocus();
      }
      public void actionPerformed(ActionEvent thisEvent)
      {
        text[whoGets].requestFocus();
        but.setText("whos got="+whoGets);
        whoGets = (whoGets+1)%text.length;
    
      }
    }
    it simply makes an array of textfields, and every time you click on the button it knocks a counter up by 1 (modded by array.length to always keep it in the bounds of the array) then gets that text field to request the focus.. repeatedly hammer the button and watch the cursor track along each box
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center