2

I wrote a class that extends JWindow that serves as a kind of customizable dialog box in my application. When I need to invoke one of these windows, I create a new instance of the class; to remove the window, I call the method dispose().

The problem I am having is that the user cannot edit components that have a text box, such as JTextField and JSpinner. The user can click on components such as drop-down boxes and buttons, and this works fine, but when it comes to entering text in a text box, this does not work.

Has anyone else experienced this problem?

Thanks!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
spookymodem
  • 531
  • 1
  • 9
  • 15
  • 2
    You will have to show us some code to get some useful help! In general, using `JTextField` (or `JSpinner` or whatever) works with `JWindow` as well as it works with any other top-level container. So there must be something you do in your code that prevents these component from working normally. – jfpoilpret Sep 15 '11 at 08:21

2 Answers2

3

There are a bunch of conditions to meet until a window child can receive focus, see the api doc for window.isFocusableWindow().

for most contexts it's enough to set its focusableWindowState property to true, like

    JFrame owner = new JFrame();
    owner.setVisible(true);
    JWindow window = new JWindow(owner);
    window.setFocusableWindowState(true);
    window.add(new JTextField("edit me"));
    window.setSize(200, 200);
    window.setVisible(true);
kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • +1 but, I've never had to use the `setFocusableWindowState()`. I just specify a visible JFrame as the owner and it works for me. Are there other problems I'm not aware of? – camickr Sep 15 '11 at 16:24
  • 1
    Did some testing and found that the default value is true for the focusable window state. Setting it false will turn of focusablity, but setting it true does not guarantee focusability. You still need to make sure the window has a visible owner JFrame. – camickr Sep 15 '11 at 19:00
  • @camickr - you are right, removing the setFocusableWindowState call gives the same result, as long as the owner is visible. Looks like my memory is .. incorrect. Thanks for checking :-) – kleopatra Sep 15 '11 at 21:45
1

1) maybe you are mixing AWT with Swing drop-down box and button, are you sure that all Components definitions starts with J, drop-down boxes == JComboBox, Button == JButton etc

2) don't create lots of Top-Level Containers on the Fly/Runtinme

3) Has anyone else experienced this problem? no, never

4) for real and better help sooner, please edit you post and sent here code that demonstrate your problem here are simples rulles sscce

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • I saw something similar this morning with a JDialog, might be related? The problem was running a task after setVisible returned that took a few seconds. This pause (immediately after setVisible returned) seemed to screw up the focus code, and when I next launched the JDialog, the text fields were un-clickable but buttons worked. Hitting tab showed me that the focus was still in the underlying parent window, not the new JPanel. Sounds suspiciously similar. – Andy Dingfelder Dec 12 '12 at 03:46