JTextField uneditable in JWindow

I have a Jwindow, and when I added a Jtextfield to it, the text field became unedited.

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 the owner of Jwindow, the text box was now edited, but the frame appeared along with 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:

  • Why is JTextField invalid in JWindow and how can I make it editable?
  • What is the main purpose of using a JFrame as the border of a JWindow?
+4
source share
3 answers

EDIT,

  • JWindow content is only available if its parent is displayed on the screen

  • for editable and accessible content use un_decorated JDialog instead of JWindow , jDialog does not call sloppy content,

  • the reason why ... I can’t explain, but it’s not clear why, in no way at this moment the API tells me anything about the called accessible, 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 requires a JFrame to work around properly.

  • no one will say that this JFrame should be visible (valid for the GUI), then remove these lines of code from frame.setDefaultClose .... including frame.setVisible(true); from my example

  • in this form, the current JVM instance never exited RAM until your computer reboots or shuts down, you need to add the highlighted exit JButton using the System.exit(0) line of code inside the ActionListener

+4
source

JWindow must be customizable. Use the public void setFocusable(boolean focusable) .

+3
source
+1
source

Source: https://habr.com/ru/post/1400940/


All Articles