I’m quite new to java, but have run into a problem I don’t understand. I want a jbutton to show one window and hide another. I have done this with an endless loop listening for a variable change when the jbutton is pressed.
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) { //Jbutton in question
NewJFrame frame = new NewJFrame(); //another JFrame from which I created the public variable(Visible)
frame.Visible = false;
}
while (always == true) { **//code in main method to test variable change**
if (frame1.Visible == true) {
frame1.show();
frame2.hide();
}
else {
frame1.show();
frame2.hide();
}
Interrestingly, this works when I do the same with a button inside the JFrame of which the public variable (Visible) is created, AKA: the [if] part, but the else does not execute.
Is there something I can do to get the Main method to rocognize this variable change?
I think you have a logic error.
You say
if(frame1.Visible)thenframe1.show(). Butframe1is already visible! It doesn’t look like you ever hide it.My guess would be your intentions were instead to have:
Also, as a side note, you don’t have to use
== trueor== falsewith booleans. They will be evaluated in the conditional automatically as whichever value they represent.