Java this.dispose not closing window when called
I am writing a program from class, and I am attempting to have it set up so a window is created which shows search results in the form of buttons. I would like it if there are no search results, that the window would call a pop-up warning stating such and then just close the window.
I have it setup that whenever I want to make the window close, I call a CloseWindow() method that just contains a this.dispose(); command. If I call it from the actionEvent method once a button is pushed, the window closes fine, but if I try to call it almost anywhere else in the method, it will not close the window. Is there some basic Java concept I am missing? I know the JFrame has the dispose method from the Window class, but "this" seems to only work under certain conditions.
The relevant code is below:
public class MovieSearch extends JFrame implements ActionListener, Serializable{
private static final long serialVersionUID = 7526471155622776147L;
private Container con = getContentPane();
int llSize, searchResults = 0;
MovieNode currentNode;
String searchText;
JPanel listPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
JScrollPane scrollPane = new JScrollPane(listPanel);
public MovieSearch(String searchText){
super("Search Results");
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.searchText = searchText;
con.add(scrollPane);
currentNode = MovieView.firstNode;
for(int i = 0; i < llSize; i++){
if (currentNode.getTitle().indexOf(searchText) != -1) {
BufferedImage Thumbnail = new BufferedImage(200, 300, BufferedImage.TYPE_INT_ARGB);
Thumbnail.getGraphics().drawImage(currentNode.getImage().getImage(), 0, 0, 200, 300, null);
ImageIcon icon = new ImageIcon(Thumbnail);
JButton button = new JButton("Go to " + currentNode.getTitle());
button.addActionListener(this);
button.setVerticalTextPosition(AbstractButton.BOTTOM);
button.setHorizontalTextPosition(AbstractButton.CENTER);
button.setIcon(icon);
listPanel.add(button);
searchResults++;
currentNode = currentNode.getLink();
} else {
System.out.println("String " + currentNode.getTitle() + " does not contain String " + searchText);
currentNode = currentNode.getLink();
}
}
if(searchResults == 0){
int messageType = JOptionPane.ERROR_MESSAGE;
JOptionPane.showMessageDialog(null, "No results match that query.", "NO RESULTS!", messageType);
CloseWindow();
}else{
currentNode = MovieView.firstNode;
repaint();
}
}
public 开发者_JAVA百科void actionPerformed(ActionEvent e){
Object source = e.getSource();
for(int i = 0; i < llSize; i++){
JButton button;
button = (JButton) source;
if(button.getText().equals(("Go to " + currentNode.getTitle()))){
MovieView.currentNode = currentNode;
MovieView.searchTextField.setText("");
CloseWindow();
}
System.out.println("button is " + button.getText());
System.out.println("text is: " + "Go to " + currentNode.getTitle());
currentNode = currentNode.getLink();
}
}
private void CloseWindow(){
System.out.println("Closing Window");
this.dispose();
}
}
Again, the CloseWindow() method [and hence the this.dispose() method] works when called form the ActionEvent method but not from anywhere else. [I have inserted it into other places just to test and it is reached but it still does not close the window.]
As you can see, I put a println in the CloseWindow() method to make sure that it was being reached and it is reached every time, it just isn't working.
Any insight into this would be very appreciated. Thank you for your time.
A JOptionPane creates a "modal dialog" which means that the statements after the "showMessageDialog" to not execute until after the dialog is closed.
You have two options:
a) create you own custom "non modal dialog" that displays your message and then closes. b) Read the JOptionPane API. It shows you how to manually access the dialog that is create by the JOptionPane class so you have a reference to the dialog.
In both cases you would need to start a Swing Timer before you display the dialog. Then when the Timer fires you can dispose the dialog.
精彩评论