JTextField is not focussed in JWindow

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jitheshborgia
    New Member
    • Jul 2009
    • 8

    JTextField is not focussed in JWindow

    Can anybody help me??? I have created a java page that extends JWindow. But i can't get focus on my JTextfield. The code is shown below

    import java.awt.*;
    import javax.swing.*;
    public class testWindow extends JWindow {
    JPanel pan;
    JTextField tf;
    public testWindow() {
    pan = new JPanel();
    tf = new JTextField(20);
    }
    add(pan);
    pan.add(tf);
    }
  • pbrockway2
    Recognized Expert New Member
    • Nov 2007
    • 151

    #2
    What happens when you compile that? If you get a compiler message that you can't understand, post the exact, complete message.

    Comment

    • jitheshborgia
      New Member
      • Jul 2009
      • 8

      #3
      compilation was success. There were no messages. But at the time of running, there was no focus to the text field.

      Comment

      • pbrockway2
        Recognized Expert New Member
        • Nov 2007
        • 151

        #4
        That's not true.

        Code:
        pbrockway@lind6off:~/Temp$ cat testWindow.java                                                                                                                                                        
        import java.awt.*;                                                                                                                                                                                    
        import javax.swing.*;                                                                                                                                                                                 
        public class testWindow extends JWindow {                                                                                                                                                             
        JPanel pan;                                                                                                                                                                                           
        JTextField tf;                                                                                                                                                                                        
        public testWindow() {                                                                                                                                                                                 
        pan = new JPanel();                                                                                                                                                                                   
        tf = new JTextField(20);                                                                                                                                                                              
        }                                                                                                                                                                                                     
        add(pan);                                                                                                                                                                                             
        pan.add(tf);                                                                                                                                                                                          
        }                                                                                                                                                                                                     
        pbrockway@lind6off:~/Temp$ javac -cp . testWindow.java                                                                                                                                                
        testWindow.java:10: invalid method declaration; return type required
        add(pan);
        ^
        testWindow.java:10: <identifier> expected
        add(pan);
               ^
        testWindow.java:11: <identifier> expected
        pan.add(tf);
               ^
        testWindow.java:11: <identifier> expected
        pan.add(tf);
                  ^
        4 errors
        pbrockway@lind6off:~/Temp$
        Last edited by pbrockway2; Feb 4 '10, 06:28 AM. Reason: Irrelevant output snipped

        Comment

        • jitheshborgia
          New Member
          • Jul 2009
          • 8

          #5
          This is the original program.

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

          public class testWindow extends JWindow
          {
          JTabbedPane tp;
          JPanel p1,p2;
          JButton be,bd,ba;
          JLabel l1,lh1,lh2;
          JTextField ta;
          public testWindow()
          {
          setVisible(true );
          setLocation(150 ,100);
          tp=new JTabbedPane();
          p1=new JPanel();
          p2=new JPanel();
          p1.setLayout(nu ll);
          p2.setLayout(nu ll);
          be=new JButton("edit") ;
          bd=new JButton("delete ");
          ba=new JButton("add");
          lh2=new JLabel("Languag e Listing");
          lh1=new JLabel("Add Language");
          l1=new JLabel("Languag e Name :");
          ta=new JTextField();
          // be.setBounds();
          //bd.setBounds();
          lh1.setBounds(1 70,120,150,25);
          lh2.setBounds(1 70,120,150,25);
          l1.setBounds(17 0,170,100,25);
          ta.setBounds(35 0,170,100,25);

          p1.add(ta);
          p1.add(ba);
          p1.add(lh1);
          p2.add(be);
          p1.add(l1);
          p2.add(lh2);

          setFocusableWin dowState(true);
          setFocusable(tr ue);
          boolean focus = isFocusableWind ow();
          System.out.prin tln(focus);
          tp.add("add",p1 );
          tp.add("list",p 2);
          add(tp);

          setSize(800,600 );

          }

          public static void main(String a[]) {
          testWindow act = new testWindow();
          act.show();
          }


          }

          Comment

          • pbrockway2
            Recognized Expert New Member
            • Nov 2007
            • 151

            #6
            Thanks. It's much easier to see what's going on we we know what the problem really is.

            As I guess you have already seen, that program does something when you run it: it prints "false". So we know that the JWindow instance is not a focusable window.

            The API documentation for isFocusableWind ow() explains why that is the case: "For a Window which is not a Frame or Dialog to be focusable, its focusable Window state must be set to true, its nearest owning Frame or Dialog must be showing on the screen, and it must contain at least one Component in its focus traversal cycle". The important bit is its nearest owning Frame or Dialog must be showing on the screen.

            So what is the nearest owning Frame or Dialog of your JWindow instance (and is it showing)?

            There is a JWindow constructor that lets you set the owner. But you are not using it, instead you use the no argument JWindow constructor. (the default one). It's worth while seeing what effect this has on the owner and focusability. Again the JWindow() constructor documentation says that it "Creates a window with no specified owner. This window will not be focusable".

            So the bottom line is that you have to create your JWindow with a constructor that specifies an owner for it. (And the other conditions described in isFocusableWind ow() have to be met).

            -----

            I would strongly suggest that you work through a Swing tutorial - Sun's is a good one - in order to see what's required and how you would go about writing the code.

            There are a few things in the code you posted that ring alarm bells: variable/class naming conventions (classes should start with a capital letter, variable names should be descriptive), making the window visible with a deprecated method, making it visible from the main() thread and the focussing woes. These can be put right if you take the time to follow a (good) tutorial.

            Comment

            • jitheshborgia
              New Member
              • Jul 2009
              • 8

              #7
              Thank you so much. I'll go through the instructions.. Thank you again...

              Comment

              • pbrockway2
                Recognized Expert New Member
                • Nov 2007
                • 151

                #8
                You're welcome.

                The important thing in my wordy answer is that you have to use the other JWindow constructor - the one with a window argument which will probably be a JFrame that you have to create. Post your attempt if you get stuck.

                Comment

                Working...