Reputation: 134
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
Upvotes: 1
Views: 2896
Reputation: 36601
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.
Upvotes: 1
Reputation: 109815
setVisible(true);
must be last code lines in the in the public MainFrame() {
what should be this.keyboard = new start_vk();
???, maybe to use Swing Timer
for invoke something and dealyed
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
Upvotes: 2