Can't get JTextArea to gain focus in JWindow, works in JFrame
I usually use JFrames for my top level container, but now I'm trying to work with a JWindow. I can't get the JTextArea in the JWindow to accept keyboard input. It's clear from the behavior that it just isn't getting the focus. If I change the class from JWindow to JFrame, it works. What do I need to do differently in a JWindow to let the user type text into the JTextArea? I added an invocation of requestFocus() on the JTextArea, even though that's not needed with JFrame, but it didn't help.
Here's a simple program to demo the above two problems. Run the code and see that you can't enter text into the JTextArea. Change the class to JFrame and you can enter text into the JTextArea.
package net.midnightjava.rcb;
import java.awt.Container;
import java.awt.Point;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JWindow;
publicclass FilterWindowTestextends JWindow{
privatefinalint WIDTH = 200;
privatefinalint HEIGHT = 200;
Point location;
MouseEvent pressed;
public FilterWindowTest(){
Container contentPane = getContentPane();
contentPane.setLayout(null);
JTextArea allowArea =new JTextArea();
allowArea.setEditable(true);
JScrollPane allowPane =new JScrollPane(allowArea);
allowArea.setBackground(contentPane.getBackground());
allowPane.setBackground(contentPane.getBackground());
allowPane.setOpaque(true);
allowArea.setOpaque(true);
allowArea.setText("This is the allow pane");
allowPane.setBounds(10, 10, 100, 100);
contentPane.add(allowPane);
this.setSize(WIDTH, HEIGHT);
this.setLocationRelativeTo(null);
allowPane.requestFocus();
setVisible(true);
}
publicstaticvoid main(String[] args){
new FilterWindowTest();
}
}
# 3
Actually, I'm calling the JWindow from a JFrame, so it makes sense to call the JWindow constructor with JFrame as an argument and let the JFrame be the owner. The JFrame will be visible when the JWindow is created and shown, so according to the API, the JWindow should be focusable.
But alas, such is not my experience with the following code. Execute FilterWindowFrame's main method and you'll see a button. Click the button and you'll see the JWindow, but it is not focusable.
I tried using the no-argument constructor for the JWindow and inserting the statement
super(new JFrame(){public boolean isShowing(){return true;}});
in the constructor, and this works. The JWindow is focusable. But why won't it work with the JFrame object as the JWindow object's parent?
Here's the JFrame
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class FilterWindowFrame extends JFrame implements ActionListener{
public static void main(String[] args){
new FilterWindowFrame();
}
private FilterWindowFrame(){
JButton button = new JButton("Show the JWindow");
button.addActionListener(this);
getContentPane().add(button);
pack();
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
FilterWindowTest fwt = new FilterWindowTest(this);
fwt.setVisible(true);
}
}
and here's the JWindow
import java.awt.Container;
import java.awt.Point;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JWindow;
public class FilterWindowTest extends JWindow {
private final int WIDTH = 200;
private final int HEIGHT = 200;
Point location;
MouseEvent pressed;
public FilterWindowTest(JFrame frame){
Container contentPane = getContentPane();
contentPane.setLayout(null);
JTextArea allowArea = new JTextArea();
allowArea.setEditable(true);
JScrollPane allowPane = new JScrollPane(allowArea);
allowArea.setBackground(contentPane.getBackground());
allowPane.setBackground(contentPane.getBackground());
allowPane.setOpaque(true);
allowArea.setOpaque(true);
allowArea.setText("This is the allow pane");
allowPane.setBounds(10, 10, 100, 100);
contentPane.add(allowPane);
this.setSize(WIDTH, HEIGHT);
this.setLocationRelativeTo(null);
allowPane.requestFocus();
}
}