I can seek the items randomly in Jlist with a next button. When I press the next button fastly, I'm getting the exception: Exception in thread "Thread-7" java.lang.ClassCastException: sun.java2d.NullSurfaceData cannot be cast to sun.java2d.d3d.D3DSurfaceData So since the frequency of clicking next triggers this problem can we say there's something wrong with threads? As a note there is no update in jlist. Jlist is been initialized and filled with elements at start and then just seeking the items randomly with the next button. If I remove ensureIndexIsVisible then there's no trouble.
I hope below summary code helps:
public class B_Object {
private NewJFrame njf = new NewJFrame();
HandlePlay hPlay = new HandlePlay(njf);
njf.nextButton.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
playNext();
}
});
}
public class HandlePlay {
private NewJFrame n_j_f;
HandlePlay(NewJFrame njf){
n_j_f = njf;
}
private void setDisplay(int i) {
String str = "dummy";
n_j_f.jList1.setSelectedIndex(i);
n_j_f.setTitle(str);
n_j_f.jTextArea1.setText(str);
n_j_f.jList1.ensureIndexIsVisible(n_j_f.jList1.getSelectedIndex());
}
}
After trying different threads and searching the event dispatch thread I found it's related with EDT. To solve this issue following modification is enough:
private void test(final int i) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
jList1.setSelectedIndex(i);
System.out.println("javax.swing.SwingUtilities.isEventDispatchThread()=" + javax.swing.SwingUtilities.isEventDispatchThread());
jList1.ensureIndexIsVisible(i);
}
});
}
So make sure that if you have such gui problems intermittenly occuring, check if you're doing that operation within the EDT, checking by isEventDispatchThread().