Search code examples
javaswingjtextfield

JTextField uneditable in JWindow


I have a Jwindow, and when I added a Jtextfield to it, the textfield became uneditable.

JWindow window = new JWindow();
window.setBounds(400, 100, 700,500);
window.setVisible(true);
window.setLayout(null);
JTextField text = new JTextField();
text.setBounds(300, 300, 150, 30);
text.setEditable(true);       
window.getContentPane().add(text);

But when I tried to use Jframe as Jwindow's owner, the textfield was now editable, but the frame showed up together with the jwindow :

JFrame frame = new JFrame();
frame.setVisible(true);
JWindow window = new JWindow();
window.setBounds(400, 100, 700,500);
window.setVisible(true);
window.setLayout(null);
JTextField text = new JTextField();
text.setBounds(300, 300, 150, 30);
text.setEditable(true);       
window.getContentPane().add(text);

So, I have 2 questions :

  1. Why JTextField is uneditable in JWindow and how could I make it editable?
  2. What is the main purpose of using JFrame as JWindow's border?

Solution

  • EDIT,

    • contents of JWindow is accesible only if its parent is displayed on the screen

    • for editable and accesible contents use un_decorated JDialog instead of JWindow, jDialog doesn't caused non_accesible contents,

    • reason why ..., I can't explain, not undestand why, no way in this moment, the API says me nothing about caused accesible, editable ...

    . . .

    1. Why JTextField is uneditable in JWindow and how could i let it able to edit?
    

    really don't know

    import java.awt.*;
    import javax.swing.*;
    
    public class WindowTest {
    
        private JFrame frame;
    
        public JPanel createContentPane() {
            JTextField text = new JTextField("Whatewer");        
            JPanel panel = new JPanel();
            panel.add(text);
            createAndShowWindow();
            return panel;
        }
    
        void createAndShowGUI() {
            frame = new JFrame("Window Test");
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.setContentPane(createContentPane());
            frame.setLocation(50, 50);
            frame.pack();
            frame.setVisible(true);
        }
    
        private void createAndShowWindow() {
            JTextField text = new JTextField("Whatewer");
            JWindow win = new JWindow(frame);
            win.setLayout(new GridLayout(0, 1));
            win.add(text);
            win.pack();
            win.setLocation(150, 50);
            win.setVisible(true);
        }
    
        public static void main(String args[]) {
            EventQueue.invokeLater(new Runnable() {
    
                public void run() {
                    new WindowTest().createAndShowGUI();
                }
            });
        }
    }
    

    EDIT

    Yes, both are editable, and i wannt only JWindow to be displayed. Thanks!! 
    
    • by default JWindow required JFrame for correct workaround

    • nobody tell that this JFrame must be visible (valid for GUI), then remove these code lines from frame.setDefaultClose.... including frame.setVisible(true); from my example

    • in this form current JVM instance never gone from RAM, untill your PC restarted or swith off, you have to add separated exit JButton with code line System.exit(0) inside ActionListener