Below is a code that reproduces the code of a student of a friend of mine who is a professor. The student if reinventing the well, trying to make something like the java.awt.CardLayout, but in this attempt a strange/curious behavior arise. I'm already tried various things to try to understand whats is happening, but I really can't figure out whats is going on. I put everything in the same file to make easier for you to run the code. Another thing: it's a student design and it is awfull.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
*
* @author David
*/
public class Test {
public static int currentPanel;
public static int selectedPanel;
/**
* @param args the command line arguments
*/
public static void main( String[] args ) {
Window w = new Window();
ButtonPanel btnPanel = new ButtonPanel();
Panel01 panel01 = new Panel01();
Panel02 panel02 = new Panel02();
w.setLayout( new BorderLayout() );
w.add( btnPanel, BorderLayout.CENTER );
w.setVisible( true );
while ( true ) {
System.out.println( currentPanel != selectedPanel );
if ( currentPanel != selectedPanel ) {
switch ( selectedPanel ) {
case 1:
currentPanel = selectedPanel;
w.remove( btnPanel );
w.add( panel01, BorderLayout.CENTER );
w.revalidate();
break;
case 2:
currentPanel = selectedPanel;
w.remove( btnPanel );
w.add( panel02, BorderLayout.CENTER );
w.revalidate();
break;
default:
break;
}
}
}
}
private static class Window extends JFrame {
public Window() {
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setSize( 400, 400 );
setLocationRelativeTo( null );
}
}
private static class ButtonPanel extends JPanel {
private JButton btn01;
private JButton btn02;
public ButtonPanel() {
btn01 = new JButton( "01" );
btn02 = new JButton( "02" );
btn01.addActionListener( new ActionListener() {
@Override
public void actionPerformed( ActionEvent e ) {
Test.selectedPanel = 1;
System.out.println( "btn 01 clicked" );
System.out.printf( " Test.selectedPanel: %d\n",
Test.selectedPanel );
}
});
btn02.addActionListener( new ActionListener() {
@Override
public void actionPerformed( ActionEvent e ) {
Test.selectedPanel = 2;
System.out.println( "btn 02 clicked" );
System.out.printf( " Test.selectedPanel: %d\n",
Test.selectedPanel );
}
});
add ( btn01 );
add ( btn02 );
}
}
private static class Panel01 extends JPanel {
public Panel01() {
add( new JLabel( "Panel 01" ) );
}
}
private static class Panel02 extends JPanel {
public Panel02() {
add( new JLabel( "Panel 02" ) );
}
}
}
If you compile and run this code, it will run as expected, i.e., open a JFrame with a JPanel that contains two JButtons. When one of these two buttons is clicked, it changes a static variable inside de Test class. This var is being "monitored" inside a while loop that, when changed, will remove the button panel from the JFrame and insert another JPanel. The code, as posted above, works, but, if you remove the line "System.out.println( currentPanel != selectedPanel );" it will "stop" to work, making the impression that the if statement inside the while loop is not being evaluated. If you start a debugger session with a breakpoint inside the while loop and without the System.out.println() line the code will work too! I really can't figure out what is happening. As I said, the code design is not good, but since the student wrote the code, now he, his professor and I want to know whats happening. It's probably something simple, but I really whant to know if somebody can figure out whats happening. If you remove the if statement, the code will work too. I tried various things like to insert the while loop inside a new Thread, but it also does not seem to work. It seems to be something related to synchronization...