5

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?
Waynn Lue
  • 11,344
  • 8
  • 51
  • 76
TU_HEO DAKAI
  • 2,197
  • 8
  • 28
  • 39
  • Take a look here http://www.velocityreviews.com/forums/t366288-jtextfield-inactive-in-a-jwindow.html – RanRag Mar 12 '12 at 06:50
  • The problem is that when i used jframe, then both the jwindow and jframe appeared at the same time.. – TU_HEO DAKAI Mar 12 '12 at 06:56
  • Are you sure you read the complete solution provided in that link. – RanRag Mar 12 '12 at 06:57
  • Yeah, im sure (with Andrew's code in ur link) – TU_HEO DAKAI Mar 12 '12 at 08:34
  • unrelated to your problem: _do not_ locate/size components manually, that's the job of a LayoutManager – kleopatra Mar 12 '12 at 09:33
  • for 1) no. Read the api doc as well: http://docs.oracle.com/javase/7/docs/api/java/awt/Window.html#isFocusableWindow. But: what is your goal? Why do you want a _window_ (as opposed to an undecorated dialog) that doesn't appear in the taskbar? – kleopatra Mar 12 '12 at 09:49

2 Answers2

5

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

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Thanks, but it doesn't work on my machine, in the sense that it didn't let the JWindow appear without the JFrame. – TU_HEO DAKAI Mar 12 '12 at 08:57
  • @Fff Dgg no idea what and why something doesn't works on your machine, there must be another issue hidden you your code, I'm assuning that my code example works, – mKorbel Mar 12 '12 at 09:01
  • This is your code's output on my machine, pzl have a look : http://flic.kr/p/bCpojk – TU_HEO DAKAI Mar 12 '12 at 09:09
  • @Fff Dgg right agreed, my question are 1) both JTextField are accesible and editable isn't it ??? 2) do you want to display only JWindow ???, 3) is/are there another issue(s) – mKorbel Mar 12 '12 at 09:13
  • Yes, both are editable, and i wannt only JWindow to be displayed. Thanks!! – TU_HEO DAKAI Mar 12 '12 at 09:15
  • When i deleted codes from : frame.setDefaultClose.. to frame.setVisible(true) >> there's nothing appeared. Only deleted the last code frame.setVisible(true) >> the jframe disappeared, but again the JWindow is uneditable.. Im using java 1.6.. – TU_HEO DAKAI Mar 12 '12 at 09:40
  • _really don't know_ reading the api doc helps :-) http://docs.oracle.com/javase/7/docs/api/java/awt/Window.html#isFocusableWindow clearly states that the second bullet of your edit is wrong – kleopatra Mar 12 '12 at 09:51
  • @kleopatra this code caused non_editable JTextField for me too – mKorbel Mar 12 '12 at 10:00
  • @Fff Dgg no way, true is that I never used alone JWindow, for the rest see my edit here – mKorbel Mar 12 '12 at 11:11
3

The JWindow should be focusable. Use public void setFocusable(boolean focusable) method.

RanRag
  • 48,359
  • 38
  • 114
  • 167
StanislavL
  • 56,971
  • 9
  • 68
  • 98