S
Sam
All
I am fairly new at Swing and trying the foll.
Load an image to a Panel inside a SplitPane... and then make the
program to add another image after the first one. This is more of a
testing to learn threads better but here is the code i am faltering
with.
public class MyCanvas extends JFrame {
JSplitPane jSplitPane1 = new JSplitPane();
Image img;
JPanel jPanel1 = new JPanel();
VerticalFlowLayout verticalFlowLayout1 = new VerticalFlowLayout();
public MyCanvas() {
try {
setTitle("Frame Canvas");
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void jbInit() throws Exception
{
this.getContentPane().add(jSplitPane1,
java.awt.BorderLayout.CENTER);
jPanel1.setLayout(verticalFlowLayout1);
// Adding the first image to be displayed.....
img = Toolkit.getDefaultToolkit().getImage("first.jpg");
img = img.getScaledInstance(70, 80, 0);
jPanel1.add(new JLabel(new ImageIcon(img)));
//
// Start running a thread to load one more...
Runnable updateComponent1 = new Runnable() {
public void run()
{
try
{
Thread.sleep(5000);
img =
Toolkit.getDefaultToolkit().getImage("second.jpg");
img = img.getScaledInstance(70, 80, 0);
jPanel1.add(new JLabel(new ImageIcon(img)));
} catch(Exception ex)
{
ex.printStackTrace();
}
validate();
}
};
javax.swing.SwingUtilities.invokeLater(updateComponent1);
}
public static void main (String args[]) {
MyCanvas frame = new MyCanvas();
frame.setSize(600,600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
for me the frame shows up w/o the first image and then both first and
the second show up together. I think i am missing something here.
Please assist.
Thanks
Sam
I am fairly new at Swing and trying the foll.
Load an image to a Panel inside a SplitPane... and then make the
program to add another image after the first one. This is more of a
testing to learn threads better but here is the code i am faltering
with.
public class MyCanvas extends JFrame {
JSplitPane jSplitPane1 = new JSplitPane();
Image img;
JPanel jPanel1 = new JPanel();
VerticalFlowLayout verticalFlowLayout1 = new VerticalFlowLayout();
public MyCanvas() {
try {
setTitle("Frame Canvas");
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void jbInit() throws Exception
{
this.getContentPane().add(jSplitPane1,
java.awt.BorderLayout.CENTER);
jPanel1.setLayout(verticalFlowLayout1);
// Adding the first image to be displayed.....
img = Toolkit.getDefaultToolkit().getImage("first.jpg");
img = img.getScaledInstance(70, 80, 0);
jPanel1.add(new JLabel(new ImageIcon(img)));
//
// Start running a thread to load one more...
Runnable updateComponent1 = new Runnable() {
public void run()
{
try
{
Thread.sleep(5000);
img =
Toolkit.getDefaultToolkit().getImage("second.jpg");
img = img.getScaledInstance(70, 80, 0);
jPanel1.add(new JLabel(new ImageIcon(img)));
} catch(Exception ex)
{
ex.printStackTrace();
}
validate();
}
};
javax.swing.SwingUtilities.invokeLater(updateComponent1);
}
public static void main (String args[]) {
MyCanvas frame = new MyCanvas();
frame.setSize(600,600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
sleep in the runnable) on the second image from being displayed. ButFrom what i understand the invokeLater should only halt (because of the
for me the frame shows up w/o the first image and then both first and
the second show up together. I think i am missing something here.
Please assist.
Thanks
Sam