After calling setVisible(false) my JFrame contents are gone when calling set Visible(true)

怎甘沉沦 提交于 2019-12-02 08:24:00

问题


I'm designing a drawing program (in Java) in which text should be drawn. Since I'm doing this with kinect I'd like to use an onscreenKeyboard which I've already found. This keyboard is basically a JFrame witch JComponents in it, I dont't want to go too much in detail ...

public MainFrame() {
    super("Draw");
    setLayout(new BorderLayout());
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null); //so basically some standard stuff
    makeGUI();
    this.keyboard = new start_vk(); //strange name for a class but I didn't make it
    }

In start_vk I could call setVisible(true) and it'd works but I would love to actually call this later only when I need it ... Now I call at some place the setVisible(true) and only the JFrame appears with no Components in it ...

Should I call this in an apart Thread because the constructor for start_vk is done with SwingUtilities.invokeLater()? Or any other suggestion?

 public start_vk() {
 SwingUtilities.invokeLater(new Runnable() {
     public void run() {
        myConf = defaultConf.setDefault(myConf);
        myKeys = defaultConf.setKeyboard(myKeys);
        readConf();
        thisClass = new vk_gui();
        thisClass.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        thisClass.setVisible(true); // I've set it to true here but would like to set it to false to be able to call it later ...
     }
  });
  }

  public  void newText(){
      thisClass.newText();
  }

  public  boolean isVisible(){
      return thisClass.isVisible();
  }

  public  String getText(){
      return thisClass.getText();
  }

The code in vk_gui:

public String getText(){
   if (super.isVisible())
       return null;
   return jTextArea.getText();
}

public void newText(){
   this.setVisible(true);
   this.setAlwaysOnTop(true);
   jTextArea.setText("");
}

And this is how I call it then:

keyboard.newText();
while(keyboard.getText()==null){} // I know it's busy waiting and it's not good and all that ...
text = keyboard.getText();

Thanks for any suggestions, Max


回答1:


  1. setVisible(true); must be last code lines in the in the public MainFrame() {

  2. what should be this.keyboard = new start_vk(); ???, maybe to use Swing Timer for invoke something and dealyed

  3. After calling setVisible(false) my JFrame contents are gone when calling set Visible(true)

if you add or remove something from/to already visible container you have to call

nearestContainer.revalidate();
// for JFrame/JDialog/JWindow and upto Java7 is there only validate();
nearestContainer.reapaint()

. 4. for better help sooner post an SSCCE




回答2:


Since your setVisible( true ) call is one of the first calls (before you added any components to the container), you should revalidate the layout.

This is clearly mentioned in the javadoc of those methods (e.g. a copy-paste from the Container#add method):

This method changes layout-related information, and therefore, invalidates the component hierarchy. If the container has already been displayed, the hierarchy must be validated thereafter in order to display the added component.



来源:https://stackoverflow.com/questions/11085804/after-calling-setvisiblefalse-my-jframe-contents-are-gone-when-calling-set-vis

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!