473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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);
}
Feb 3 '10 #1
7 7019
pbrockway2
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
pbrockway2
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$
  29.  
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()
{
setVisible(true);
setLocation(150,100);
tp=new JTabbedPane();
p1=new JPanel();
p2=new JPanel();
p1.setLayout(null);
p2.setLayout(null);
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();
//bd.setBounds();
lh1.setBounds(170,120,150,25);
lh2.setBounds(170,120,150,25);
l1.setBounds(170,170,100,25);
ta.setBounds(350,170,100,25);

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

setFocusableWindowState(true);
setFocusable(true);
boolean focus = isFocusableWindow();
System.out.println(focus);
tp.add("add",p1);
tp.add("list",p2);
add(tp);

setSize(800,600);

}

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


}
Feb 4 '10 #5
pbrockway2
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
pbrockway2
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

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

Similar topics

0
by: Alexander Mazo | last post by:
Hi! I'm new in JAVA. I'm wont change "Home" and "End", but nothing receive. public class RLTextField { public static String s = new String(); public static void main(String args) { JFrame...
0
by: kumar | last post by:
hai, i have a small problem regarding the focussed button. i have an application. in that application i will call some exe. so that a pop will be appear with two buttons. in those 2 buttons...
0
by: AA Arens | last post by:
I made a find record button on my form "company" (with field for name, phone nr, e-mail etc.). I expect that when I focus on one of the field, the "look in" of the find-record dialog box should...
2
by: AA Arens | last post by:
When I push on my find-record button, after I focussed on the ID-number field on my form, the default search field in the Search-form dialog menu is not the focussed field, but another. Why? ...
0
JoeMac3313
by: JoeMac3313 | last post by:
The question is how can I update the JTextField in Class CreditDisplayField when I hit the button in class CreditButton? I wrote in a test printing to console to make sure the "logic.addCredits();"...
2
by: all eyes | last post by:
hi all, i am using here JTextField to get sum of all previously pressed numbers in one button called (+) and so on. there is only one button take care of addition and displaying the result in the...
1
by: ahmed222too | last post by:
the jtextfield donot accept Arabic language font when i run this code jtextfield.settext="ذهب الولد إلى المدرسة"; the arabic font change into another font in the jtextfield
4
by: ahmed222too | last post by:
when i read data from file by code into jtextfield the lines of the file apprear in Concatenation in the jtextfield (without Enters between lines) i want to transfere the content of the file to the...
1
by: dig909 | last post by:
I am trying to compare a number inserted into a jtextfield with a number generated with the program. Unfortunately the numbers never evaluate as equal. Here is an example of the code I'm working...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.