In swing application, I am using DefaultTableCellRenderer
to flash the table cell when the cell time is equal to system time. I wrote if statement to compare cell time with hh:mm
, If both time are equal, time cells backgroung will blink on the table rows. It is blinking 60 secs only still if statement is true, But i want to continue blinking same cell after false if statement.
I got the position value of blinking cell like (0,2) and (1,2). from this integer, how can i set continues blink after false if statement or Is there any other way? Thank you.
My Code is here :
I added table method and inner class.
public void table() throws SQLException
{
Connection c=null;
PreparedStatement pre;
try
{
Class.forName("com.mysql.jdbc.Driver");
c=DriverManager.getConnection("jdbc:mysql://localhost:3306/telecaller_database", "root", "root");
Statement st= c.createStatement();
DateFormat df=new SimpleDateFormat(" dd/MM/yyyy");
Calendar cl=Calendar.getInstance();
Date dt1=cl.getTime();
String str1=df.format(dt1);
System.out.println("today date is "+str1);
pre=c.prepareStatement("Select name,phoneno,time from components where date=?");
pre.setString(1,str1);
ArrayList<String> arl=new ArrayList<String>();
ResultSet rs=pre.executeQuery();
ResultSetMetaData md=rs.getMetaData();
System.out.println("result set data "+pre);
int column=md.getColumnCount();
System.out.println(column);
for(int i=1;i<=column;i++)
{
columns.addElement(md.getColumnName(i));
}
while(rs.next())
{
arl.add(rs.getString(1));
System.out.println(arl);
Vector row=new Vector(column);
for(int j=1;j<=column;j++)
{
row.addElement(rs.getString(j));
}
rows.addElement(row);
}
rs.close();
st.close();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
final JTable table=new JTable(rows,columns);
Border bd2=BorderFactory.createLineBorder(Color.black,1);
table.setBorder(bd2);
table.setDefaultRenderer(Object.class, (TableCellRenderer) new MyFlashingCellRenderer2());
final long startTime = System.currentTimeMillis();
Thread thread = new Thread()
{
public void run()
{
while(true)
{
long now = System.currentTimeMillis();
long second = (now -startTime) / 1000;
// System.out.println(second);
color = second / 2 * 2 == second ? Color.red : Color.yellow;
color1= second / 2 * 2 == second ? Color.green : Color.blue;
// System.out.println(second/2*2);
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
table.tableChanged(new TableModelEvent(table.getModel()));
//table.setBackground(color1);
}
});
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
};
thread.start();
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(14,20,345,400);
jp1.add(scrollPane);
}
public class MyFlashingCellRenderer extends DefaultTableCellRenderer
{
public int cellr;
public int cellc;
public Component getTableCellRendererComponent(JTable table, Object rows1, boolean isSelected, boolean hasFocus, int row, int column)
{
JLabel label = (JLabel)super.getTableCellRendererComponent(table, rows1, isSelected, hasFocus, row, column);
String timeformat = " h:mm a";
SimpleDateFormat obDateFormat = new SimpleDateFormat(timeformat);
Calendar time = Calendar.getInstance();
String time1=obDateFormat.format(time.getTime()).toString();
//System.out.println("metod "+cellr+cellc);
if (time1.equals(rows1))
{
cellr=row;
cellc=column;
System.out.println("time "+time1+" row "+rows1);
getTableCellRendererComponent2(table, rows1, isSelected, hasFocus, row, column);
}
else if(!time1.equals(rows1))
{
label.setBackground(null);
}
return label;
}
public Component getTableCellRendererComponent2(JTable table, Object rows1, boolean isSelected,
boolean hasFocus, int x, int y)
{
JLabel label = (JLabel)super.getTableCellRendererComponent(table, rows1, isSelected, hasFocus, x, y);
label.setBackground(color);
System.out.println("outer method "+cellr+cellc);
x=cellr; y=cellc; // potition of blinking cell
System.out.println("x and y "+x+y);
return label;
}
}