PDA

View Full Version : JComboBox problem with repaint() methos


BK MEN
Oct 11, 2015, 09:44 AM
I am new to Java and I'm creating a clock with time zone. I have a little problem with JComboBox's repaint method. If I put repaint() in my code I can't see the JComboBox in the Applet window, and if I remove repaint() then it is visible. As you know it is very necessary to put repaint() in your code.
Analyse my code and tell me...
What am I doing wrong?


` package apps;


import java.awt.*;


import java.awt.event.*;


import java.util.Calendar;


import javax.swing.*;


public class Alarm2 extends JFrame implements ItemListener,Runnable{


private Graphics dbg;


private Image dbImage;


JComboBox c=new JComboBox();


Thread t;


int h,m,s;


boolean alarm=false;


String time="";






TextField tf=new TextField("SEX:",10);


Alarm2(){










setTitle("BALVEER");


setSize(250,250);


setResizable(false);


setLocationRelativeTo(null);


setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


c.addItem("MALE");


c.addItem("FEMALE");


setBackground(Color.GREEN);




c.addItemListener(this);


add(c);


add(tf);


setLayout(new FlowLayout(FlowLayout.RIGHT,10,195));


setVisible(true);




}
public void aisha(){


t=new Thread(this);


t.start();






}
public void run(){


try{


while(true){


Calendar cal=Calendar.getInstance();


h=cal.get(Calendar.HOUR);


m=cal.get(Calendar.MINUTE);




s=cal.get(Calendar.SECOND);


t.sleep(1000);


time=" "+h+" : "+m+" : "+s;


repaint();






}






}


catch(Exception e){






}
}


public void itemStateChanged(ItemEvent e){


tf.setText("SEX: "+e.getItem());


}






public void paint(Graphics g){


dbImage =createImage(getWidth(),getHeight());




dbg=dbImage.getGraphics();


draw(dbg);


g.drawImage(dbImage,0,0,this);


}


public void draw(Graphics g){


g.setColor(Color.red);


g.drawString("TIME:"+time, 50, 100);






}


public static void main(String[] args) {


Alarm2 java=new Alarm2();


java.aisha();


}
}`