The sequence of execution that is happening is : First I need to type correct password Then JOptionPane msgBox pops out I hit the "ok" button then nothing happens in JFrame but as per the code under actionPerformed,the textField should be set visible. I noticed that when I change the state of JFrame from maximise to minimise or visa versa then the textField becomes visible. I need the JFrame to change instantly without waiting for any mouse event or since I haven't involved any.
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class GraphicItems extends JFrame{
private JPasswordField password = new JPasswordField(10);
private JTextField textField;
public GraphicItems(){
super("Graphics is fun");
setLayout(new FlowLayout());
textField = new JTextField("This secret will reveal after correct password");
textField.setEditable(false);
textField.setVisible(false);
add(textField);
add(password);
HandlerClass theHandler = new HandlerClass();
password.addActionListener(theHandler);
}//end graphicItems constructor
private class HandlerClass implements ActionListener{
public void actionPerformed(ActionEvent event){
if(event.getSource()==password)
if(password.getText().equalsIgnoreCase("kamal123")){
JOptionPane.showMessageDialog(null,"CorrectPassword","MessageBox",JOptionPane.INFORMATION_MESSAGE);
textField.setVisible(true);
}//end if
}//end actionPerformed
}//end HandlerClass
}//end graphicItems Class
public class MainClass{
public static void main(String args[]){
GraphicItems frameObj = new GraphicItems();
frameObj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frameObj.setSize(500,500);
frameObj.setVisible(true);
}//main method ended
}//MainClass ended
You must
repaint()
your component in order to see changes, this is what is happening when you minimize and maximize your window.Check this answer to see correct way to use
repaint()