469,658 Members | 1,855 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,658 developers. It's quick & easy.

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);
Feb 3 '10 #1
7 6414
151 Expert 100+
What happens when you compile that? If you get a compiler message that you can't understand, post the exact, complete message.
Feb 3 '10 #2
compilation was success. There were no messages. But at the time of running, there was no focus to the text field.
Feb 4 '10 #3
151 Expert 100+
That's not true.

Expand|Select|Wrap|Line Numbers
  1. pbrockway@lind6off:~/Temp$ cat testWindow.java                                                                                                                                                        
  2. import java.awt.*;                                                                                                                                                                                    
  3. import javax.swing.*;                                                                                                                                                                                 
  4. public class testWindow extends JWindow {                                                                                                                                                             
  5. JPanel pan;                                                                                                                                                                                           
  6. JTextField tf;                                                                                                                                                                                        
  7. public testWindow() {                                                                                                                                                                                 
  8. pan = new JPanel();                                                                                                                                                                                   
  9. tf = new JTextField(20);                                                                                                                                                                              
  10. }                                                                                                                                                                                                     
  11. add(pan);                                                                                                                                                                                             
  12. pan.add(tf);                                                                                                                                                                                          
  13. }                                                                                                                                                                                                     
  14. pbrockway@lind6off:~/Temp$ javac -cp . testWindow.java                                                                                                                                                
  15. testWindow.java:10: invalid method declaration; return type required
  16. add(pan);
  17. ^
  18. testWindow.java:10: <identifier> expected
  19. add(pan);
  20.        ^
  21. testWindow.java:11: <identifier> expected
  22. pan.add(tf);
  23.        ^
  24. testWindow.java:11: <identifier> expected
  25. pan.add(tf);
  26.           ^
  27. 4 errors
  28. pbrockway@lind6off:~/Temp$
Feb 4 '10 #4
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()
tp=new JTabbedPane();
p1=new JPanel();
p2=new JPanel();
be=new JButton("edit");
bd=new JButton("delete");
ba=new JButton("add");
lh2=new JLabel("Language Listing");
lh1=new JLabel("Add Language");
l1=new JLabel("Language Name :");
ta=new JTextField();
// be.setBounds();


boolean focus = isFocusableWindow();



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

Feb 4 '10 #5
151 Expert 100+
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 isFocusableWindow() 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 isFocusableWindow() 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.
Feb 4 '10 #6
Thank you so much. I'll go through the instructions.. Thank you again...
Feb 5 '10 #7
151 Expert 100+
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.
Feb 5 '10 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Alexander Mazo | last post: by
1 post views Thread by dig909 | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.