Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2006

Tip: Looking for answers? Try searching our database.

request focus not focusing

Thread view: 
Justin - 22 Dec 2006 02:58 GMT
I have a JApplet class and an extended JPanel.  I add the JPanel to the
JApplet when the JApplet gets to its constructor.  It is inside this
method, after I add the component, that I call
JPanel.JTextField.requestFocus().  And it does nothing.  I put a
system.out statement directly after this line printing out
JTextField.isFocusOwner() and it prints false.  Any ideas why this
occurs?

below is the code for my two classes:

package consultrequest;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

public class Main extends javax.swing.JApplet {
   JPanel jpan;

   int width = 604;

   public void init(){
    setSize(width, 400);
   }

   public Main(){
    Container c = getContentPane();
       c.setLayout( new BorderLayout() );

       jpan = new JPanel();
       jpan.setLayout(new FlowLayout());

       jpan.setBorder(new EtchedBorder());
       c.add(jpan,BorderLayout.NORTH);

    Panel panel = new Panel();
    jpan.add(panel);
    panel.lastName.requestFocus();
    System.out.println(panel.lastName.isFocusOwner());
   }
}

//**********************************************************************
//**********************************************************************

package consultrequest;

import java.util.Calendar;
import javax.swing.*;
import java.awt.*;
import java.util.Date;
import java.text.DateFormat;

public class Panel extends JPanel{

   JTextField date, time, firstName, middleInitial, lastName;

  int width = 600;

   public Panel() {
    setLayout(null);
    setPreferredSize(new Dimension(600, 400));

       Font smallFont = new Font("Serif", Font.BOLD, 10);

       JLabel textDate, textTime, textName, textLast, textComma,
textFirst, textMI;

       textDate = new JLabel("Date:");
       textTime = new JLabel("Time:");
       textName = new JLabel("Patient's Name:");
       textLast = new JLabel("Last");
           textLast.setFont(smallFont);
    textComma = new JLabel(",");
       textFirst = new JLabel("First");
       textFirst.setFont(smallFont);
       textMI = new JLabel("MI");
       textMI.setFont(smallFont);

       date = new JTextField();
       time = new JTextField();
       firstName = new JTextField();
       lastName = new JTextField();
    mI = new JTextField();

    add(textDate);
    textDate.setBounds(10, 50, textDate.getPreferredSize().width,
textDate.getPreferredSize().height);
    add(date);
    date.setBounds(20 + textDate.getPreferredSize().width, 49, 70,
date.getPreferredSize().height);

    add(textTime);
    textTime.setBounds(210, 50, textTime.getPreferredSize().width,
textTime.getPreferredSize().height);
    add(time);
    time.setBounds(220 + textTime.getPreferredSize().width, 49, 70,
time.getPreferredSize().height);

    add(textName);
    textName.setBounds(10, 80, textName.getPreferredSize().width,
textName.getPreferredSize().height);

    add(lastName);
    lastName.setBounds(110, 79, 100, lastName.getPreferredSize().height);
    add(textLast);
    textLast.setBounds(110, 100, textLast.getPreferredSize().width,
textLast.getPreferredSize().height);

    add(textComma);
    textComma.setBounds(212, 80, textComma.getPreferredSize().width,
        textComma.getPreferredSize().height);

    add(firstName);
    firstName.setBounds(220, 79, 100,
firstName.getPreferredSize().height);
    add(textFirst);
    textFirst.setBounds(220, 100, textFirst.getPreferredSize().width,
textFirst.getPreferredSize().height);

    add(mI);
    mI.setBounds(330, 79, 20, mI.getPreferredSize().height);
    add(textMI);
    textMI.setBounds(330, 100, textMI.getPreferredSize().width,
textMI.getPreferredSize().height);

    Date now = new Date();
       DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
       String today = df.format(now);
       date.setText(today);

    Calendar tNow = Calendar.getInstance ();
    String hour = String.valueOf(tNow.get(Calendar.HOUR_OF_DAY));
    if(hour.length() == 1){
       hour = "0" + hour;
    }
    String minute = String.valueOf(tNow.get(Calendar.MINUTE));
    if(minute.length() == 1){
       minute = "0" + minute;
    }
    time.setText(hour + ":" + minute);

   }

   public static void main(String args[]){
    JFrame frame = new JFrame("SliderDemo");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       //Create and set up the content pane.
       Panel panel = new Panel();
       panel.setSize(panel.getPreferredSize());
       frame.setSize(panel.getPreferredSize().width + 20,
panel.getPreferredSize().height + 20);
       panel.setOpaque(true); //content panes must be opaque
       frame.setContentPane(panel);

       //Display the window.
       frame.setVisible(true);
   }
}

I copied and pasted the code, deleting a few components, any errors
will be null pointers, which just delete the offending component.

As always, thank you for any help...
Paul Hamaker - 23 Dec 2006 09:02 GMT
The use of requestFocus is 'discouraged because its behavior is
platform dependent', try requestFocusInWindow instead.
--
http://javalessons.com  Paul Hamaker, SEMM
Teaching ICT since 1987


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.