How can I put a loading screen that would be visible in java applet until the graph is drawn? My code:
public class Test extends JApplet
{
GrappaPanel gp = null;
JPanel jpanel = null;
JEditorPane dataDisplayer = null;
Graph graph = null;
JProgressBar progressBar = null;
public void init() {
Parser program =null;
InputStream input;
String sgraph = getGraph();
try {
input = new ByteArrayInputStream(sgraph.getBytes("UTF-8"));
program = new Parser(input);
program.parse();
} catch (Exception e) {
e.printStackTrace();
}
graph = program.getGraph();
gp = new GrappaPanel(graph);
JScrollPane jspg = new JScrollPane(gp,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
GridBagLayout gb = new GridBagLayout();
jpanel = new JPanel(gb);
setContentPane(jpanel);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gb.setConstraints(jspg,gbc);
jpanel.add(jspg);
}
private String getGraph(){
...
}
}
Drawing the graph in the applet takes the biggest part of the loading time. Getting the string that represents graph and parsing it to Graph object is relatively cheap. Any help would be appreciated!
Use a CardLayout
with the JProgressBar
(or an animated 'loading' icon) in one card, and the graph in the other. Default to the 'loading' card first, then do the long running task in a SwingWorker
. When the results are returned, prepare the graph, add it to the other card & flip to that card.