tags:

views:

53

answers:

2

I want to display an image as welll as some text on my JFrame in a way that both get displayed simultaneously part by part. I have implemented it but not able to perform it the right way.Is there anyone who could help me in solving this problem such that after certain portion of image some text shoulg get displayed and then image then text till my requirement but in a single JFrame. Thnx

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Show1 extends JFrame implements Runnable
{
ImageIcon i1;
JLabel j1;
Thread t;
JTextField jt;
boolean value=true;
 Show1()
 {

  setSize(1100,800);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setBackground(Color.black);
setLayout(null);
i1=new ImageIcon("Sunset.jpg");
j1=new JLabel("",i1,JLabel.CENTER);

jt=new JTextField(50);
jt.setBounds(250,750,100,50);
t=new Thread(this,"Main");
/*   try
     {
       SwingUtilities.invokeLater(
        new Runnable()
         {
           public void run()
            {
              makeGUI();
    makeGSI();
             }
         });
     }
    catch(Exception exc)
     {
       System.out.println("can't create because of"+exc);
      }
   }

private void makeGUI()
{*/


while(!value)
{
new Thread(this,"Image").start;

public void run()
{
int i,j;
try
{
for(i=-50;i<=500;i+=50)
{
j1.setBounds(200,100,700,i);
add(j1);
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
}




public void makeGSI()
{
t1=new Thread(this,"Message");
t1.setPriority(4);
t1.start();
}
public void run()
{
try
{
String msg[]={"Customer"," Care"," Services"};
int x=msg.length;
for(int i=0;i<=x-1;i++)
{
jt.setText(msg[i]);
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
}
class Show
{
public static void main(String args[])
{
Show1 sh=new Show1();

}}
+2  A: 

Whatever it is you're trying to do there (I still don't quite understand what exactly it is), Swing doesn't allow updates to the UI from multiple threads. You have to make sure that only the AWT Event Dispatcher Thread will make updated to the UI.

There are helper methods in SwingUtilities such as invokeAndWait(Runnable) or invokeLater(Runnable) to help with that.

Joey
+2  A: 

You might want to use a SwingWorker to

  • load your image in one thread and then
  • set your frame visible on the AWT thread.

Here is another article describing the SwingWorker.

akf