....................................filter class
import java.awt.Color;
import java.awt.image.BufferStrategy;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class flicker extends JFrame implements Runnable {
JTable table; // Creating a table
DefaultTableModel model = new DefaultTableModel(); // Defining a model for the table
JButton Acknowledge; // Creating an instance of Button "Acknowledge"
JPanel Panel; // Creating a panel to hold below 2 panes
JScrollPane Pane; // Creating a pane to hold the table
JScrollPane Pane1; // Creating a pane1 to hold the Button "Acknowledge"
JScrollPane Pane2;
String[] Optlist = {"Show Alarm", "Show History", "Report Alarm (PDF)", "Report History (PDF)"};
JComboBox Options=null;
BufferStrategy myStrategy;
String p=null;
String w=null;
// Creating an instance of class "Clientack"
// Creating an instance to play "Sound File"
String area=null;
int i,r;
String ip,selection,stso,group,message,p1,p2,Sensor;
static String checkedval;
static String Name;
Double Value;
Object data[][]=null;
static Object abc=null;
static Object Checkedvalues;
public static void main(String [] args)
{
flicker myRunnable = new flicker();
Thread t = new Thread(myRunnable);
t.start();
}
@Override
public void run()
{
try
{
setTitle( group );
setSize(520,570);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
Panel=new JPanel();
Panel.setLayout(null);
getContentPane().add(Panel);
model = new DefaultTableModel();
Scanner sc = new Scanner(System.in);
while (true)
{
System.out.println("Write message here: ");
message=sc.nextLine();
System.out.println("Message is: "+message);
Object col[] = {"Sensor","Values","Arrival_Time","Status",""};
if(!message.equals("ITS EMPTY"))
{
String[] parts = message.split("\t");
data = new Object[parts.length / 4][5];
for (i = 0, r = 0; r < data.length; r++)
{
data[r][0] = parts[i++];
//data[r][1] = new Double(parts[i++]) ;
data[r][1] = parts[i++] ;
data[r][2] = parts[i++];
data[r][3] = parts[i++];
p=(String) data[r][3];
w = (String) abc(p);
data[r][4]=Boolean.parseBoolean(w) ;
// System.out.println(data[r][4]);
}
//System.out.println(java.util.Arrays.deepToString(data));
//Display Table
model.setDataVector(data, col);
table = new JTable(model)
{
private static final long serialVersionUID = 1L;
@Override
public Class getColumnClass(int column) {
switch (column) {
case 0:
return String.class;
case 1:
return String.class;
case 2:
return String.class;
case 3:
return String.class;
default:
return Boolean.class;
}
}
};
table.setDefaultRenderer(Object.class, new CustomTableCellRenderer3());
table.setPreferredScrollableViewportSize(table.getPreferredSize());
table.getColumnModel().getColumn(4).setCellEditor(new CustomTableCellRenderer(new JCheckBox(),Name));
Pane=new JScrollPane(table);
Pane.setSize(500,500);
Pane1=new JScrollPane(Acknowledge);
Pane1.setSize(90,30);
Pane1.setBounds(0,500,320,30);
Pane2=new JScrollPane(Options);
Pane2.setBounds(320,500,180,30);
Panel.add(Pane);
Panel.add(Pane1);
Panel.add(Pane2);
setVisible(true);
}
else
{
System.out.println("NO TABLE");
}
}
}
catch(Exception e)
{
System.out.println("Exception Occured:" +e);
}
}
private Object abc(String p)
{
String val=null;
if(p.equals("ACK"))
{
val="true";
}
else if(p.equals("nil"))
{
val="false";
}
return val;
}
}
....................................customtablecellrenderer class
import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.AbstractButton;
import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JTable;
public class CustomTableCellRenderer extends DefaultCellEditor implements ItemListener {
private static final long serialVersionUID = 1L;
private JCheckBox checkBox;
private JTable table1;
private int row;
Object abc="null";
private String Name;
public CustomTableCellRenderer(JCheckBox checkBox,String name2) {
super(checkBox);
this.checkBox = checkBox;
this.checkBox.addItemListener(this);
this.Name=name2;
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected,int row, int column)
{
this.row = row;
this.table1=table;
checkBox.setSelected((Boolean)value);
return super.getTableCellEditorComponent(table,value,isSelected,row, column);
}
public void itemStateChanged(ItemEvent e)
{
this.fireEditingStopped();
//System.out.println("Item Changed " + row + " value is: " + checkBox.isSelected());
//System.out.println("Item Changed " + column + " value is: " + checkBox.isSelected());
String Sensor =(String) table1.getValueAt(row, 0);
String Value =(String) table1.getValueAt(row, 1);
String Date =(String) table1.getValueAt(row, 2);
//String Status=(String) table1.getValueAt(row, 3);
Boolean select=(Boolean) table1.getValueAt(row,4);
if (Boolean.TRUE.equals(select))
{
abc += Sensor+"\t"+Value+"\t"+Name+"\t"+Date+"\t";
System.out.println(abc);
}
else
{
}
}
}
....................................customtablecellrenderer3 class
import java.awt.Color;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
public class CustomTableCellRenderer3 extends DefaultTableCellRenderer{
public Component getTableCellRendererComponent (JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
Component c = super.getTableCellRendererComponent(table, value, isSelected, isSelected, row, column);
Boolean last=(Boolean)table.getValueAt(row, 4);
String status=(String)table.getValueAt(row, 3);
if(Boolean.TRUE.equals(last) && status.equals("ACK"))
{
c.setBackground(Color.green);
}
else if(Boolean.TRUE.equals(last) && status.equals("nil"))
{
c.setBackground(Color.yellow);
}
else
{
c.setBackground(Color.red);
}
table.repaint();
return c;
}
}